SpringBoot安全性注销行为

时间:2018-07-30 12:33:08

标签: security thymeleaf logout

嗨,我使用springboot和一个有效的自定义登录表单。 当我创建注销表单时:

<form th:action="@{/logout}" method="post">
    <input type="submit" value="Log out"/>
</form>

一切正常。

但是,如果我添加一个简单的链接:

<div class="navbar-header">
    <a class="navbar-brand" th:href="@{'/logout'}" th:text="#{menu.common.logout}"></a>
</div>  

应用程序显示一般错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jul 30 14:21:16 CEST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

我不明白问题所在。表单和href都指向“ / logout”,但只有第一个起作用。

出于安全考虑,我有此配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                //.logoutSuccessUrl("/")
                .permitAll()
                .and()
            .rememberMe()
                .tokenValiditySeconds(1800) //1800 sec=30 min
                .key("authKey")
                .userDetailsService(userDetailsService)
            ;
    }

1 个答案:

答案 0 :(得分:1)

在spring安全文档中查看this注释。如果要使用get请求注销用户,则需要在.logout()之后调用logoutRequestMatcher,并带有用户要注销的路径,在本例中为“ / logout”。

相关问题