spring security oauth2 github 用户存储
应用:将获取的三方用户信息进行存储
**********************
示例
******************
config 层
WebSecurityConfig:自定义Oauth2UserService,将用户信息输出到控制台
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource
private UserService userService;
@Bean
public PasswordEncoder initPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin().loginPage("/login/github").loginProcessingUrl("/login/form")
.and()
.authorizeRequests()
.antMatchers("/hello").hasAnyAuthority("ROLE_USER")
.antMatchers("/**").permitAll()
.and()
.logout().deleteCookies("JSESSIONID")
.logoutSuccessUrl("/").permitAll();
http.oauth2Login().loginPage("/login/github")
.userInfoEndpoint().userService(this.initOAuth2UserService());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(initPasswordEncoder());
}
private OAuth2UserService<OAuth2UserRequest, OAuth2User> initOAuth2UserService(){
OAuth2UserService<OAuth2UserRequest,OAuth2User> oAuth2UserService=new DefaultOAuth2UserService();
return oAuth2UserRequest -> {
OAuth2User oAuth2User=oAuth2UserService.loadUser(oAuth2UserRequest);
System.out.println("输出用户信息:\n"+oAuth2User);
return oAuth2User;
};
}
}
******************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(Principal principal){
System.out.println(principal.toString());
return "hello "+principal.getName();
}
@RequestMapping("/")
public String redirect(){
return "redirect";
}
}
**********************
使用测试
localhost:8080/hello,通过认证够控制台输出
输出用户信息:
Name: [41827785],
Granted Authorities: [[ROLE_USER, SCOPE_read:user]],
User Attributes: [{login=lihu12344, id=41827785, node_id=MDQ6VXNlcjQxODI3Nzg1, avatar_url=https://avatars3.githubusercontent.com/u/41827785?v=4, gravatar_id=, url=https://api.github.com/users/lihu12344, html_url=https://github.com/lihu12344, followers_url=https://api.github.com/users/lihu12344/followers, following_url=https://api.github.com/users/lihu12344/following{/other_user}, gists_url=https://api.github.com/users/lihu12344/gists{/gist_id}, starred_url=https://api.github.com/users/lihu12344/starred{/owner}{/repo}, subscriptions_url=https://api.github.com/users/lihu12344/subscriptions, organizations_url=https://api.github.com/users/lihu12344/orgs, repos_url=https://api.github.com/users/lihu12344/repos, events_url=https://api.github.com/users/lihu12344/events{/privacy}, received_events_url=https://api.github.com/users/lihu12344/received_events, type=User, site_admin=false, name=null, company=null, blog=, location=null, email=null, hireable=null, bio=null, public_repos=84, public_gists=0, followers=0, following=0, created_at=2018-07-28T11:56:10Z, updated_at=2020-05-16T01:32:56Z, private_gists=0, total_private_repos=0, owned_private_repos=0, disk_usage=4355, collaborators=0, two_factor_authentication=false, plan={name=free, space=976562499, collaborators=0, private_repos=10000}}]