Administrator пре 1 година
родитељ
комит
943d624d3c

+ 1 - 1
pom.xml

@@ -226,4 +226,4 @@
 			</plugin>
 		</plugins>
 	</build>
-</project>
+</project>

+ 6 - 0
src/main/java/com/xiesx/fastboot/core/token/annotation/GoToken.java

@@ -15,4 +15,10 @@ import java.lang.annotation.Target;
 @Retention(RetentionPolicy.RUNTIME)
 public @interface GoToken {
 
+    /**
+     * 默认为空字符串 如果不为空则需要验证权限
+     * @return
+     */
+    String permissions() default "";
+
 }

+ 11 - 0
src/main/java/com/xiesx/fastboot/core/token/handle/GoTokenAuthentication.java

@@ -0,0 +1,11 @@
+package com.xiesx.fastboot.core.token.handle;
+
+
+/**
+ * GoToken鉴权接口
+ */
+public interface GoTokenAuthentication{
+
+    void authenticate(Long userId, String permissions);
+
+}

+ 16 - 2
src/main/java/com/xiesx/fastboot/core/token/handle/TokenInterceptorHandler.java

@@ -11,6 +11,8 @@ import javax.servlet.http.HttpServletResponse;
 
 import com.alibaba.fastjson.JSONObject;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.web.method.HandlerMethod;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
@@ -40,13 +42,17 @@ public class TokenInterceptorHandler implements HandlerInterceptor {
 
     private String key;
 
+    @Autowired
+    @Lazy
+    GoTokenAuthentication goTokenAuthentication;
+
     /**
      * Controller执行之前,如果返回false,controller不执行
      *
      * @throws Exception
      */
     @Override
-    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
         log.debug("preHandle ......");
         // 获取配置
         TokenProperties properties = SpringHelper.getBean(TokenProperties.class);
@@ -71,9 +77,10 @@ public class TokenInterceptorHandler implements HandlerInterceptor {
                                 throw new RunException(RunExc.TOKEN, "请重新登录");
                             }
                         }
+                        Claims claims = null;
                         try {
                             // 获取token
-                            Claims claims = JwtHelper.parser(token);
+                            claims = JwtHelper.parser(token);
                             // 设置request
                             request.setAttribute(TokenCfg.USERID, claims.getOrDefault(TokenCfg.USERID, ""));
                             request.setAttribute(TokenCfg.USERNAME, claims.getOrDefault(TokenCfg.USERNAME, ""));
@@ -87,6 +94,12 @@ public class TokenInterceptorHandler implements HandlerInterceptor {
                                 throw new RunException(RunExc.TOKEN);
                             }
                         }
+                        //是否需要验证Permissions
+                        GoToken goToken = (GoToken)annotation2;
+                        if(StringUtils.isNotEmpty(goToken.permissions())){
+                            Long userId = Long.parseLong(claims.getOrDefault(TokenCfg.USERID, "").toString());
+                            goTokenAuthentication.authenticate(userId, goToken.permissions());
+                        }
                     }
                 }
             }
@@ -134,4 +147,5 @@ public class TokenInterceptorHandler implements HandlerInterceptor {
         }
         return param;
     }
+
 }