如何获取当前会话区域设置?

时间:2014-10-29 19:15:50

标签: java spring session

@Autowired
private SessionLocaleResolver localeResolver;

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    // clean previous failed access
    authorizationServiceConfig.cleanUserFailedAccess();

    // set the session locale
    localeResolver.setLocale(request, response, authorizationServiceConfig.getUserLocale());

    //redirect login..
    handle(request, response, authentication);

}

我需要获取我在身份验证服务中设置的SessionLocaleResolver区域设置值。但我没有这里的要求:(

public String getMessage(String key) {
    String answer;

    if (key == null) {
        return "";
    }
    try {
        answer = getMessageSourceAccessor().getMessage(key, "here i need the Session locale");
    } catch (Exception e) {
        answer = key;
    }
    return answer;
}

1 个答案:

答案 0 :(得分:0)

如果只想在没有指向它的方法中获取当前请求,RequestContextHolder就是你的朋友。由于这是一个常见的要求,spring将一个对象存储在一个线程局部变量中,并通过该类的静态方法提供对它的访问:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                                 .getRequestAttributes()).getRequest();

当然如果在servlet应用程序中以这种方式工作而不是在portlet中工作 - 你必须使用PortletRequestAttributes并最终获得PortletRequest