我的应用程序中有security.xml security.xml
<session-management session-authentication-error-url="/genesis">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" expired-url="/genesis?sessionExpired=true"/>
</session-management>这将用户限制为单个会话。然而,我现在有一个要求,一个帐户必须允许多个会话,同时仍然限制所有其他帐户的单一会话。
对于如何实现这一点,有什么建议吗?
发布于 2013-01-18 18:34:55
覆盖默认并发筛选器。跳过对特殊用户的处理:
public class CustomConcurrentSessionFilter extends ConcurrentSessionFilter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
ServletException {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!auth.getName().equals("bob")) {
super.doFilter(req, res, chain);
}
}
}在conf中将默认过滤器替换为自定义过滤器:
<security:http ... >
<security:custom-filter position="CONCURRENT_SESSION_FILTER" ref="customConcurrentSessionFilter"/>
</security:http>
<bean id="customConcurrentSessionFilter" class="com.domain.CustomConcurrentSessionFilter"/>发布于 2013-03-26 19:45:09
(我在这里展开我的评论,为这个问题提供更完整的解决方案。)
只需在XML子类(下面我使用com.example.CustomConcurrentSessionFilter)和XML add中覆盖getMaximumSessionsForThisUser():
SessionAuthenticationStrategy bean ( id为"sas"),<bean:property name="sessionAuthenticationStrategy" ref="sas" /> in UsernamePasswordAuthenticationFilter,<http>中的
<session-management session-authentication-strategy-ref="sas" />,
<bean:property name="sessionAuthenticationStrategy" ref="sas" />到您的UsernamePasswordAuthenticationFilter完整的设置应类似于所示的here in docs
<http>
<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" />
<session-management session-authentication-error-url="/genesis"
session-authentication-strategy-ref="sas"/>
</http>
<beans:bean id="concurrencyFilter"
class="com.example.CustomConcurrentSessionFilter">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="expiredUrl" value="/genesis?sessionExpired=true" />
</beans:bean>
<beans:bean id="myAuthFilter"
class="o.s.s.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="sessionAuthenticationStrategy" ref="sas" />
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>
<beans:bean id="sas"
class="o.s.s.web.authentication.session.ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
<beans:property name="maximumSessions" value="1" />
</beans:bean>
<beans:bean id="sessionRegistry"
class="o.s.s.core.session.SessionRegistryImpl" />https://stackoverflow.com/questions/14396621
复制相似问题