oauth2 token为空拦截_Spring Security OAuth2 token权限隔离

本文介绍了一个自定义的 `CustomOAuth2Authentication` 类,该类继承自 Spring Security 的 `AbstractAuthenticationToken`,用于在OAuth2中进行权限隔离。这个类在创建时会根据是否包含用户认证信息来设置权限,支持服务端token权限隔离。同时,提供了获取客户端认证请求、用户认证等方法,并重写了 equals 和 hashCode 方法以确保正确性。

1 packagecom.brightcns.wuxi.citizencard.auth.domain;2

3 importorg.springframework.security.authentication.AbstractAuthenticationToken;4 importorg.springframework.security.core.Authentication;5 importorg.springframework.security.core.CredentialsContainer;6 importorg.springframework.security.core.GrantedAuthority;7 importorg.springframework.security.oauth2.provider.OAuth2Request;8

9 importjava.util.Collection;10

11 /**

12 *@authormaxianming13 * @date 2018/10/29 13:5314 */

15 public class CustomOAuth2Authentication extendsAbstractAuthenticationToken {16

17 private static final long serialVersionUID = -4809832298438307309L;18

19 private finalOAuth2Request storedRequest;20

21 private finalAuthentication userAuthentication;22

23 /**

24 * Construct an OAuth 2 authentication. Since some grant types don't require user authentication, the user25 * authentication may be null.26 *@paramstoredRequest The authorization request (must not be null).27 *@paramuserAuthentication The user authentication (possibly null).28 */

29 public CustomOAuth2Authentication(OAuth2Request storedRequest, Authentication userAuthentication, Collection extends GrantedAuthority>authorities) {30 /**

31 * 为了服务端进行token权限隔离 {@link@PreAuthorize("hasAuthority('server')")},自定义OAuth2Authentication使得支持改变authorities32 */

33 super(authorities != null ? authorities : userAuthentication == null ?storedRequest.getAuthorities() : userAuthentication.getAuthorities());34 this.storedRequest =storedRequest;35 this.userAuthentication =userAuthentication;36 }37

38 publicObject getCredentials() {39 return "";40 }41

42 publicObject getPrincipal() {43 return this.userAuthentication == null ? this.storedRequest.getClientId() : this.userAuthentication44 .getPrincipal();45 }46

47 /**

48 * Convenience method to check if there is a user associated with this token, or just a client application.49 *50 *@returntrue if this token represents a client app not acting on behalf of a user51 */

52 public booleanisClientOnly() {53 return userAuthentication == null;54 }55

56 /**

57 * The authorization request containing details of the client application.58 *59 *@returnThe client authentication.60 */

61 publicOAuth2Request getOAuth2Request() {62 returnstoredRequest;63 }64

65 /**

66 * The user authentication.67 *68 *@returnThe user authentication.69 */

70 publicAuthentication getUserAuthentication() {71 returnuserAuthentication;72 }73

74 @Override75 public booleanisAuthenticated() {76 return this.storedRequest.isApproved()77 && (this.userAuthentication == null || this.userAuthentication.isAuthenticated());78 }79

80 @Override81 public voideraseCredentials() {82 super.eraseCredentials();83 if (this.userAuthentication != null && CredentialsContainer.class.isAssignableFrom(this.userAuthentication.getClass())) {84 CredentialsContainer.class.cast(this.userAuthentication).eraseCredentials();85 }86 }87

88 @Override89 public booleanequals(Object o) {90 if (this ==o) {91 return true;92 }93 if (!(o instanceofCustomOAuth2Authentication)) {94 return false;95 }96 if (!super.equals(o)) {97 return false;98 }99

100 CustomOAuth2Authentication that =(CustomOAuth2Authentication) o;101

102 if (!storedRequest.equals(that.storedRequest)) {103 return false;104 }105 if (userAuthentication != null ? !userAuthentication.equals(that.userAuthentication)106 : that.userAuthentication != null) {107 return false;108 }109

110 if (getDetails() != null ? !getDetails().equals(that.getDetails()) : that.getDetails() != null) {111 //return false;

112 }113

114 return true;115 }116

117 @Override118 public inthashCode() {119 int result = super.hashCode();120 result = 31 * result +storedRequest.hashCode();121 result = 31 * result + (userAuthentication != null ? userAuthentication.hashCode() : 0);122 returnresult;123 }124

125 }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值