[英]Spring Security OAuth2 check_token endpoint
我正在尝试设置一个资源服务器以使用 spring security oauth 与单独的授权服务器一起工作。 我使用RemoteTokenServices
需要/check_token
端点。
我可以看到/oauth/check_token
端点在使用@EnableAuthorizationServer
时默认启用。 但是,默认情况下无法访问端点。
是否应该手动添加以下条目以将此端点列入白名单?
http.authorizeRequests().antMatchers("/oauth/check_token").permitAll();
这将使所有人都可以访问此端点,这是所需的行为吗? 或者我错过了什么。
提前致谢,
你必须
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception
{
oauthServer.checkTokenAccess("permitAll()");
}
有关这方面的更多信息 ::
只是为了澄清几点,并在Pratik Shah (以及相关主题中的Alex )提供的答案中添加更多信息:
1- 通过创建一个扩展AuthorizationServerConfigurerAdapter
的类来覆盖提到的configure
方法:
@EnableAuthorizationServer
@Configuration
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("ger-client-id")
.secret("ger-secret")
.authorizedGrantTypes("password")
.scopes("read", "write");
}
}
2- 当我们包含@EnableAuthorizationServer
注释(包括AuthorizationServerConfigurer
bean)时,我建议阅读这篇 Spring 指南,解释 Spring Boot 执行的自动配置。 如果您像我上面那样创建一个扩展AuthorizationServerConfigurerAdapter
的配置 bean,那么整个自动配置将被禁用。
3- 如果自动配置非常适合您,并且您只想操作对/oauth/check_token
端点的访问,您仍然可以在不创建AuthorizationServerConfigurer
bean 的情况下执行此操作(因此无需以编程方式配置所有内容)。
您必须将security.oauth2.authorization.check-token-access
属性添加到application.properties
文件中,例如:
security.oauth2.client.client-id=ger-client-id
security.oauth2.client.client-secret=ger-secret
security.oauth2.client.scope=read,write
security.oauth2.authorization.check-token-access=permitAll()
当然,如果你愿意,你可以给它一个isAuthenticated()
值。
您可以将日志级别设置为 DEBUG 以检查所有内容是否按预期配置:
16:16:42.763 [main] DEBUG o.s.s.w.a.e.ExpressionBasedFilterInvocationSecurityMetadataSource - Adding web access control expression 'permitAll()', for Ant [pattern='/oauth/check_token']
没有太多关于这些属性的文档,但您可以从这个自动配置类中找出它们。
最后值得一提的是,虽然在最新的 Spring 版本中似乎已经修复了,但我只是在spring-security-oauth项目中提交了一个问题; 如果您向请求添加尾部斜杠,则似乎默认启用了 token_check 功能:
$ curl localhost:8080/oauth/check_token/?token=fc9e4ad4-d6e8-4f57-b67e-c0285dcdeb58
{"scope":["read","write"],"active":true,"exp":1544940147,"authorities":["ROLE_USER"],"client_id":"ger-client-id"}
POST参数有3个,分别是client_id(用户名)、client_secret(用户名对应的密码)、token(申请的令牌)、client_id、client_secret与/oauth/token接口中的参数不同
首先,配置令牌访问表达式:
@Override
public void configure(AuthorizationServerSecurityConfigurer securityConfigurer) throws Exception {
securityConfigurer
.allowFormAuthenticationForClients()
.checkTokenAccess("isAuthenticated()")
.addTokenEndpointAuthenticationFilter(checkTokenEndpointFilter());
}
然后,我们需要定义一个过滤器来处理客户端认证:
@Bean
public ClientCredentialsTokenEndpointFilter checkTokenEndpointFilter() {
ClientCredentialsTokenEndpointFilter filter = new ClientCredentialsTokenEndpointFilter("/oauth/check_token");
filter.setAuthenticationManager(authenticationManager);
filter.setAllowOnlyPost(true);
return filter;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.