Spring Boot Oauth2注销端点

问题描述:

我有一个Spring Boot REST应用程序分为资源服务器和Auth服务器 - 受无状态Oauth2安全保护。Spring Boot Oauth2注销端点

我使用弹簧安全的oauth2首发:

 <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.security.oauth</groupId> 
      <artifactId>spring-security-oauth2</artifactId> 
     </dependency> 

资源服务器只是链接到使用此行的身份验证服务器在我application.properties

security.oauth2.resource.userInfoUri: http://localhost:9000/my-auth-server/user 

中的验证服务器存储使用的凭据一个数据库并有如下配置:

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    @Qualifier("userDetailsService") 
    private UserDetailsService userDetailsService; 

    @Autowired 
    private AuthenticationManager authenticationManager; 

    @Value("${gigsterous.oauth.tokenTimeout:3600}") 
    private int expiration; 

    // password encryptor 
    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception { 
     configurer.authenticationManager(authenticationManager); 
     configurer.userDetailsService(userDetailsService); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory().withClient("gigsterous").secret("secret").accessTokenValiditySeconds(expiration) 
       .scopes("read", "write").authorizedGrantTypes("password", "refresh_token").resourceIds("resource"); 
    } 

} 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    /** 
    * Constructor disables the default security settings 
    */ 
    public WebSecurityConfig() { 
     super(true); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/login"); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

} 

一切正常,我可以得到一个访问令牌,并用它来从我的资源服务器得到一个受保护的资源:

curl -X POST --user 'my-client-id:my-client-secret' -d 'grant_type=password&[email protected]&password=password' http://localhost:9000/my-auth-server/oauth/token 

不过,我想不通,怎么处理注销(一旦用户决定注销,则令牌无效)。我认为会提供一些端点来使令牌无效,或者我是否必须创建自己的端点来处理它?我不需要指定任何类型的TokenStore bean,所以我不知道如何使当前令牌无效。对于任何见解,我都会很高兴 - 我发现的大多数教程都解释了如何使用会话或JWT令牌处理这些问题。

我有这个问题,并在this post上发布了解决方案。

从客户端应用程序注销后,它基本上重定向到授权服务器上的端点,然后以编程方式注销到那里。

+0

哇,这看起来非常整齐。我会尝试将它实现到我的应用程序中,并在我得到它的时候接受这个答案。非常感谢你! – Smajl