2017-04-24 52 views
-1

这是我使用Spring Boot和Spring Security的代码。问题是,当我用于注销(使用Thyemleaf)注销不适合我。注销不适用于Spring Boot和Spring Security

@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(); 
    } 
} 

链接使用Thyemleaf:

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

一切正常,除了注销注销用户支持非员额注销,我的意思是会话不会过期,当我点击注销链接,例如我是一个用户,我注册(登录),因此我注销我仍然可以访问用户页面 –

+0

请参阅我的答案http://stackoverflow.com/questions/40885178/logout-是 - 不工作,在弹簧的安全性。你必须使用HTTP'POST'而URL只是'/ logout'。 – dur

回答

-1

尝试,而不是执行以下操作:

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> 
+0

为什么downvote? – bphilipnyc

1

尝试做这样的事情。

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

春季安全登出网址仅限POST。你可以通过改变你的Java配置

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

这种方式,您可以使用GET请求

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

是啊,它的工作原理,我已经通过使用表单*做了这个解决方案,但对于第二次当我尝试它,我在文本编辑器(Eclipse)中得到一个错误.. 非常感谢@ahsan;) –

+0

这是一个记录解决方案,它应该工作。你能告诉我错误吗? http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf-logout –

相关问题