首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并发控制是否可重写

并发控制是否可重写
EN

Stack Overflow用户
提问于 2013-01-18 18:12:08
回答 2查看 1.9K关注 0票数 2

我的应用程序中有security.xml security.xml

代码语言:javascript
复制
<session-management session-authentication-error-url="/genesis"> 
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" expired-url="/genesis?sessionExpired=true"/> 
    </session-management>

这将用户限制为单个会话。然而,我现在有一个要求,一个帐户必须允许多个会话,同时仍然限制所有其他帐户的单一会话。

对于如何实现这一点,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-18 18:34:55

覆盖默认并发筛选器。跳过对特殊用户的处理:

代码语言:javascript
复制
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中将默认过滤器替换为自定义过滤器:

代码语言:javascript
复制
<security:http ... >
    <security:custom-filter position="CONCURRENT_SESSION_FILTER" ref="customConcurrentSessionFilter"/>
</security:http>

<bean id="customConcurrentSessionFilter" class="com.domain.CustomConcurrentSessionFilter"/>
票数 2
EN

Stack Overflow用户

发布于 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

代码语言:javascript
复制
<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" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14396621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档