当前位置: 首页 > 知识库问答 >
问题:

Spring oauth2服务器,无法验证令牌

巫马英豪
2023-03-14

我从spring boot oauth2服务器生成JWT令牌,但当我想使用'http://auth_server/oauth/check_token'endpoint验证此JWT令牌时,我将post请求发送到此endpoint,并将JWT包含在授权头中,得到401-未经授权的http错误代码。

我从服务器得到的结果是:

{
    "timestamp": "2020-03-19T16:28:39.999+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/oauth/check_token"
}

为什么oauth服务器返回401?

下面是我对oauth服务器的实现:

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

        @Autowired
        @Qualifier("authenticationManagerBean")
        private AuthenticationManager authenticationManager;

        @Autowired
        private ClientDetailsServiceImpl clientDetailsService;

        private String privateKey = "private key";

        @Value("${jwt.accessTokenValidititySeconds:43200}") // 12 hours
        private int accessTokenValiditySeconds;

        @Value("${jwt.authorizedGrantTypes: authorization_code}")
        private String[] authorizedGrantTypes;

        @Value("${jwt.refreshTokenValiditySeconds:2592000}") // 30 days
        private int refreshTokenValiditySeconds;

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.withClientDetails(clientDetailsService);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {      
            TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
            tokenEnhancerChain.setTokenEnhancers(
              Arrays.asList(tokenEnhancer(), accessTokenConverter()));

            endpoints.tokenStore(tokenStore())
            .accessTokenConverter(accessTokenConverter())
            .authenticationManager(authenticationManager);
        }

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


        @Bean
        public JwtAccessTokenConverter accessTokenConverter() {
           JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
           converter.setSigningKey(privateKey);
           return converter;
        }


        @Bean
        public TokenStore tokenStore() {
            return new JwtTokenStore(accessTokenConverter());
        }

        @Bean
        public TokenEnhancer tokenEnhancer() {
            return new CustomTokenEnhancer();
        }

        @Bean
        PasswordEncoder passwordEncoder() {
            return PasswordEncoderFactories.createDelegatingPasswordEncoder();
        }

    }

共有1个答案

薄烨
2023-03-14

最后,我发现get请求应该发送到http://server.com/oauth/check_token?token=[access_token],在授权头中使用客户端名称密码来检查访问令牌的有效性。下面是邮递员的例子:

 类似资料:
  • 我正在使用asp。net核心与openiddict,为了授权,我使用jwtmiddleware 但由于某些原因,它抛出了这个错误,任何帮助都将不胜感激。 info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[7] Bearer 未经过身份验证.失败消息:IDX10501:签名验证失败。无法匹配“孩子”: “7

  • 出身背景我需要让我的网站通过IdentityServer(IDS)进行身份验证。“example.com” 我正在用DotNetCore构建我所有的网站,用apache在代理服务器上托管它们,让我们调用私有ip12.3.4.5。 它们应该如何工作。我去网站的例子。我应该可以和ids通话。例如。com获取身份验证信息,然后重新路由回示例。具有身份验证令牌的com。相反,我得到了一个SSL握手错误。见

  • 问题内容: 我获得了用于Web推送的Firebase Cloud Messaging注册令牌。然后,我将此邮件发送到服务器以保存在数据库中,以供以后推送。但是,如何验证此令牌有效或伪造的? 我已经尝试过了,但是我认为这是针对Auth令牌而不是针对Web推送的。 其他人可以将随机伪造令牌的请求发送到我的服务器。我想防止这种情况,然后再保存到数据库中。 编辑:已解决,我编写了一个简单的类来使用FCM快

  • 问题内容: 我编写了以下bean来验证我的邮件。 问题在于smtp设置,当我在本地服务器上运行应用程序时,它运行正常。但是当我在openshift服务器上运行应用程序时,在 有人可以指出以上设置中的问题,为什么它们在我的本地计算机上工作? 以下是我得到的例外 问题答案: 我有同样的问题。尝试从浏览器登录到Gmail帐户。如果Gmail认为是非法访问(可能来自其他地理位置),则会阻止身份验证请求。您

  • 我浏览了互联网,还没有找到一个解决方案或方法来验证证书时,通过HTTPS连接使用TIDWTTP。 我已经连接了一个idsslohandlersocketopenssl组件作为IOHandler,设置SSLModes,等等https://s3.amazonaws.com它无法验证证书。 OpenSSL(Indy)提供 “连接SSL时出错。SSL3\u获取\u服务器\u证书:证书验证失败” OpenS

  • 在任何给定的POD中,服务帐户令牌被挂载在位置。 /var/run/secrets/kubernetes.io/serviceaccount/token 我想了解这个令牌是如何签名的。Kubernetes在签名此令牌时使用什么密钥?如何访问有助于脱机签名验证的公钥 /var/run/secrets/kubernetes.io/serviceaccount/ca.crt-我尝试使用与此证书关联的公钥