From 670457fd7c03f04b73166cba0509ad8c7da8d576 Mon Sep 17 00:00:00 2001 From: Lucas Shao Date: Tue, 6 Aug 2019 11:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=9D=83=E9=99=90=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=9A=84=E7=BB=86=E8=8A=82?= =?UTF-8?q?=E9=97=AE=E9=A2=98.=20=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E7=9A=84=E8=BF=94=E5=9B=9E=E6=9D=83=E9=99=90?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=A4=B1=E8=B4=A5=E7=9A=84=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E4=BA=86.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit aad29c6a0b400c887acde5292941e895c095569c) --- .../auth/aspect/PreAuthorizeAspect.java | 130 +++++++++--------- 1 file changed, 62 insertions(+), 68 deletions(-) diff --git a/ruoyi-common/ruoyi-common-auth/src/main/java/com/ruoyi/common/auth/aspect/PreAuthorizeAspect.java b/ruoyi-common/ruoyi-common-auth/src/main/java/com/ruoyi/common/auth/aspect/PreAuthorizeAspect.java index 88236e1..91e8f9a 100644 --- a/ruoyi-common/ruoyi-common-auth/src/main/java/com/ruoyi/common/auth/aspect/PreAuthorizeAspect.java +++ b/ruoyi-common/ruoyi-common-auth/src/main/java/com/ruoyi/common/auth/aspect/PreAuthorizeAspect.java @@ -1,69 +1,63 @@ -package com.ruoyi.common.auth.aspect; - -import java.lang.reflect.Method; - -import javax.servlet.http.HttpServletRequest; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.Signature; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.reflect.MethodSignature; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import com.ruoyi.common.auth.annotation.HasPermissions; -import com.ruoyi.common.constant.Constants; -import com.ruoyi.common.core.domain.R; -import com.ruoyi.common.utils.ServletUtils; -import com.ruoyi.system.feign.RemoteMenuService; - -import lombok.extern.slf4j.Slf4j; - -@Aspect -@Component -@Slf4j -public class PreAuthorizeAspect -{ - @Autowired - private RemoteMenuService sysMenuClient; - - @Around("@annotation(com.ruoyi.common.auth.annotation.HasPermissions)") - public Object around(ProceedingJoinPoint point) throws Throwable - { - Signature signature = point.getSignature(); - MethodSignature methodSignature = (MethodSignature) signature; - Method method = methodSignature.getMethod(); - HasPermissions annotation = method.getAnnotation(HasPermissions.class); - if (annotation == null) - { - return point.proceed(); - } - String authority = new StringBuilder(annotation.value()).toString(); - if (has(authority)) - { - return point.proceed(); - } - else - { - return R.error(401, "权限不足"); - } - } - - private boolean has(String authority) - { - // 用超管帐号方便测试,拥有所有权限 - HttpServletRequest request = ServletUtils.getRequest(); - Long userid = Long.valueOf(request.getHeader(Constants.USER_KEY)); - log.debug("userid:{}", userid); - if (null != userid) - { - if (userid == 1l) - { - return true; - } - return sysMenuClient.selectPermsByUserId(userid).stream().anyMatch(a -> authority.equals(a)); - } - return false; - } +package com.ruoyi.common.auth.aspect; + +import com.ruoyi.common.auth.annotation.HasPermissions; +import com.ruoyi.common.constant.Constants; +import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.exception.UnauthorizedException; +import com.ruoyi.common.utils.ServletUtils; +import com.ruoyi.system.feign.RemoteMenuService; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Method; +import java.util.Optional; + +@Aspect +@Component +@Slf4j +public class PreAuthorizeAspect { + @Autowired + private RemoteMenuService sysMenuClient; + + @Around("@annotation(com.ruoyi.common.auth.annotation.HasPermissions)") + public Object around(ProceedingJoinPoint point) throws Throwable { + Signature signature = point.getSignature(); + MethodSignature methodSignature = (MethodSignature) signature; + Method method = methodSignature.getMethod(); + HasPermissions annotation = method.getAnnotation(HasPermissions.class); + if (annotation == null) { + return point.proceed(); + } + String authority = new StringBuilder(annotation.value()).toString(); + if (has(authority)) { + return point.proceed(); + } else { + throw new UnauthorizedException(); + } + } + + private boolean has(String authority) { + // 用超管帐号方便测试,拥有所有权限 + HttpServletRequest request = ServletUtils.getRequest(); + String tmpUserKey = request.getHeader(Constants.USER_KEY); + if (Optional.ofNullable(tmpUserKey).isPresent()) { + Long userId = Long.valueOf(tmpUserKey); + log.debug("userid:{}", userId); + if (userId == 1L) { + return true; + } + return sysMenuClient + .selectPermsByUserId(userId) + .stream() + .anyMatch(authority::equals); + } + return false; + } } \ No newline at end of file -- Gitee