---
title: "Grails 后台代码中获取session变量信息"
author: "Ashton"
date: "2012-04-25T13:43:47Z"
tags: []
categories: ["Web前端"]
slug: "grails-session"
draft: false
lastmod: "2012-04-25T13:43:47Z"
id: 1497968
url: "https://ashton.iteye.com/blog/1497968"
---

在Grails项目中用到了这个，先记下来，实质上是Spring提供的支持，通过ThreadLocal把request信息保存在执行当前请求的线程里，后台代码就可以从当前线程ThrealLocal变量里就可以取得调用它的那个HttpRequest信息了。

```java

import org.springframework.web.context.request.RequestContextHolder

def userName  = RequestContextHolder.currentRequestAttributes().getSession()?.userName

```