From 8789f4885d5d417291f25374336cba849e265e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Tue, 7 May 2024 17:05:07 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A2=9E=E5=8A=A0=20permission.has?= =?UTF-8?q?Scope=20=E5=88=A4=E6=96=AD=E5=B7=A5=E5=85=B7=EF=BC=8C=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=A8=A1=E5=BC=8F=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 8f0d861d7a6b1d25a71160142f1aac66687ef6c5) --- .../security/component/PermissionService.java | 71 ++++++++++++++----- 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PermissionService.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PermissionService.java index 3dee9caa3..daf55b578 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PermissionService.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PermissionService.java @@ -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,22 +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 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 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 scopeList = MapUtil.get(bearerTokenAuthentication.getTokenAttributes() + , OAuth2ParameterNames.SCOPE, List.class, new ArrayList<>()); + return scopeList.stream().anyMatch(x -> PatternMatchUtils.simpleMatch(scopes, x)); + } + return false; + } }