【发布时间】:2017-03-26 13:50:48
【问题描述】:
我有一个 Spring Boot REST 应用程序,分为资源服务器和身份验证服务器 - 受无状态 Oauth2 安全性保护。
我正在使用 Spring Security 和 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&username=peter@hotmail.com&password=password' http://localhost:9000/my-auth-server/oauth/token
但是,我不知道如何处理注销(一旦用户决定注销,令牌就会失效)。我假设会提供一些端点来使令牌无效,还是我必须创建自己的端点来处理它?我不需要指定任何类型的 TokenStore bean,所以我不确定如何使当前令牌无效。如果有任何见解,我会很高兴 - 我发现的大多数教程都解释了如何使用会话或 JWT 令牌进行处理。
【问题讨论】:
标签: java spring-boot oauth-2.0 spring-security-oauth2