gpt4 book ai didi

java - Spring security 并发控制不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:32 25 4
gpt4 key购买 nike

我尝试在 Spring security 3.2 中实现并发控制。我使用表单登录进行身份验证。这是我的 security.xml

    <beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http access-denied-page="/login.html" create-session="ifRequired">

<intercept-url pattern="/settings.html" access="ROLE_USER"/>
<intercept-url pattern="/history.html" access="ROLE_USER"/>

<form-login login-page="/"
authentication-failure-url="/error.do"
default-target-url="/logged.do"
always-use-default-target="true"
login-processing-url="/j_spring_security_check"/>

<logout logout-url="/j_spring_security_logout" logout-success-url="/index.html" invalidate-session="true"/>

<session-management session-authentication-strategy-ref="sas"/>

<custom-filter ref="accessFilter" after="FORM_LOGIN_FILTER" />
<custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
<custom-filter before="FORM_LOGIN_FILTER" ref="myAuthFilter" />
</http>

<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />

<beans:bean id="accessFilter" class="ua.com.site.http.filter.PlayerAccessFilter" />

<beans:bean id="passwordUserDetailService" class="ua.com.site.web.security.cristal.PasswordUserDetailService">
<beans:property name="playerDao" ref="playerDao"/>
</beans:bean>

<beans:bean id="tokenUserDetailsService" class="ua.com.site.web.security.cristal.TokenUserDetailService">
<beans:property name="playerDao" ref="playerDao"/>
</beans:bean>

<beans:bean id="passwordAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="passwordEncoder">
<beans:bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
</beans:property>
<beans:property name="userDetailsService" ref="passwordUserDetailService" />
</beans:bean>

<beans:bean id="tokenAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="passwordEncoder">
<beans:bean class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder" />
</beans:property>
<beans:property name="userDetailsService" ref="tokenUserDetailsService" />
</beans:bean>

<authentication-manager alias="authenticationManager">
<authentication-provider ref="passwordAuthenticationProvider" />
<authentication-provider ref="tokenAuthenticationProvider" />
</authentication-manager>

<beans:bean id="concurrentSessionController" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="maximumSessions" value="1" />
</beans:bean>


<beans:bean id="concurrencyFilter"
class="org.springframework.security.web.session.ConcurrentSessionFilter">
<beans:property name="sessionRegistry" ref="sessionRegistry" />
<beans:property name="expiredUrl" value="/expired.html" />
</beans:bean>

<beans:bean id="myAuthFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="sessionAuthenticationStrategy" ref="sas" />
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<beans:bean id="sas" class="org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy">
<beans:constructor-arg>
<beans:list>
<beans:bean class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
<beans:constructor-arg ref="sessionRegistry"/>
<beans:property name="maximumSessions" value="1" />
<beans:property name="exceptionIfMaximumExceeded" value="true" />
</beans:bean>
<beans:bean class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy">
</beans:bean>
<beans:bean class="org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy">
<beans:constructor-arg ref="sessionRegistry"/>
</beans:bean>
</beans:list>
</beans:constructor-arg>
</beans:bean>

监听器

<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>

包含在我的 web.xml 中。

我构建了该应用程序,在不同的浏览器中运行它,在两个浏览器中都登录,但我有 2 个 Activity session 。看来并发控制不起作用。

如何使用form-login实现并发控制?

在以前的版本中我只是使用

<session-management>
<concurrency-control max-sessions="1" expired-url="/expired.html" error-if-maximum-exceeded="false" session-registry-ref="sessionRegistry" />
</session-management>

感谢您的建议。

最佳答案

SessionRegistry使用UserDetails的equals()/hashCode()来查找同一用户的 session 。我有自定义 UserDetails,因此我必须实现此方法并恢复以前的配置。

关于java - Spring security 并发控制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26843383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com