20

我正在尝试使用 spring security oauth 设置资源服务器以与单独的授权服务器一起使用。我正在使用RemoteTokenServices需要/check_token端点。

我可以看到/oauth/check_token端点在使用时默认启用@EnableAuthorizationServer。但是,默认情况下无法访问端点。

是否应该手动添加以下条目以将此端点列入白名单?

http.authorizeRequests().antMatchers("/oauth/check_token").permitAll();

这将使所有人都可以访问此端点,这是所需的行为吗?或者我错过了什么。

提前致谢,

4

4 回答 4

20

你必须

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception
{
   oauthServer.checkTokenAccess("permitAll()");    
}

有关此的更多信息::

如何使用远程令牌服务?

于 2014-11-06T07:21:22.073 回答
9

只是为了澄清几点,并在Pratik Shah(以及Alex在相关线程中)提供的答案中添加更多信息:

1-configure通过创建扩展的类来覆盖提到的方法AuthorizationServerConfigurerAdapter

    @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-我建议阅读这个 Spring 指南@EnableAuthorizationServer,解释当我们包含注解时 Spring Boot 执行的自动配置,包括一个AuthorizationServerConfigurerbean。如果您AuthorizationServerConfigurerAdapter像我在上面所做的那样创建一个扩展 bean 的配置 bean,那么整个自动配置将被禁用。

3- 如果自动配置非常适合您,并且您只想操作对/oauth/check_token端点的访问,您仍然可以在不创建AuthorizationServerConfigurerbean 的情况下这样做(因此不必以编程方式配置所有内容)。

您必须将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"}
于 2018-12-15T18:32:19.303 回答
2

POST参数有3个,分别是client_id(用户名)、client_secret(用户名对应的密码)、token(申请的令牌)、client_id、client_secret与/oauth/token接口中的参数不同

在此处输入图像描述

于 2020-09-25T17:22:25.147 回答
1

首先,配置令牌访问表达式:

@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;
}
于 2016-04-23T03:04:40.263 回答