所以用戶登錄- >關閉瀏覽器- >打開瀏覽器再一次- >出現錯誤:春季安全重定向時,此主體最大會話數超過
HTTP Status 401 - Authentication Failed: Maximum sessions of 1 for this principal exceeded
我需要的是捕捉到這一事件會話無效,刪除所有會話該用戶並重定向到正常的登錄頁面
春季安全配置:
<http auto-config="true" use-expressions="true">
<session-management session-fixation-protection="migrateSession">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/>
</session-management>
<intercept-url pattern="/login" access="hasRole('ROLE_ANONYMOUS')" requires-channel="any"/>
<!--<custom-filter after="CONCURRENT_SESSION_FILTER" ref="sessionExpiration" /> -->
<!-- .... -->
</http>
<beans:bean id="sessionExpiration" class="com.test.security.SessionExpirationFilter">
<beans:property name="expiredUrl">
<beans:value>/login</beans:value>
</beans:property>
</beans:bean>
我試圖執行一些過濾器,但它總是顯示會話爲空:
public class SessionExpirationFilter implements Filter, InitializingBean {
private String expiredUrl;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String path = httpRequest.getServletPath();
HttpSession session = httpRequest.getSession(false);
System.out.println(session);
if (session == null && !httpRequest.isRequestedSessionIdValid()) {
SecurityContextHolder.getContext().setAuthentication(null);
String targetUrl = httpRequest.getContextPath()
+ expiredUrl;
httpResponse.sendRedirect(httpResponse.encodeRedirectURL(targetUrl));
return;
}
chain.doFilter(request, response);
}
public void setExpiredUrl(String expiredUrl) {
this.expiredUrl = expiredUrl;
}
}
我已經設置'錯誤如果利用最大exceeded'爲'FALSE'。當我嘗試多次登錄時,最新的會話將會使以前的會話失效。但是,*如果我必須保持第一次會話,並且不允許在第一次登出完成之前再次登錄,該怎麼辦?*您是否知道如何實現這一點? – OO7 2015-05-14 09:19:55
@ OO7您需要將該屬性設置爲'true',那麼第二個會話將不被允許 'error-if-maximum-exceeded =「true」' – 2016-09-28 07:06:33