2016-11-12 330 views
2

我有一個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令牌處理這些問題。

回答

1

我有這個問題,並在this post上發佈瞭解決方案。

從客戶端應用程序註銷後,它基本上重定向到授權服務器上的端點,然後以編程方式註銷到那裏。

+0

哇,這看起來非常整齊。我會嘗試將它實現到我的應用程序中,並在我得到它的時候接受這個答案。非常感謝你! – Smajl