2017-07-18 165 views
1

带有JdbcTokenStore的Spring OAuth2 - 已使用自定义登录页面,如以下代码片段中所示。无法使用JdbcTokenStore注销Spring OAuth2

来自不同的在线资源here Spring安全似乎有一个内置的终端/注销登录用户,但这似乎并不适用于我。当我点击该端点时,它会重定向回自定义登录页面,这很好,但不一致。使用多个选项卡,有时可以但不是每次都有效。还注意到Spring创建的cookie还没有被清除。

是否有问题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"); 
}