Browse Source

验证token有效性

zhoutg 6 years ago
parent
commit
2a0b362eee

+ 2 - 0
gateway-service/src/main/java/com/diagbot/client/UserServiceClient.java

@@ -19,6 +19,8 @@ public interface UserServiceClient {
     @PostMapping("/user/login")
     RespDTO<User> login(@RequestParam("username") String username, @RequestParam("password") String password);
 
+    @PostMapping("/user/verifyToken")
+    RespDTO<Boolean> verifyToken(@RequestParam("token") String token);
 }
 
 

+ 6 - 0
gateway-service/src/main/java/com/diagbot/client/hystrix/UserServiceHystrix.java

@@ -19,4 +19,10 @@ public class UserServiceHystrix implements UserServiceClient {
         log.error("【hystrix】调用{}异常", "login");
         return null;
     }
+
+    @Override
+    public RespDTO<Boolean> verifyToken(String token) {
+        log.error("【hystrix】调用{}异常", "verifyToken");
+        return null;
+    }
 }

+ 10 - 0
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -5,6 +5,7 @@ import com.diagbot.client.UserServiceClient;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.ServiceToken;
 import com.diagbot.util.GsonUtil;
+import com.diagbot.util.StringUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -70,6 +71,15 @@ public class GlobalGatewayFilter implements GlobalFilter {
         log.info("APIURL:{}", requestUri);
         log.info("SERVICENAME:{}", serviceName);
 
+        //验证token有效性
+        String token = request.getHeaders().getFirst("Authorization");
+        if(StringUtil.isNotEmpty(token)) {
+            RespDTO<Boolean> res = userServiceClient.verifyToken(token);
+            if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
+                return getVoidMono(serverWebExchange, res);
+            }
+        }
+
         //        if(!IS_GENERATE) {
         //            RespDTO<List<ServiceFilter>> filter = diagbotmanServiceClient.getAll();
         //            if (filter != null){

+ 1 - 0
user-service/src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -44,6 +44,7 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .antMatchers("/user/verifyExistUsername").permitAll()
                 .antMatchers("/userAuthentication/getAuthInfoCount").permitAll()
                 .antMatchers("/user/getUserAllInfo").permitAll()
+                .antMatchers("/user/verifyToken").permitAll()
                 .antMatchers("/**").authenticated();
         //        .antMatchers("/**").permitAll();
     }

+ 1 - 0
user-service/src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -88,6 +88,7 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/user/verifyExistUsername", request)
                 || matchers("/userAuthentication/getAuthInfoCount", request)
                 || matchers("/user/getUserAllInfo", request)
+                || matchers("/user/verifyToken", request)
                 || matchers("/", request)) {
             return true;
         }

+ 16 - 0
user-service/src/main/java/com/diagbot/web/UserController.java

@@ -11,6 +11,7 @@ import com.diagbot.dto.UserOrgDTO;
 import com.diagbot.entity.Permission;
 import com.diagbot.entity.User;
 import com.diagbot.facade.PermissionFacade;
+import com.diagbot.facade.TokenFacade;
 import com.diagbot.facade.UserFacade;
 import com.diagbot.vo.AppkeySecretVO;
 import com.diagbot.vo.BaseIdVO;
@@ -52,6 +53,8 @@ public class UserController {
     @Autowired
     private UserFacade userFacade;
     @Autowired
+    private TokenFacade tokenFacade;
+    @Autowired
     private PermissionFacade permissionFacade;
 
 
@@ -72,6 +75,19 @@ public class UserController {
     }
 
 
+
+    @ApiOperation(value = "验证token有效性[by:zhoutg]",
+            notes = "token:token信息,必填<br>")
+    @PostMapping("/verifyToken")
+    @SysLogger("verifyToken")
+    @ApiIgnore
+    public RespDTO<Boolean> verifyToken(@RequestBody String token) {
+        Boolean data = tokenFacade.verifyToken(token);
+        return RespDTO.onSuc(data);
+    }
+
+
+
     @ApiOperation(value = "校验用户(手机号)已注册[by:zhoutg]",
             notes = "username:用户名(手机号),必填<br>")
     @PostMapping("/verifyExistUsername")