[英]How to enable session and set session timeout in Spring Security
[英]How to set spring security session timeout?
我在 tomcat 服務器中使用 spring security。 如何更改默認會話超時?
我試過用以下方法修改 web.xml:
<session-config>
<session-timeout>1</session-timeout>
</session-config>
這似乎不起作用。
我還讀到 spring boot 使用參數 server.servlet.session.timeout,但我不使用 spring boot。
Spring Security 中配置會話超時時間(maxInactiveInterval)的不同方式。
通過在 web.xml 中添加會話配置
通過創建 HttpSessionListener 的實現並將其添加到 servlet 上下文。(來自 munilvc 的回答)
通過在 spring 安全配置中注冊您的自定義 AuthenticationSuccessHandler ,並在 onAuthenticationSuccess 方法中設置會話最大非活動間隔。
這種實現有優勢
登錄成功后,您可以為不同的角色/用戶設置不同的 maxInactiveInterval 值。
登錄成功后,您可以在會話中設置用戶對象,因此可以在任何控制器中從會話訪問用戶對象。
缺點:不能為匿名用戶(未經身份驗證的用戶)設置會話超時
創建 AuthenticationSuccessHandler 處理程序
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler{ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); if (roles.contains("ROLE_ADMIN")) { request.getSession(false).setMaxInactiveInterval(60); } else { request.getSession(false).setMaxInactiveInterval(120); } //Your login success url goes here, currently login success url="/" response.sendRedirect(request.getContextPath()); } }
注冊成功處理程序
以 Java Config 方式
@Override
protected void configure(final HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.antMatchers("/resources/**", "/login"").permitAll()
.antMatchers("/app/admin/*").hasRole("ADMIN")
.antMatchers("/app/user/*", "/").hasAnyRole("ADMIN", "USER")
.and().exceptionHandling().accessDeniedPage("/403")
.and().formLogin()
.loginPage("/login").usernameParameter("userName")
.passwordParameter("password")
.successHandler(new MyAuthenticationSuccessHandler())
.failureUrl("/login?error=true")
.and().logout()
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
.invalidateHttpSession(true)
.and().csrf().disable();
http.sessionManagement().maximumSessions(1).expiredUrl("/login?expired=true");
}
以xml配置方式
<http auto-config="true" use-expressions="true" create-session="ifRequired">
<csrf disabled="true"/>
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/app/admin/*" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" />
<intercept-url pattern="/app/user/*" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" />
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
authentication-success-handler-ref="authenticationSuccessHandler"
authentication-failure-url="/login?error=true"
username-parameter="userName"
password-parameter="password" />
<logout invalidate-session="false" success-handler-ref="customLogoutSuccessHandler"/>
<session-management invalid-session-url="/login?expired=true">
<concurrency-control max-sessions="1" />
</session-management>
</http>
<beans:bean id="authenticationSuccessHandler" class="com.pvn.mvctiles.configuration.MyAuthenticationSuccessHandler" />
請記住,此值以秒為單位
<session-config>
<session-timeout>1</session-timeout>
</session-config>
此值將四舍五入到分鍾。
如果服務器沒有收到來自例如 GUI 的任何請求,它將等待至少 1 分鍾,然后使會話過期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.