每个用户组或每个用户的Spring Security并发会话控制
问题描述:
我写这个问题是为了了解如何控制用户可以提到的会话的数量Spring Security 在春天,我可以定义最大数量会议的所有用户必须拥有即低谷会话管理我定义例如所有用户不应该被允许有更多然后3届每个用户组或每个用户的Spring Security并发会话控制
.sessionManagement().maximumSessions(3)
这本身是不够的,即我们需要给servlet容器中通知春季安全更新会话或删除会话等方式,因此我们需要配置HttpSessionEventPublishet
@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}
现在这里是如何配置类似的东西的问题: 管理员用户应当允许有会话设置为8届亲管理员用户的最大数量,但普通用户不应该被允许有更多的则每个用户一个会话。
答
默认策略只允许在全局范围内设置最大会话,而不管用户。该物业位于ConcurrentSessionControlAuthenticationStrategy
级,该物业拥有一个简单的二手房。
实际值由getMaximumSessionsForThisUser
方法确定,该方法在默认实现中返回maximumSession
属性的值。
您需要通过要么完全实现它自己通过创建imlpements SessionAuthenticationStrategy
,或者更简单的一类,通过创建ConcurrentSessionControlAuthenticationStrategy
子类,只需覆盖getMaximumSessionsForThisUser
方法来实现自己的策略。
public class CustomConcurrentSessionControlAuthenticationStrategy extends ConcurrentSessionControlAuthenticationStrategy {
protected int getMaximumSessionsForThisUser(Authentication authentication) {
boolean admin = // Check authentication.getAuthorities() for the admin role
return admin ? 8 : 1;
}
}
然后在你的配置创建一个@Bean
它和电线这个bean的配置的sessionManagement
部分。
@Bean
public CustomConcurrentSessionControlAuthenticationStrategy sessionControlStrategy() {
return new CustomConcurrentSessionControlAuthenticationStrategy(new SessionRegistryImpl());
}
在安全配置代码
然后以此为基础的`ConcurrentSessionControlAuthenticationStrategy`类像
sessionManagement().sessionAuthenticationStrategy(sessionControlStrategy());
实现自己的策略。你需要重写'getMaximumSessionsForThisUser'方法并在那里实现你的逻辑。然后用'sessionmanagement'连接你的定制imlpementation。 – 2014-11-06 06:56:33
Deinum谢谢你的回应,我将花一周时间处理这些信息,并让你知道这是否是答案,这对我来说似乎是合理的。再次感谢您的快速反馈。 – Tito 2014-11-06 08:13:36