我将Spring Boot REST应用程序分为资源服务器和Auth服务器 - 受无状态Oauth2安全保护。
我正在使用spring security和Oauth2 starters:
<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
中的这一行链接到auth服务器:
security.oauth2.resource.userInfoUri: http://localhost:9000/my-auth-server/user
auth服务器在数据库中存储使用凭据,并具有以下配置:
@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&username=peter@hotmail.com&password=password' http://localhost:9000/my-auth-server/oauth/token
但是,我无法弄清楚,如何处理注销(一旦用户决定注销就使令牌无效)。我假设会提供一些端点来使令牌无效,或者我是否必须创建自己的端点来处理它?我不需要指定任何类型的TokenStore bean,因此我不确定如何使当前令牌无效。我会很高兴有任何见解 - 我发现的大多数教程都解释了如何使用会话或JWT令牌来处理它。