【问题标题】:Logout doesn't work with Spring Boot and Spring Security注销不适用于 Spring Boot 和 Spring Security
【发布时间】:2017-04-24 22:22:07
【问题描述】:

这是我使用 Spring Boot 和 Spring Security 的代码。问题是当我曾经注销(使用Thymleaf)时,注销对我不起作用。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private DataSource dataSource;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth
            .jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("select username as principal, password as credentials,active from users where username=?")
                .authoritiesByUsernameQuery("select username as principal,roles as role from users_roles where username=?")
                .rolePrefix("ROLE_")
                .passwordEncoder(new Md5PasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginPage("/login");
        http
            .authorizeRequests()
                .antMatchers("/index1").permitAll();
        http
            .authorizeRequests()
                .antMatchers("/user").hasRole("USER")
                .and()
            .logout();

        http
            .authorizeRequests()
                .antMatchers("/adpage").hasRole("ADMIN");
        http
            .exceptionHandling().accessDeniedPage("/403");
        http
            .logout().permitAll();
    }
}

使用 Thymleaf 链接:

<li><a th:href="@{/login?logout}">logout</a></li>

【问题讨论】:

  • 除注销外一切正常,我的意思是当我单击注销链接时会话不会过期,例如我是用户,我注册(登录),因此我注销我仍然得到访问用户页面
  • 查看我对stackoverflow.com/questions/40885178/… 的回答。您必须使用 HTTP POST,并且 URL 仅为 /logout

标签: spring spring-security thymeleaf logout


【解决方案1】:

尝试做这样的事情。

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

Spring 安全注销 URL 仅是 POST。您可以通过更改 Java 配置来支持非 POST 注销

protected void configure(HttpSecurity http) throws Exception {
  http
    // ...
    .logout()
       .logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}

这样您就可以使用 GET 请求注销用户

<li><a th:href="@{/logout}">logout</a></li>

【讨论】:

  • 是的,它有效,我已经使用表单*完成了这个解决方案,但是当我第二次尝试时,我在文本编辑器(Eclipse)中出现错误。非常感谢@ahsan; )
  • 这是一个记录在案的解决方案,它应该可以工作。你能告诉我错误吗? docs.spring.io/spring-security/site/docs/current/reference/…
【解决方案2】:

请尝试以下方法:

    http
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?login_error=true")
            .loginProcessingUrl("/j_spring_security_check") //if needed
            .and()
                .authorizeRequests()
                .antMatchers("/index1").permitAll()
                .antMatchers("/user").hasRole("USER")
                .antMatchers("/adpage").hasRole("ADMIN")
            .and()
                .exceptionHandling().accessDeniedPage("/403")
            .and()
                .logout()
                .logoutSuccessUrl("/index") //or whatever page you want
                .logoutUrl("/logout") //thinking this is what you need
                .permitAll();

你的链接是:

<li><a th:href="@{/logout}">logout</a></li>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2017-06-09
    • 2020-04-02
    • 2014-08-31
    • 2018-03-18
    相关资源
    最近更新 更多