key store是否创建_避坑指南(八):Spring Cloud Oauth2 token_key端点404

问题

授权服务器基于Spring Cloud Oauth2创建后,配置TokenStore为JwtTokenStore,访问/oauth/token_key接口获取公钥,出现404错误。

授权服务器配置如下。

package com.luas.xmall.auth.configuration;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import org.springframework.http.HttpMethod;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.oauth2.config.annotation.builders.JdbcClientDetailsServiceBuilder;import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;import org.springframework.security.oauth2.provider.ClientDetails;import org.springframework.security.oauth2.provider.ClientDetailsService;import org.springframework.security.oauth2.provider.ClientRegistrationException;import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;import org.springframework.security.oauth2.provider.endpoint.TokenKeyEndpoint;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.oauth2.provider.token.store.*;import javax.sql.DataSource;import java.security.KeyPair;@Configuration@EnableAuthorizationServerpublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {    @Autowired    private PasswordEncoder passwordEncoder;    @Autowired    private UserDetailsService userDetailsService;    @Autowired    private AuthenticationManager authenticationManager;    @Autowired    private DataSource dataSource;    @Override    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {        security.                allowFormAuthenticationForClients()                .tokenKeyAccess("permitAll()")                .checkTokenAccess("permitAll()")        ;    }    @Override    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {        endpoints.authenticationManager(authenticationManager)                .userDetailsService(userDetailsService)                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)                .accessTokenConverter(jwtAccessTokenConverter())                .tokenStore(new JwtTokenStore(jwtAccessTokenConverter()));        ;    }    @Override    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {        // 配置方法1,只需配置DataSource即可,其它交给框架自动配置        clients.jdbc(dataSource).passwordEncoder(passwordEncoder);    }    private JwtAccessTokenConverter jwtAccessTokenConverter() {        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();        jwtAccessTokenConverter.setKeyPair(keyPair());        return jwtAccessTokenConverter;    }    private KeyPair keyPair() {        return new KeyStoreKeyFactory(new ClassPathResource("xxx.jks"), "123456".toCharArray()).getKeyPair("xxx", "123456".toCharArray());    }}

分析

先检查授权服务器配置,是否将该端点放开。本例配置中,此端点已为公共端点。

查看授权服务器端点配置类AuthorizationServerEndpointsConfiguration,发现其中只有授权端点、token端点、token校验端点的定义,并无token_key相关端点的定义。

查看token key端点类TokenKeyEndpoint,也并无异常。

4987d23bbe20c83a4d62fbe1c420a6a1.png

解决

基于以上分析,可以大胆猜测,加上端点404报错,应该是没有注册到容器中。那么我们仿照授权端点、token端点、token校验端点的定义,在授权服务器配置中,添加token key端点的定义。

@Beanpublic TokenKeyEndpoint tokenKeyEndpoint() {    return new TokenKeyEndpoint(jwtAccessTokenConverter());}

重启服务,再次访问端点,已可以正常展示公钥信息。

913b688dc3b1013f254e5b6eff812929.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 中使用 OAuth 2.0 进行 Token 认证的步骤如下: 1. 引入依赖:在项目的 `pom.xml` 文件中添加相应的依赖,包括 Spring Security OAuth2、Spring Web 等。 2. 配置认证服务器:创建一个认证服务器来颁发和验证访问令牌(Access Token)。可以使用 Spring Security OAuth2 提供的 `AuthorizationServerConfigurerAdapter` 类来配置认证服务器。 3. 配置资源服务器:创建一个资源服务器来保护你的资源,只有携带有效的访问令牌才能访问。可以使用 Spring Security OAuth2 提供的 `ResourceServerConfigurerAdapter` 类来配置资源服务器。 4. 定义用户信息服务:实现 `UserDetailsService` 接口来加载用户信息,包括用户名、密码和权限等。 5. 配置安全规则:在资源服务器中配置安全规则,定义哪些请求需要进行认证和授权。 6. 客户端注册:在认证服务器中注册客户端信息,包括客户端 ID、密钥和授权类型等。 7. 获取访问令牌:客户端可以通过认证服务器的授权端点进行授权,获取访问令牌。 8. 使用访问令牌:在请求资源服务器的受保护资源时,需要将访问令牌作为身份凭证传递给资源服务器。 这只是 OAuth 2.0 认证流程的基本步骤,具体的实现会因为使用的框架和需求而有所差异。你可以参考 Spring Security OAuth2 的官方文档和示例代码来了解更多详细信息。 另外,Spring Cloud 还提供了一些高级工具和组件,如 Spring Cloud Security、Spring Cloud OAuth2 Client 等,可以进一步简化和集成 OAuth 2.0 认证流程。你可以根据具体需求选择合适的组件来使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值