如何在spring安全性中添加注销功能

时间:2017-02-27 01:54:20

标签: java spring spring-security

我有使用spring security的登录功能,下面是代码:

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.authorizeRequests().antMatchers("/**").hasRole("USER")
    .and()
    .formLogin().loginPage("login.html").permitAll();

    httpSecurity.authorizeRequests().antMatchers("/taxi_state?start=*&end=*&zone_id=*").hasRole("USER")
    .and()
    .formLogin().loginPage("login.html").permitAll();
}

但我也想要注销功能。如何在代码中使用注销功能?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

您可以使用logout方法配置注销功能,如本示例底部所示:

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity             
          .authorizeRequests()
             .antMatchers("/")
                 .permitAll()
             .anyRequest()
                 .authenticated()
             .and()
          .formLogin()
             .loginPage("/login")
             .permitAll()
             .and()
          .logout()
             .permitAll()
         ;
 }

这将提供注销的默认配置。例如,URL将为/logout,会话将失效。您可以通过调用logoutUrl方法来更改注销URL。

请注意,"/login"等视图路径不需要.html扩展名。它们通常会在内部使用默认的Thymeleaf视图配置解析为login.html

相关问题