使用 Spring Boot 使用 OpenID Provider 对 Web 应用程序进行身份验证时出错

时间:2021-08-01 17:24:19

标签: spring-boot spring-security oauth-2.0 oauth openid-connect

我正在尝试使用 Spring Boot 2 使用第三方配置的 Open ID Connect 提供程序对 Web 应用程序进行身份验证。 输入登录凭据并提交后,在代码交换期间,我收到此错误

org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse] and content type [text/html]

我有点卡在这一点上。我正在关注此博客enter link description here 下面是yaml和pom文件。

应用程序.yml


    logging: 
      level: 
        org.springframework.security: DEBUG
    spring: 
      security: 
        oauth2: 
          client: 
            provider: 
              abc: 
                authorization-uri: https://xyz/oauth/authorize
                token-uri: https://xyz/oauth/token
                user-info-uri: https://xyz/oauth/userinfo
                user-name-attribute: sub
                jwk-set-uri: https://xyz/oauth/jwks/jwk-url
            registration: 
              abc: 
                authorization-grant-type: authorization_code
                client-id: ****
                client-secret: ****
                scope: openid,profile,email
                redirect-uri: https://xyz/login/oauth2/code/abc

pom.xml

    

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>

redirect-uri 已在服务器上注册并且工作正常,一旦我提交凭据,我就会收到代码作为响应。 交换代码时,请求处理失败。 如果您能帮助我解决此错误,我将不胜感激。

这是我的配置文件:

@Configuration
@EnableWebSecurity
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests().antMatchers("/oauth2/**","/login/**").permitAll().anyRequest()
                .authenticated().and().oauth2Login()

    }
}

控制器

public class SwController {
    
    final Logger logger = LoggerFactory.getLogger(SwController.class);
    
    @GetMapping("/user/oidc-principal")
    @CrossOrigin
    public OidcUser user(@AuthenticationPrincipal OidcUser principal) { 
        return principal; 
    }
}

1 个答案:

答案 0 :(得分:0)

其实这里的问题不在于Spring Boot代码,而在于服务本身。第三方服务提供商的域出现问题,导致返回 [text/html] 页面。由于 Spring 期望来自该服务的 application/json 响应,因此无法找到合适的 httpmessageconverter 来处理此请求。