2017-04-24 2 views
-1

이것은 스프링 부트 및 스프링 보안을 사용하는 제 코드입니다. 문제는 내가 로그 아웃 (Thyemleaf 사용)을 사용하여 로그 아웃이 작동하지 않는 경우입니다. Thyemleaf를 사용스프링 부트 및 스프링 보안에서 로그 아웃이 작동하지 않습니다.

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

링크 :

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

모든 로그 아웃을 제외하고 작동 GET 요청을 사용하여 사용자가 로그 아웃 수있는이 방식을 변경하여 비 POST 로그 아웃을 지원할 수있다, 내 말은 세션이 만료되지 않을 때 로그 아웃 링크를 클릭하십시오. 예를 들어 저는 사용자이고 가입 (로그인)이므로 로그 아웃합니다. 사용자 페이지에 대한 액세스 권한을 여전히 얻습니다. –

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

봄 보안 로그 아웃 URL은 POST 전용입니다. 당신은 자바 구성

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

당신이

<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 –

관련 문제