0

I'm using spring security oauth2 and authorization_code type.

Oauth2 client succefully retrieved access_token for a user.

Now, the client needs to retrieve user info such as email using the access_token.

However I'm not able to find tutorial, example to illustrate how I can do that..

eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

0

You have two choices.

  1. Add the user info in the token response by adding the extra claims, see can I include user information while issuing an access token?
  2. Implement an userinfo endpoint

Such as

@RestController
@ProcessingController
@RequestMapping("/identity/userinfo")
public class UserInfoController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<?> userInfo(Principal principal,
                                      HttpServletRequest request) {
        return ResponseEntity.ok(principal);
    }

}

And make sure you have a resourceServer that is configured to use oauth2 at that endpoint

@Configuration
public class ResourceServerConfig {

    @Autowired
    private ResourceServerTokenServices defaultTokenServices;

    @Bean
    protected ResourceServerConfiguration identityResources() {

        ResourceServerConfiguration resource = new ResourceServerConfiguration() {
            // Switch off the Spring Boot @Autowired configurers
            public void setConfigurers(List<ResourceServerConfigurer> configurers) {
                super.setConfigurers(configurers);
            }
        };

        resource.setConfigurers(Arrays.asList(new ResourceServerConfigurerAdapter() {

            @Override
            public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
                resources.resourceId(ResourceId.IDENTITY_API.getName())
                        .tokenServices(defaultTokenServices);
            }

            @Override
            public void configure(HttpSecurity http) throws Exception {
                http
                        .requestMatchers()
                        .antMatchers("/identity/**")
                        .and()
                        .authorizeRequests()
                        .antMatchers("/identity/userinfo/**").hasAnyAuthority("ROLE_USER_INFO")
                        .anyRequest().authenticated();
            }
        }));
        resource.setOrder(3);
        return resource;
    }

}
Jun Huh
  • 242
  • 1
  • 8