From 5268c8348daad40edaf3d1a60d44cbd20a5ea5f6 Mon Sep 17 00:00:00 2001
From: wuy <1311695042@qq.com>
Date: Tue, 6 Aug 2019 10:46:49 +0800
Subject: [PATCH 1/8] =?UTF-8?q?+=20=E5=A2=9E=E5=8A=A0=E6=9D=83=E9=99=90?=
=?UTF-8?q?=E7=BC=93=E5=AD=98=E9=85=8D=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 10 ++++
diboot-shiro/build.gradle | 1 +
.../authz/annotation/AuthorizationCache.java | 20 +++++++
.../shiro/authz/aspect/CacheHandler.java | 57 +++++++++++++++++++
.../shiro/authz/cache/MemoryCondition.java | 24 ++++++++
.../diboot/shiro/authz/cache/RedisCache.java | 51 +++++++++++++++++
.../shiro/authz/cache/RedisCacheManager.java | 28 +++++++++
.../shiro/authz/cache/RedisCondition.java | 23 ++++++++
.../AuthorizationAutoConfiguration.java | 4 +-
.../authz/properties/AuthCacheProperties.java | 50 ++++++++++++++++
.../properties/AuthorizationProperties.java | 29 +++++++---
.../com/diboot/shiro/config/ShiroConfig.java | 42 +++++++++++++-
.../shiro/controller/RoleController.java | 7 +++
.../com/diboot/shiro/jwt/BaseJwtRealm.java | 2 +
.../shiro/util/ProxyToTargetObjectHelper.java | 6 +-
15 files changed, 340 insertions(+), 14 deletions(-)
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/annotation/AuthorizationCache.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/aspect/CacheHandler.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/cache/MemoryCondition.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/cache/RedisCache.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/cache/RedisCacheManager.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/cache/RedisCondition.java
create mode 100644 diboot-shiro/src/main/java/com/diboot/shiro/authz/properties/AuthCacheProperties.java
diff --git a/README.md b/README.md
index ef37742..00e9070 100644
--- a/README.md
+++ b/README.md
@@ -35,6 +35,11 @@ RBAC的角色权限+基于Shiro的细粒度权限控制
#### 2、@AuthorizationWrapper
类/方法注解,在保证shiro的@RequirePermissions注解的功能基础上,增加名称、权限前缀特性,使用方式同@RequiresPermissions
+#### 3、@AuthorizationCache
+方法注解,在资源授权校验过程中,系统会频繁与数据库进行交互,故而提供缓存机制
+ * 缓存时机:缓存会在用户第一次进行权限验证的之后缓存数据
+ * 当前注解作用:如果通过系统调整权限,只需要将该注解加在更新或添加权限处,将会清空权限缓存,下次进入将重新加载权限
+
#### 3、AuthorizationProperties
提供一些权限相关的配置,主要包括:
- 权限环境变量:提供dev、test、prod三种选项
@@ -54,6 +59,11 @@ diboot.shiro.auth.env=dev
diboot.shiro.auth.has-all-permissions-role-list[0]=ALL1
diboot.shiro.auth.has-all-permissions-role-list[1]=ALL2
diboot.shiro.auth.has-all-permissions-role-list[2]=ALL3
+#配置权限缓存机制
+##是否开启缓存
+diboot.shiro.cache.permission-caching-enabled=true
+##缓存方式:暂时提供shiro内置的内存缓存
+diboot.shiro.cache.cache-way=memory
```
#### 4、AuthorizationStorage
diff --git a/diboot-shiro/build.gradle b/diboot-shiro/build.gradle
index 53c96a8..867fa78 100644
--- a/diboot-shiro/build.gradle
+++ b/diboot-shiro/build.gradle
@@ -5,6 +5,7 @@ dependencies {
// compile("org.springframework.boot:spring-boot-configuration-processor")
compile("org.apache.shiro:shiro-spring:1.4.1")
+ compile("org.aspectj:aspectjweaver")
compile("com.auth0:java-jwt:3.4.1",
"io.jsonwebtoken:jjwt:0.9.1")
diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/authz/annotation/AuthorizationCache.java b/diboot-shiro/src/main/java/com/diboot/shiro/authz/annotation/AuthorizationCache.java
new file mode 100644
index 0000000..1d9c30d
--- /dev/null
+++ b/diboot-shiro/src/main/java/com/diboot/shiro/authz/annotation/AuthorizationCache.java
@@ -0,0 +1,20 @@
+package com.diboot.shiro.authz.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * 权限缓存
+ *
+ * 缓存目的:在资源授权校验过程中,系统会频繁与数据库进行交互,故而提供缓存机制
+ * 缓存时机:缓存会在用户第一次进行权限验证的之后缓存数据
+ * 当前注解作用:如果通过系统调整角色的权限,只需要将该注解加在更新或添加权限处,将会清空缓存,下次进入将重新加载
+ *
+ * @author : wee
+ * @version v1.0
+ * @Date 2019-07-23 09:27
+ */
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface AuthorizationCache {
+}
diff --git a/diboot-shiro/src/main/java/com/diboot/shiro/authz/aspect/CacheHandler.java b/diboot-shiro/src/main/java/com/diboot/shiro/authz/aspect/CacheHandler.java
new file mode 100644
index 0000000..438f4d0
--- /dev/null
+++ b/diboot-shiro/src/main/java/com/diboot/shiro/authz/aspect/CacheHandler.java
@@ -0,0 +1,57 @@
+package com.diboot.shiro.authz.aspect;
+
+import com.diboot.core.util.V;
+import com.diboot.shiro.authz.annotation.AuthorizationCache;
+import com.diboot.shiro.jwt.BaseJwtRealm;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.cache.Cache;
+import org.apache.shiro.cache.CacheManager;
+import org.apache.tomcat.util.http.parser.Authorization;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * 当有操作的时候,自动更新被注解的相关数据
+ * @author : wee
+ * @version : v2.0
+ * @Date 2019-07-24 23:20
+ */
+@Slf4j
+@Aspect
+@Component
+public class CacheHandler{
+
+ private static final String DEFAULT_AUTHORIZATION_CACHE_SUFFIX = ".authorizationCache";
+
+ @Autowired
+ private CacheManager cacheManager;
+
+ /**
+ * 设置切片
+ */
+ @Pointcut("@annotation(com.diboot.shiro.authz.annotation.AuthorizationCache)")
+ public void proxyAspect() {}
+
+ /**
+ * 当请求{@link AuthorizationCache}注解的方法执行完成后,自动触发此处切面
+ * 作用:重新缓存权限和方法
+ * @param joinPoint
+ */
+ @AfterReturning("proxyAspect()")
+ public void afterReturning(JoinPoint joinPoint) {
+ try {
+ log.info("【修改权限】==> 正在调用【{}#{}()】方法修改!", joinPoint.getThis().getClass(), joinPoint.getSignature().getName());
+ Cache