HttpServletRequest 获取新会话
HttpServletRequest getting new session
我有一个通过 oauth 进行身份验证的应用程序。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpResp = (HttpServletResponse) response;
// Check if already logged in
if (getUser(httpReq) != null) {
chain.doFilter(request, response);
return;
}
// Try to parse auth response
if (procAuthResponse(httpReq)) {
chain.doFilter(request, response);
return;
}
// Go to auth server
sendAuthRequest(httpReq, httpResp);
}
这很好用。
在方法 procAuthResponse 中,我将服务器的响应与此进行配对。
HttpSession session = request.getSession();
session.setAttribute(USER_PRINCIPLE_ATR, userInfo);
它也工作得很好,但是有一个会话范围 class,方法是 getCurrent 用户,servlet 使用它。
public UserInfo getCurrentUser() {
HttpSession session = getHttpSession();
if (session == null) {
LOG.warn("Method getCurrentUser: unable to find a session");
return null;
}
Object user = session.getAttribute(OAuthLoginFilter.USER_PRINCIPLE_ATR);
if (!(user instanceof UserInfo)) {
LOG.warn(String.format("Method getCurrentUser, wrong type for attribute %s", OAuthLoginFilter.USER_PRINCIPLE_ATR));
return null;
}
currentUser = (UserInfo) user;
return currentUser;
}
此方法被多次调用,事实证明,在第一次调用时,一切都按预期工作,之后 getHttpSession() returns 一个不同的会话,不包含在过滤器 class。每次都不是新会话,没有所需信息的会话总是相同的。
getHttpSession()的代码
private HttpSession getHttpSession() {
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (!(request instanceof HttpServletRequest)) {
LOG.warn("not a valid http request");
return null;
}
HttpServletRequest hreq = (HttpServletRequest) request;
return hreq.getSession(false);
}
你知道为什么会这样吗?
谢谢你的帮助
还有一个旧过滤器 class,未在 web.xml 中配置,但用 @WebFilter("/*")
注释。
我删除了这个文件,现在一切正常。
我有一个通过 oauth 进行身份验证的应用程序。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpResp = (HttpServletResponse) response;
// Check if already logged in
if (getUser(httpReq) != null) {
chain.doFilter(request, response);
return;
}
// Try to parse auth response
if (procAuthResponse(httpReq)) {
chain.doFilter(request, response);
return;
}
// Go to auth server
sendAuthRequest(httpReq, httpResp);
}
这很好用。 在方法 procAuthResponse 中,我将服务器的响应与此进行配对。
HttpSession session = request.getSession();
session.setAttribute(USER_PRINCIPLE_ATR, userInfo);
它也工作得很好,但是有一个会话范围 class,方法是 getCurrent 用户,servlet 使用它。
public UserInfo getCurrentUser() {
HttpSession session = getHttpSession();
if (session == null) {
LOG.warn("Method getCurrentUser: unable to find a session");
return null;
}
Object user = session.getAttribute(OAuthLoginFilter.USER_PRINCIPLE_ATR);
if (!(user instanceof UserInfo)) {
LOG.warn(String.format("Method getCurrentUser, wrong type for attribute %s", OAuthLoginFilter.USER_PRINCIPLE_ATR));
return null;
}
currentUser = (UserInfo) user;
return currentUser;
}
此方法被多次调用,事实证明,在第一次调用时,一切都按预期工作,之后 getHttpSession() returns 一个不同的会话,不包含在过滤器 class。每次都不是新会话,没有所需信息的会话总是相同的。
getHttpSession()的代码
private HttpSession getHttpSession() {
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (!(request instanceof HttpServletRequest)) {
LOG.warn("not a valid http request");
return null;
}
HttpServletRequest hreq = (HttpServletRequest) request;
return hreq.getSession(false);
}
你知道为什么会这样吗?
谢谢你的帮助
还有一个旧过滤器 class,未在 web.xml 中配置,但用 @WebFilter("/*")
注释。
我删除了这个文件,现在一切正常。