提问者:小点点

无法在Spring OAuth2中使用JdbcTokenStore注销


Spring OAuth2 with JdbcTokenStore-已使用自定义登录页面,如下面的代码片段所示。

从不同的在线资源,例如这里,Spring Security似乎有一个内置的endpoint /logout将用户注销,但这似乎对我不起作用。当我点击该endpoint时,它会重定向回自定义登录页面,这很好,但不一致。使用多个选项卡,它有时有效,但不是每次都有效。还注意到Spring创建的cookie也没有清除。

下面定义的WebSecurityConfigrerAdapter是否有问题?

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

一旦内置的注销功能开始工作,最好也删除数据库中创建的令牌。在线尝试了一些潜在的答案,但它们不起作用。任何建议都将不胜感激?

我可以发布更多的代码片段,如果它有助于提供更多的清晰度。


共1个答案

匿名用户

最后,我们做到了这一点——希望这对同一条船上的其他人有所帮助。

如果您在下面的代码片段中不需要会话管理配置,您可以忽略它。

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