Spring OAuth2 -当将值赋值给user-name-attribute时,它会给出错误“Missing attribute 'name' in attributes”

gg58donl  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(155)

下面是我的application.yml文件
spring:security:oauth2:

client:
    registration:
      callback:
        client-id: *******
        client-secret: ******
        client-name: Auth Server
        scope: openid, profile
        provider: custom-provider
        redirect-uri: http://*******/v1/oauth/callback
        client-authentication-method: basic
        authorization-grant-type: authorization_code
    provider:
      custom-provider:
        token-uri: https://*******oauth/nam/token
        jwk-set-uri: https://*******/oauth/nam/keys
        authorization-uri: https://*******/oauth/nam/authz
        user-info-uri: https://*******/oauth/nam/userinfo
        user-name-attribute: name

字符串

下面是配置文件

@Override public void configure(HttpSecurity httpSecurity)throws Exception {

httpSecurity.authorizeRequests()
    .anyRequest().authenticated()
    .and()
    .oauth2Login().redirectionEndpoint()
    .baseUri("/v1/oauth/callback");
    
}

下面是控制器文件

@Autowired OAuth2AuthorizedClientService oauth2ClientService;

@GetMapping("/v1/oauth/callback")
public String getLoginInfo(Model model, OAuth2AuthenticationToken authentication) {

    OAuth2AuthorizedClient client = oauth2ClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());

    String userInfoEndpointUri = client.getClientRegistration()
        .getProviderDetails()
        .getUserInfoEndpoint()
        .getUri();

    if (!StringUtils.isEmpty(userInfoEndpointUri)) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + client.getAccessToken()
            .getTokenValue());

        HttpEntity<String> entity = new HttpEntity<String>("", headers);

        ResponseEntity<Map> response = restTemplate.exchange(userInfoEndpointUri, HttpMethod.GET, entity, Map.class);
        Map userAttributes = response.getBody();
        model.addAttribute("name", userAttributes.get("name"));
    }

    return "loginSuccess";

获取下方错误

2021-01-26T10:33:23.611837592Z:[INFO] java.lang.IllegalArgumentException:属性中缺少属性“name”2021-01- 26 T10:33:23.611842592Z:[INFO] at org.springframework.security.oauth2.core.user.DefaultOAuth2User.(DefaultOAuth2User.java:72)~[spring-security-oauth2-core-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611856692Z:[INFO] at org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService.loadUser(DefaultOAuth2UserService.java:116)~[spring-security-oauth2-client-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611861892Z:[INFO] at org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService.loadUser(OidcUserService.java:109)~[spring-security-oauth2-client-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611883692Z:[INFO] at org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService.loadUser(OidcUserService.java:66)~[spring-security-oauth2-client-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611888592Z:[INFO] at org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizationCodeAuthenticationProvider.authenticate(OidcAuthorizationCodeAuthenticationProvider.java:156)~[spring-security-oauth2-client-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611893092Z:[INFO] at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182)~[spring-security-core-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611897292Z:[INFO] at org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter.attemptAuthentication(OAuth2LoginAuthenticationFilter.java:192)~[spring-security-oauth2-client-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611901792Z:[INFO] at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:222)~[spring-security-web-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611905992Z:[INFO] at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)~[spring-security-web-5.4.2.jar:5.4.2] 2021-01-26T10:33:23.611910392Z:[INFO] at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)~[spring-security-web-5.4.2.jar:5.4.2]

8wigbo56

8wigbo561#

一切都很简单:Oauth提供程序对您的服务有一个响应,该响应具有提供程序特定的架构。例如,Github有login字段,而Google没有:

15:50:21.459 [reactor-http-nio-1] DEBUG o.s.s.w.s.c.WebSessionServerSecurityContextRepository - Found SecurityContext 'SecurityContextImpl [Authentication=OAuth2AuthenticationToken [Principal=Name: [Andrey Kuleshov], Granted Authorities: [[ROLE_USER, SCOPE_https://www.googleapis.com/auth/userinfo.email, SCOPE_https://www.googleapis.com/auth/userinfo.profile, SCOPE_openid]], User Attributes: [{email_verified=true, iss=https://accounts.google.com, given_name=Andrey, locale=ru, name=Andrey Kuleshov, exp=2023-07-13T13:50:17Z, family_name=Kuleshov, ...],

字符串
因此,使用Spring Security,您可以显式设置user-name-attribute将从中获取的字段的名称:

github:
    user-name-attribute: login


在您的案例中,提供商的响应中缺少name字段。查看用户信息详情。

相关问题