|
|
@@ -0,0 +1,52 @@
|
|
|
+package cn.iselab.mooctest.site.configure.realm;
|
|
|
+
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
|
+import org.apache.shiro.cache.Cache;
|
|
|
+import org.apache.shiro.cache.CacheManager;
|
|
|
+import org.apache.shiro.subject.SimplePrincipalCollection;
|
|
|
+import org.apache.shiro.subject.Subject;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author sean
|
|
|
+ * @date 2017-07-28.
|
|
|
+ */
|
|
|
+public class ShiroAuthorizationHelper {
|
|
|
+
|
|
|
+ private static CacheManager cacheManager;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除用户权限
|
|
|
+ * @param principal
|
|
|
+ */
|
|
|
+ public static void clearAuthorizationInfo(SimplePrincipalCollection principal) {
|
|
|
+
|
|
|
+ Cache<Object, Object> cache = cacheManager.getCache("myShiroCache");//myShiroCache是我配置用于缓存的cache的Name,在spring配置文件中配置,可以看文章最后
|
|
|
+
|
|
|
+ cache.remove(principal);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 清除当前用户的权限
|
|
|
+ */
|
|
|
+ public static void clearAuthorizationInfo() {
|
|
|
+ if (SecurityUtils.getSubject().isAuthenticated()) {
|
|
|
+ Subject subject = SecurityUtils.getSubject();
|
|
|
+ String username = subject.getPrincipal().toString();
|
|
|
+ String realmName = subject.getPrincipals().getRealmNames().iterator().next();
|
|
|
+ SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(username, realmName);
|
|
|
+ // 调用清理用户权限
|
|
|
+ clearAuthorizationInfo(principalCollection);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 由Spring bean将对象注入
|
|
|
+ * @param cacheManager
|
|
|
+ */
|
|
|
+ public static void setCacheManager(CacheManager cacheManager) {
|
|
|
+ ShiroAuthorizationHelper.cacheManager = cacheManager;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|