2017-04-24 55 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 –

相關問題