我有默认的spring security登录表单j_spring_security_check
。登录过程也很好。但登录后我遇到了一个麻烦。我的会议也非常不稳定。如果我尝试从浏览器的另一个标签访问我的网站。看起来我还没有登录。但在我的第一个标签中,我也记录了,可以查看一些私人页面。但有时一切都很好。但有时候只是。正如我所说,用户会话非常不稳定。可能有人可以说我为什么?谢谢!
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring MVC Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
MVC-调度-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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">
<context:annotation-config />
<context:component-scan base-package="com.petrez" />
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:annotation-driven />
<mvc:resources mapping="/res/**" location="file:/home/piotr/Work/apache-tomcat-7.0.42/work-dir/res/" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000000"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/Library" />
<property name="username" value="root" />
<property name="password" value="G190419g" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="com.petrez" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDetailsService" class="com.petrez.service.CustomUserDetailsService" />
<bean id="fileUploadService" class="com.petrez.service.FileUploadServiceImpl">
<property name="savePath" value="/home/piotr/Work/apache-tomcat-7.0.42/work-dir" />
</bean>
</beans>
弹簧security.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<http auto-config="true" use-expressions="true" create-session="always">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
<form-login login-page="/"
login-processing-url="/j_spring_security_check"
default-target-url="/home"
authentication-failure-url="/?error=1"/>
<remember-me user-service-ref="userDetailsService" />
<logout logout-success-url="/" />
<session-management invalid-session-url="/">
<concurrency-control expired-url="/" />
</session-management>
</http>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
</beans:beans>
index.jsp //登录部分
<form method="post" action="<c:url value='/j_spring_security_check'/>">
<table>
<tr>
<b>Login</b>
</tr>
<tr>
<td>Login:</td>
<td><input type="text" name="j_username" size="30" maxlength="40" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password" size="30" maxlength="32" /></td>
</tr>
<tr>
<td>Remember me?</td>
<td><input type="checkbox" name="_spring_security_remember_me" checked="checked" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
感谢您的帮助!对不起,如果我的英文有问题。
答案 0 :(得分:2)
不是完整的答案,就像建议一样。
要了解Spring托管Web应用程序中的会话创建和销毁,请创建实现@Component
和ApplicationListener<AuthenticationSuccessEvent>
的{{1}}。他们的ApplicationListener<HttpSessionDestroyedEvent>
方法将被调用,至少可以为您提供会话行为的一些指示。
您还需要为onApplicationEvent()
添加一个监听器,如下所示
web.xml