【问题标题】:JWT Bearer Keeps returning 401 Status - Bearer error="invalid_token", error_description="The iss claim is not valid"JWT Bearer 不断返回 401 状态 - Bearer error="invalid_token", error_description="The iss claim is not valid"
【发布时间】:2022-07-12 14:31:01
【问题描述】:

我正在使用 postman 和 spring boot,我使用 Keycloak 进行身份验证服务。我通过邮递员向 Keycloak 服务器发出请求,该服务器返回给我一个 Bearear 令牌,然后我将其发送到 Spring 服务器进行身份验证,但 Spring 回复说令牌的 iss 声明无效。

这是我的代码

类配置:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .antMatchers("/genere/**").permitAll()
                .antMatchers("/valutazione/**").permitAll()
                .antMatchers("/users/**").permitAll()
                .antMatchers("/carrello/**").permitAll()
                .antMatchers("/film/**").permitAll()
                .anyRequest().authenticated().and().oauth2ResourceServer().jwt();
    }

类休息控制器:

@RestController
public class HomeController {
    @GetMapping("/")
    @PreAuthorize("hasAuthority('user')")
    public String home(@RequestParam(value="someValue") int value){
        return "Welcome,"+ Util.getEmail()+" "+value+" !";
    }
}

应用程序.yaml

keycloak:
  realm: demo
  resource: spa-demo
  auth-server-url: http://localhost:8080/realms/demo/account
spring:
  security:
    oauth2:
      resourceserver:
                jwt:
                  jwk-set-uri: http://localhost:8080/realms/demo/protocol/openid-connect/certs
                  issuer-uri: http://localhost:8080/realms/demo/

我在开发模式下使用命令 kc.bat start-dev 启动 keyclaok。

【问题讨论】:

    标签: spring spring-boot authentication jwt keycloak


    【解决方案1】:

    Spring conf 和 iss 声明必须完全匹配。如果有的话,甚至是尾随斜杠。

    将您从 Keycloak 获得的授权令牌之一放入 https://jwt.io 之类的工具中,并将 iss 声明的值复制到您的 spring 配置中。

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2023-01-27
      • 2018-08-03
      • 2020-06-03
      • 2022-10-25
      • 2021-09-06
      • 2020-12-24
      • 2021-02-06
      • 2020-06-04
      相关资源
      最近更新 更多