简体   繁体   English

从 Keycloak 验证令牌

[英]Verify Token from Keycloak

I have a token which will be passed from a service to my Rest service.我有一个令牌,它将从服务传递到我的 Rest 服务。 I want to use Spring security to validate the token against the public key (this is hosted on a different server).我想使用 Spring 安全性来根据公钥验证令牌(这托管在不同的服务器上)。 I have defined the Security Config class which is extending KeycloakWebSecurityConfigurerAdapter and i have implemented the httpSecurity and AuthenticationManager Builder.我已经定义了扩展 KeycloakWebSecurityConfigurerAdapter 的安全配置 class 并且我已经实现了 httpSecurity 和 AuthenticationManager Builder。

I am using the OncePerRequestFilter and getting the Bearer token from the request.我正在使用 OncePerRequestFilter 并从请求中获取 Bearer 令牌。

@Component public class JwtTokenAuthenticationFilter extends OncePerRequestFilter { @Component public class JwtTokenAuthenticationFilter 扩展了 OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {

    final String authorizationHeader = request.getHeader("Authorization");


    String token = null;

    if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
        token = authorizationHeader.substring(7);

    }

} }

What is the method to validate this token using Public Key with Keycloak classes?使用带有 Keycloak 类的公钥验证此令牌的方法是什么?

In order to configure your KeycloakWebSecurityConfigurerAdapter , I guess you're providing a keycloak.json file.为了配置您的KeycloakWebSecurityConfigurerAdapter ,我猜您提供了一个keycloak.json文件。 In this file , you are telling your application where your keycloak server authorization server url is: auth-server-url , optionally you could provide a realm-public-key as well ( this is not recommended ).此文件中,您将告诉您的应用程序您的 keycloak 服务器授权服务器 url 位于: auth-server-url ,您也可以选择提供一个realm-public-key不推荐这样做)。 This is all you need, the adapter will check for you that incoming tokens are signed by that server, for this realm.这就是您所需要的,适配器将为您检查传入的令牌是否由该服务器签名,对于这个 realm。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM