我有一个Spring Boot REST应用程序,分为资源服务器和认证服务器-由无状态Oauth2安全保护。
我正在使用spring安全性和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令牌处理此问题。
我有这个问题,并在这个帖子上发布了一个解决方案。
它基本上是在从客户端应用程序注销后重定向到授权服务器上的端点,然后在那里以编程方式注销。