java - Spring security redirect when maximum sessions for this principal exceeded -
java - Spring security redirect when maximum sessions for this principal exceeded -
so user login -> closes browser -> opens browser 1 more time -> error appears:
http status 401 - authentication failed: maximum sessions of 1 principal exceeded
what need capture event session invalid, remove sessions user , redirect normal login page
spring security config:
<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>
i tried implement filter, shows session null:
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; } }
from understood, want invalidate previous session if user's session exceeds 'max-sessions'. set property 'error-if-maximum-exceeded' false. spring security automatically invalidates previous session.
if trying different,
extend concurrentsessioncontrolstrategy class, , override 'allowablesessionsexceeded' method. specify bean reference of above 'session-authentication-strategy-ref' attribute value of 'session-management'.
java spring spring-security
Comments
Post a Comment