Java Spring安全会话创建
如何在每次创建会话时初始化应用程序 因为我面临的问题是 当一个用户登录到我的应用程序时,将创建新会话 当另一个用户在看到第一个用户登录的同时尝试登录时,我需要知道如何在两个用户之间没有任何冲突的情况下为第二个用户初始化应用程序 我的班级是Java Spring安全会话创建,java,session,jakarta-ee,jsf-2,spring-security,Java,Session,Jakarta Ee,Jsf 2,Spring Security,如何在每次创建会话时初始化应用程序 因为我面临的问题是 当一个用户登录到我的应用程序时,将创建新会话 当另一个用户在看到第一个用户登录的同时尝试登录时,我需要知道如何在两个用户之间没有任何冲突的情况下为第二个用户初始化应用程序 我的班级是 @Component public class MyHttpSessionEventPublisher extends HttpSessionEventPublisher { @Autowired LoginBean loginBean;
@Component
public class MyHttpSessionEventPublisher extends HttpSessionEventPublisher {
@Autowired
LoginBean loginBean;
@Override
public void sessionCreated(HttpSessionEvent event) {
super.sessionCreated(event);
event.getSession().getId();
// loginBean.setLoginDao(null);
System.out.println("Session id is : "+ event.getSession().getId());
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> session created <<<<<<<<<<<<<<<<<<<<<<<<<");
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
//do something
super.sessionDestroyed(event);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>> session destroyed <<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
@组件
公共类MyHttpSessionEventPublisher扩展了HttpSessionEventPublisher{
@自动连线
罗金班罗金班;
@凌驾
已创建公共无效会话(HttpSessionEvent事件){
super.sessionCreated(事件);
event.getSession().getId();
//loginBean.setLoginDao(空);
System.out.println(“会话id为:+event.getSession().getId());
System.out.println(“>>>>>>>>>>>>>>>>>>>>>>>>>>创建的会话为了允许多个用户同时使用同一用户名登录,我们可以使用http元素的并发控制功能,请参阅:
通过将此添加到web.xml中,谨防常见陷阱,否则并发登录将无法工作:
<listener>
<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>
org.springframework.security.ui.session.HttpSessionEventPublisher
有关常见问题,请参见此部分。谢谢您的回复,但问题是不允许两个不同的用户登录,当第二个用户尝试登录时,他看到已经有其他用户登录,关于第二点,我将main.com.zc.attSys.security.beans.MyHttpSessionEventPublisher是否有相同的权限?或者我应该如何为新用户初始化应用程序?这应该是spring security的开箱即用功能,您可以发布您的web.xml和您的spring security配置吗?您使用的是spring security right,还是手动管理会话?我编辑了帖子和附加的web.xml,spring security文件是的,我使用的是spring security,但是我是spring security的新手我第一次使用它如果你在同一个浏览器中登录,你会看到第一次登录。这是正常的,也是意料之中的。确保你使用的是不同的浏览器。是的,我使用的是不同的浏览器,用例是什么,同时使用两个不同的用户登录,或者在不同的浏览器中登录两次br使用同一用户登录的用户?您在尝试登录时会收到什么错误消息或错误行为,您可以发布日志吗?此案例是一种正常的网站行为,如Facebook不同用户可以同时使用其帐户登录,我开发的网站可以看到,例如,如果您在我之前登录,我尝试登录,我会看到ne已登录到我的应用程序,允许..!内存未清理或会话未创建,无法新访问我不知道的应用程序!:(
<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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:property-placeholder location="classpath:resources/jdbc.properties" />
<!-- For Spring auto wiring -->
<tx:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="main.com.zc.attSys" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<http auto-config='true'>
<!-- <intercept-url pattern="/**" access="ROLE_USER"/> -->
<form-login username-parameter="Mail" password-parameter="Password"
login-page="/pages/courseFeedBack/ask/login.xhtml"
login-processing-url="/home.xhtml"
always-use-default-target="true"
authentication-failure-url="/pages/courseFeedBack/ask/login.xhtml"
/>
</http>
<!-- <authentication-manager> <authentication-provider> <user-service> <user
name="joseph" password="bagnes" authorities="Admin, User"/> <user name="bernabe"
password="jose" authorities="User"/> </user-service> </authentication-provider>
</authentication-manager> -->
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService"></beans:property>
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
</beans:beans>
<http>
<session-management>
<concurrency-control max-sessions=2 />
</session-management>
</http>
<listener>
<listener-class>org.springframework.security.ui.session.HttpSessionEventPublisher</listener-class>
</listener>