【问题标题】:Spring Security Session Timeout - JavaSpring Security 会话超时 - Java
【发布时间】:2018-09-27 14:03:18
【问题描述】:

我的 Spring 安全性运行良好,但经过一段时间后,当用户访问页面时,我开始收到大量异常。

我注意到 session and principal is null 这就是我收到错误 500 的原因。

如何再次将用户重定向到登录?

或者我可以简单地删除会话超时(我真的不需要它)

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
@ComponentScan("pt.impactzero.atp")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailsService myUserDetailsService;
    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").hasAnyRole("Administrator" , "Member")
                .and()
                .formLogin()
                    .loginPage("/login").permitAll()
                    .defaultSuccessUrl("/dashboard",true)
                    .failureUrl("/login?error")
                    .successHandler(authenticationSuccessHandler)
                .and()
                .logout()
                    .logoutUrl("/logout").permitAll()
                    .logoutSuccessUrl("/login")
                    .logoutSuccessHandler(logoutSuccessHandler)
                .and()
                .csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) {
        web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService).passwordEncoder(passwordEncoder());
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder;
    }

    @Bean
    public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
        DefaultHttpFirewall firewall = new DefaultHttpFirewall();
        firewall.setAllowUrlEncodedSlash(true);
        return firewall;
    }

    @Bean
    public CommonsMultipartResolver filterMultipartResolver(){
        return new CommonsMultipartResolver();
    }

    //Online users
    @Bean
    public ActiveUsers activeUsers(){
        return new ActiveUsers();
    }
}

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    如果你想禁用会话,你可以:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // disable session
        http.sessionManagement().disable()
            .authorizeRequests().antMatchers("/").hasAnyRole("Administrator" , "Member")
                .and()
                .formLogin()
                    .loginPage("/login").permitAll()
                    .defaultSuccessUrl("/dashboard",true)
                    .failureUrl("/login?error")
                    .successHandler(authenticationSuccessHandler)
                .and()
                .logout()
                    .logoutUrl("/logout").permitAll()
                    .logoutSuccessUrl("/login")
                    .logoutSuccessHandler(logoutSuccessHandler)
                .and()
                .csrf().disable();
    }
    

    如果你想将用户重定向到登录页面,你可以试试这个:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // redirect user to login page
        http.sessionManagement().invalidSessionUrl("http://your.login.page").and()
            .authorizeRequests().antMatchers("/").hasAnyRole("Administrator" , "Member")
                .and()
                .formLogin()
                    .loginPage("/login").permitAll()
                    .defaultSuccessUrl("/dashboard",true)
                    .failureUrl("/login?error")
                    .successHandler(authenticationSuccessHandler)
                .and()
                .logout()
                    .logoutUrl("/logout").permitAll()
                    .logoutSuccessUrl("/login")
                    .logoutSuccessHandler(logoutSuccessHandler)
                .and()
                .csrf().disable();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 2014-10-18
      • 2015-12-10
      • 2014-05-04
      • 2011-07-20
      • 2014-10-29
      • 2017-01-31
      • 2012-12-26
      • 2016-08-16
      相关资源
      最近更新 更多