From d8a687741273d121586b83745280c57f7e9d3297 Mon Sep 17 00:00:00 2001
From: XiaoRuby <3114200645@qq.com>
Date: 星期一, 07 八月 2023 17:51:28 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/yuanchu_code/lims-management-system

---
 user-server/src/main/java/com/yuanchu/limslaboratory/shiro/realm/JwtRealm.java |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/realm/JwtRealm.java b/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/realm/JwtRealm.java
new file mode 100644
index 0000000..d75fd50
--- /dev/null
+++ b/user-server/src/main/java/com/yuanchu/limslaboratory/shiro/realm/JwtRealm.java
@@ -0,0 +1,48 @@
+package com.yuanchu.limslaboratory.shiro.realm;
+
+import com.yuanchu.limslaboratory.pojo.User;
+import com.yuanchu.limslaboratory.shiro.JwtToken;
+import com.yuanchu.limslaboratory.utils.RedisUtil;
+import org.apache.shiro.authc.*;
+import org.apache.shiro.authz.AuthorizationInfo;
+import org.apache.shiro.authz.SimpleAuthorizationInfo;
+import org.apache.shiro.realm.AuthorizingRealm;
+import org.apache.shiro.subject.PrincipalCollection;
+
+public class JwtRealm extends AuthorizingRealm {
+    /**
+     * 闄愬畾杩欎釜 Realm 鍙鐞嗘垜浠嚜瀹氫箟鐨� JwtToken
+     */
+    @Override
+    public boolean supports(AuthenticationToken token) {
+        return token instanceof JwtToken;
+    }
+
+    /**
+     * 姝ゅ鐨� SimpleAuthenticationInfo 鍙繑鍥炰换鎰忓�硷紝瀵嗙爜鏍¢獙鏃朵笉浼氱敤鍒板畠
+     */
+    @Override
+    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
+        JwtToken jwtToken = (JwtToken) authcToken;
+        if (jwtToken.getPrincipal() == null) {
+            throw new AccountException("Token鍙傛暟寮傚父锛�");
+        }
+        // 褰撳墠鐢ㄦ埛
+        String account = jwtToken.getPrincipal().toString();
+        // 褰撳墠鐢ㄦ埛鐨則oken
+        String credentials = (String)jwtToken.getCredentials();
+        User user = (User) RedisUtil.get(credentials);
+        // 鐢ㄦ埛涓嶅瓨鍦�
+        if (user == null) {
+            throw new UnknownAccountException("鐢ㄦ埛涓嶅瓨鍦紒");
+        }
+        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, account, getName());
+        return info;
+    }
+
+    @Override
+    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
+        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
+        return info;
+    }
+}

--
Gitblit v1.9.3