2017-07-18 11 views
1

Spring OAuth2 with JdbcTokenStore - 以下のスニペットでコードされたカスタムログインページが使用されています。JdbcTokenStoreを使用してSpring OAuth2でログアウトできません

オンラインのさまざまなリソースからhere Spring Securityはユーザをログアウトするためにエンドポイント/ログアウトが組み込まれているようですが、それは私にとってはうまくいかないようです。そのエンドポイントにヒットすると、カスタムログインページにリダイレクトされますが、これは正常ですが一貫性がありません。複数のタブを使って作業することは時々ではありませんが、毎回ではありません。また、Springによって作成されたクッキーもクリアされていないことに気付きました。

WebSecurityConfigurerAdapterが次のように定義されていますか。

@Configuration 
@Order(-20) 
protected static class LoginConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
      .formLogin() 
      .loginPage("/login") 
      .permitAll() 
      .defaultSuccessUrl("/homepage", false) 
      .failureUrl("/login?error=true")  
     .and() 
      .requestMatchers().antMatchers("/login", "/homepage", "/login?error=true", "/oauth/authorize", "/oauth/confirm_access") 
     .and() 
      .authorizeRequests().anyRequest().authenticated(); 
     // @formatter:on 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.parentAuthenticationManager(authenticationManager); 
    } 
} 

組み込みログアウト機能が動作するようになると、データベースで作成されたトークンも削除するのが理想的です。いくつかの潜在的な回答を試みたonlineしかし、彼らは動作していません。すべてのポインタは深く感謝されるだろうか?

もっと分かりやすくするためにコードスニペットを投稿することができます。

答えて

0

最後に、これを動作させました。うまくいけば、これは同じボートの他の人にとって有益なことです。

以下のコードスニペットでは、セッション管理の設定を無視することができます。

@Configuration 
@Order(-20) 
protected static class LoginConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.NEVER) 
      .invalidSessionUrl("/login") 
      .sessionAuthenticationErrorUrl("/login") 
      .and() 
       .formLogin().loginPage("/login").permitAll().defaultSuccessUrl("/homepage", false) 
       .failureUrl("/login?error=true") 
      .and() 
       .requestMatchers() 
       .antMatchers("/", "/login", "/logout", "/homepage", "/login?error=true", "/oauth/authorize", "/oauth/confirm_access") 
      .and() 
       .logout().logoutUrl("/logout").logoutSuccessUrl("/login").invalidateHttpSession(true) 
       .permitAll() 
      .and().authorizeRequests() 
       .antMatchers("/login**") 
       .permitAll().anyRequest().authenticated(); 
     // @formatter:on 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.parentAuthenticationManager(authenticationManager); 
    } 
} 

と一緒にログアウトコントローラを作成する上でのトリックに

@Controller 
public class LogoutController { 

    @RequestMapping(value = "/logout", method = RequestMethod.GET) 
    public String logout(HttpServletRequest request, HttpServletResponse response, Model model) { 
     /* Getting session and then invalidating it */ 
     HttpSession session = request.getSession(); 
     if (session != null) { 
      session.invalidate(); 
     } 
     HandleLogOutResponse(response, request); 
     return "logout"; 
    } 

    private void HandleLogOutResponse(HttpServletResponse response, HttpServletRequest request) { 
     Cookie[] cookies = request.getCookies(); 
     for (Cookie cookie : cookies) { 
      cookie.setMaxAge(0); 
      cookie.setValue(null); 
      cookie.setPath("/"); 
      response.addCookie(cookie); 
     } 
    } 

を行いますそして、あなたは次のように単純な関数を使用してビューを登録することができ

public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addViewController("/").setViewName("login"); 
    registry.addViewController("/login").setViewName("login"); 
    registry.addViewController("/homepage").setViewName("homepage"); 
    registry.addViewController("/logout").setViewName("logout"); 
} 
以下
関連する問題