- Shiro、Spring Security整合
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-O
- Spring Boot整合Spring Security简记-过
new無语 转载请注明原创出处,谢谢!
结合之前的简记,这回实现一个没有默认实现的第三方登陆,本篇的代码也放在码云上,pom依赖也不贴出了,就到git上进行查看吧。
首先注册获取,client-id
、client-secret
,与github差不多。不会申请的点击这里。
application.yml
spring:
security:
oauth2:
client:
registration:
gitee:
client-id: clientId
client-secret: clientSecret
client-name: gitee_login
scope: user_info
authorization-grant-type: authorization_code
redirect-uri-template: '{baseUrl}/login/oauth2/code/{registrationId}'
client-authentication-method: post
provider:
gitee:
authorization-uri: https://gitee.com/oauth/authorize
token-uri: https://gitee.com/oauth/token
user-info-uri: https://gitee.com/api/v5/user
user-name-attribute: id
本来按道理来说,这样配置就已经可以了,顶多OAuth2User
是默认的OAuth2默认的DefaultOAuth2User
,不能清晰的转换属性而已,但是我这边userInfo
端点一直请求401,一看就是Spring Security的默认请求方式可能有点问题,就又写了一个OAuth2UserService
,换了一种请求方式,果然好用了。下面给出代码:
CustomOAuth2UserService:
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
String uri = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri();
String tokenValue = userRequest.getAccessToken().getTokenValue();
uri = uri + "?access_token=" + tokenValue;
String result = HttpUtil.get(uri);
GiteeOAuth2User giteeOAuth2User = JSONObject.parseObject(result, GiteeOAuth2User.class);
return giteeOAuth2User;
}
GiteeOAuth2User就是把码云的userInfo
给定义了而已,给出部分代码:
private List<GrantedAuthority> authorities =
AuthorityUtils.createAuthorityList("ROLE_USER");
@JSONField(serialize = false)
private Map<String, Object> attributes;
private int id;
private String login;
private String name;
private String avatar_url;
private String url;
private String html_url;
private String followers_url;
private String following_url;
private String gists_url;
private String starred_url;
private String subscriptions_url;
private String organizations_url;
private String repos_url;
private String events_url;
private String received_events_url;
private String type;
private boolean site_admin;
private String blog;
private String weibo;
private String bio;
private int public_repos;
private int public_gists;
private int followers;
private int following;
private int stared;
private int watched;
private String created_at;
private String updated_at;
private String email;
private String unconfirmed_email;
private String phone;
private String private_token;
private int total_repos;
private int owned_repos;
private int total_private_repos;
private int owned_private_repos;
private int private_gists;
private String address;
配置:
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login()
.userInfoEndpoint()
.userService(new CustomOAuth2UserService());
最后,写一个UserController
来显示一下用户信息。
@RestController
public class UserController {
@GetMapping("/user")
public Object getUser() {
OAuth2AuthenticationToken authentication = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
OAuth2User principal = authentication.getPrincipal();
return principal.getAttributes();
}
}
之后启动程序,本地请求http://localhost:8080/user,就可以看到效果了。
网友评论