下面是我的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]
1条答案
按热度按时间8wigbo561#
一切都很简单:Oauth提供程序对您的服务有一个响应,该响应具有提供程序特定的架构。例如,Github有
login
字段,而Google没有:字符串
因此,使用Spring Security,您可以显式设置
user-name-attribute
将从中获取的字段的名称:型
在您的案例中,提供商的响应中缺少
name
字段。查看用户信息详情。