0

我在tomcat服务器中使用spring security。如何更改默认会话超时?

我尝试使用以下方法修改 web.xml:

<session-config>
         <session-timeout>1</session-timeout>
</session-config>

这似乎不起作用。

我还读到spring boot使用参数server.servlet.session.timeout,但我不使用spring boot。

4

2 回答 2

0

请记住,此值以秒为单位

<session-config>
         <session-timeout>1</session-timeout>
</session-config>

这个值将四舍五入到分钟。

如果服务器没有收到来自例如您的 GUI 的任何请求,它将等待至少 1 分钟然后过期会话。

于 2020-11-14T10:31:21.813 回答
0

在 Spring Security 中配置会话超时时间(maxInactiveInterval)的不同方法。

  1. 通过在 web.xml 中添加会话配置

  2. 通过创建 HttpSessionListener 的实现并将其添加到 servlet 上下文。(来自 munilvc 的回答)

  3. 通过在 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" />
于 2019-12-04T10:16:55.310 回答