spring security oauth client登录如何获取用户的详细信息?

重新开放
匿名用户 提问于 2018-01-02

做一个小项目想实现GitHub第三方登录,看了[spring security oauth官方文档教程](https://spring.io/guides/tutorials/spring-boot-oauth2/)但是还有些懵逼[捂脸]。
首先我在GitHub上注册了应用的名称,然后拿到id和key,在application.yml中配置好了相关的配置信息:
“`yml
security:
oauth2:
client:
clientId: bd1c0a783ccdd1c9b9e4
clientSecret: 1a9030fbca47a5b2c28e92f19050bb77824b5ad1
accessTokenUri: https://github.com/login/oauth/access_token
userAuthorizationUri: https://github.com/login/oauth/authorize
clientAuthenticationScheme: form
resource:
userInfoUri: https://api.github.com/user
“`
userInfoUri中指定了用户信息的获取路径,spirng security在登录成功后会将这个这个用户名注入到principal中,所以可以得到用户名:
“`java
@RequestMapping(“/user”)
public Object user(Principal principal) {
String name = principal.getUsername();

}
“`
请问想得到除了用户名之外比如头像应该如何实现呢?完整的实例代码如下,访问`http://localhost:8080/login/github`即可跳转登录,但是我想得到更加详细的用户信息。
“`java
@SpringBootApplication
@RestController
@EnableOAuth2Client
@EnableAuthorizationServer
@Order(6)
public class SocialApplication extends WebSecurityConfigurerAdapter {
@Autowired
OAuth2ClientContext oauth2ClientContext;
@RequestMapping({ “/user”, “/me” })
public Map<String, String> user(Principal principal) {
Map<String, String> map = new LinkedHashMap<>();
map.put(“name”, principal.getName());
return map;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher(“/**”).authorizeRequests().antMatchers(“/”, “/login**”, “/webjars/**”).permitAll().anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint(“/”))
.and()
.logout().logoutSuccessUrl(“/”).permitAll()
.and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
// @formatter:on
}
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher(“/me”).authorizeRequests().anyRequest().authenticated();
// @formatter:on
}
}
public static void main(String[] args) {
SpringApplication.run(SocialApplication.class, args);
}
@Bean
public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(filter);
registration.setOrder(-100);
return registration;
}
@Bean
@ConfigurationProperties(“github”)
public ClientResources github() {
return new ClientResources();
}
private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();
filters.add(ssoFilter(github(), “/login/github”));
filter.setFilters(filters);
return filter;
}
private Filter ssoFilter(ClientResources client, String path) {
OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path);
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),
client.getClient().getClientId());
tokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
return oAuth2ClientAuthenticationFilter;
}
}
class ClientResources {
@NestedConfigurationProperty
private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();
@NestedConfigurationProperty
private ResourceServerProperties resource = new ResourceServerProperties();
public AuthorizationCodeResourceDetails getClient() {
return client;
}
public ResourceServerProperties getResource() {
return resource;
}
}
“`
在网上搜了一下没有找到太多的和spirng security oauth2 client相关的教程,各位网友推荐一些好的blog么@_@
p.s: 官方demo下载地址 => https://github.com/spring-guides/tut-spring-boot-oauth2.git

1 答案

发布
andaily 管理员 2018-01-24

这个看提供的API啊. 一般是获取到  access_token 后去调用 用户信息的API 获取信息;
各个平台提供的API有所不同

#1

请登录或者 注册 来提交答案