我有默认的 spring security 登录表单j_spring_security_check
。登录过程工作也很好。但登录后我遇到了一个麻烦。我的 session 也很不稳定。如果我尝试从浏览器的另一个选项卡访问我的网站。看起来我没有登录。但在我的第一个选项卡中,我也登录了并且可以查看一些私有(private)页面。但有时一切都工作正常。但只是有时。正如我所说,用户 session 非常不稳定。也许有人可以告诉我为什么?谢谢!
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-dispatcher-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>
spring-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>
感谢您的帮助!抱歉,如果我的英语有问题。
最佳答案
并不意味着完整的答案,只是作为建议。
为了了解 Spring 管理的 Web 应用程序中的 session 创建和销毁,请创建 @Component
实现 ApplicationListener<AuthenticationSuccessEvent>
和ApplicationListener<HttpSessionDestroyedEvent>
。他们的onApplicationEvent()
将调用方法至少为您提供一些 session 行为的指示。
您还需要向 web.xml
添加一个监听器如下
<!-- translates session callbacks to Spring application events -->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
关于java - Spring 安全。 session 不稳定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21714513/