✨ 增加 permission.hasScope 判断工具,方便客户端模式使用
(cherry picked from commit 8f0d861d7a6b1d25a71160142f1aac66687ef6c5)
This commit is contained in:
1 parent
079d70e204
commit
179f818e4b
1 file changed
+54
-19
+54
-19
@@ -17,14 +17,19 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
||||
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
|
||||
import org.springframework.util.PatternMatchUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
@@ -32,24 +37,54 @@ import java.util.Collection;
|
||||
*/
|
||||
public class PermissionService {
|
||||
|
||||
/**
|
||||
* 判断接口是否有任意xxx,xxx权限
|
||||
* @param permissions 权限
|
||||
* @return {boolean}
|
||||
*/
|
||||
public boolean hasPermission(String... permissions) {
|
||||
if (ArrayUtil.isEmpty(permissions)) {
|
||||
return false;
|
||||
}
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
return false;
|
||||
}
|
||||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
return authorities.stream()
|
||||
.map(GrantedAuthority::getAuthority)
|
||||
.filter(StringUtils::hasText)
|
||||
.anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
|
||||
}
|
||||
/**
|
||||
* 判断接口是否有任意xxx,xxx权限
|
||||
*
|
||||
* @param permissions 权限
|
||||
* @return {boolean}
|
||||
*/
|
||||
public boolean hasPermission(String... permissions) {
|
||||
if (ArrayUtil.isEmpty(permissions)) {
|
||||
return false;
|
||||
}
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication == null) {
|
||||
return false;
|
||||
}
|
||||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||
return authorities.stream()
|
||||
.map(GrantedAuthority::getAuthority)
|
||||
.filter(StringUtils::hasText)
|
||||
.anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端模式是否有该接口权限
|
||||
*
|
||||
* @param scopes scope 列表
|
||||
* @return {boolean}
|
||||
* @PreAuthorize("@pms.hasPermission('sys_role_add')")
|
||||
*/
|
||||
public boolean hasScope(String... scopes) {
|
||||
if (ArrayUtil.isEmpty(scopes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (authentication instanceof BearerTokenAuthentication) {
|
||||
BearerTokenAuthentication bearerTokenAuthentication = (BearerTokenAuthentication) authentication;
|
||||
List<String> scopeList = MapUtil.get(bearerTokenAuthentication.getTokenAttributes()
|
||||
, OAuth2ParameterNames.SCOPE, List.class, new ArrayList<>());
|
||||
return scopeList.stream().anyMatch(x -> PatternMatchUtils.simpleMatch(scopes, x));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user