Browse Source

token存在redis中

vincent 3 years ago
parent
commit
10b3b14366

+ 1 - 0
maxkey-authentications/.gitignore

@@ -0,0 +1 @@
+/build/

+ 1 - 0
maxkey-core/.gitignore

@@ -0,0 +1 @@
+/build/

+ 0 - 14
maxkey-gateway/build.gradle

@@ -46,20 +46,6 @@ dependencies {
 	implementation project(":maxkey-core")
 	implementation project(":maxkey-persistence")
 
-	implementation project(":maxkey-authentications:maxkey-authentication-core")
-	implementation project(":maxkey-authentications:maxkey-authentication-social")
-	implementation project(":maxkey-authentications:maxkey-authentication-captcha")
-	implementation project(":maxkey-authentications:maxkey-authentication-otp")
-
-	implementation project(":maxkey-protocols:maxkey-protocol-authorize")
-	implementation project(":maxkey-protocols:maxkey-protocol-cas")
-	implementation project(":maxkey-protocols:maxkey-protocol-extendapi")
-	implementation project(":maxkey-protocols:maxkey-protocol-formbased")
-	implementation project(":maxkey-protocols:maxkey-protocol-tokenbased")
-	implementation project(":maxkey-protocols:maxkey-protocol-oauth-2.0")
-	implementation project(":maxkey-protocols:maxkey-protocol-saml-2.0")
-	implementation project(":maxkey-protocols:maxkey-protocol-jwt")
-
 	//spring-cloud gateway server
 	implementation group: 'org.springframework.cloud', name: 'spring-cloud-gateway-server', version: "${springcloudVersion}"
 	implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-gateway', version: "${springcloudVersion}"

+ 1 - 2
maxkey-gateway/src/main/java/org/gateway/GatewayApplication.java

@@ -4,7 +4,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
+
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -15,7 +15,6 @@ import org.springframework.context.ConfigurableApplicationContext;
 
 @SpringBootApplication(
         exclude={
-                RedisAutoConfiguration.class,
                 DruidDataSourceAutoConfigure.class,
                 DataSourceAutoConfiguration.class
 })

+ 12 - 13
maxkey-gateway/src/main/java/org/gateway/filter/AuthAndLogFilter.java

@@ -3,19 +3,16 @@ package org.gateway.filter;
 import com.alibaba.cloud.commons.lang.StringUtils;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.serializer.SerializerFeature;
-import com.nimbusds.jwt.SignedJWT;
 import org.apache.http.HttpHeaders;
-import org.maxkey.authn.support.jwt.JwtLoginService;
-import org.maxkey.authz.oauth2.provider.OAuth2Authentication;
-import org.maxkey.authz.oauth2.provider.token.DefaultTokenServices;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
 import org.springframework.cloud.gateway.filter.GlobalFilter;
 import org.springframework.core.Ordered;
 import org.springframework.core.io.buffer.DataBuffer;
+import org.maxkey.persistence.redis.RedisConnection;
+import org.maxkey.persistence.redis.RedisConnectionFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.server.reactive.ServerHttpResponse;
 import org.springframework.stereotype.Component;
@@ -29,13 +26,10 @@ import reactor.core.publisher.Mono;
 public class AuthAndLogFilter implements GlobalFilter, Ordered
 {
 
-    private final Logger logger = LoggerFactory.getLogger(getClass());
+    private final Logger logger = LoggerFactory.getLogger(AuthAndLogFilter.class);
 
     @Autowired
-    @Qualifier("jwtLoginService")
-    JwtLoginService jwtLoginService;
-
-
+    RedisConnectionFactory redisConnectionFactory;
 
     @Override
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
@@ -54,12 +48,17 @@ public class AuthAndLogFilter implements GlobalFilter, Ordered
             return denyAccess(exchange, "token不存在");
 
         }
-        SignedJWT jwtToken = jwtLoginService.jwtTokenValidation(token);
-        if (null == jwtToken)
+//        SignedJWT jwtToken = jwtLoginService.jwtTokenValidation(token);
+        RedisConnection redisConnection = redisConnectionFactory.getConnection();
+        redisConnection.setex("12122",300,"1111");
+        String userInfo = (String)redisConnection.get(token);
+        redisConnection.close();
+        System.out.println(userInfo);
+        if (StringUtils.isEmpty(userInfo))
         {
             return denyAccess(exchange, "token认证错误");
         }
-
+        System.out.println("token is : "+ userInfo.toString());
         return chain.filter(exchange.mutate().build());
     }
 

+ 1 - 1
maxkey-gateway/src/main/resources/META-INF/spring.factories

@@ -1,5 +1,5 @@
 # Auto Configure
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-org.maxkey.autoconfigure.JwtAuthnAutoConfiguration
+org.maxkey.autoconfigure.RedisAutoConfiguration
 
 

+ 20 - 5
maxkey-gateway/src/main/resources/application.yml

@@ -23,10 +23,10 @@ spring:
             - PrefixPath=/
           predicates:
             - Path=/taihu-demo/**
-#      discovery:
-#        locator:
-#          enabled: true
-#          lower-case-service-id: true
+    #      discovery:
+    #        locator:
+    #          enabled: true
+    #          lower-case-service-id: true
     #      default-filters:
     #       - name: Hystrix
     #          args:
@@ -45,7 +45,22 @@ spring:
 
   main:
     web-application-type: reactive
-#spring.main.web-application-type=reactive
+
+  ############################################################################
+  #redis server  configuration                                               #
+  ############################################################################
+  redis:
+    host: ${REDIS_HOST:61.132.52.38}
+    port: ${REDIS_PORT:20003}
+    password: ${REDIS_PWD:}
+    timeout: 10000
+    jedis:
+      pool:
+        max-wait: 1000
+        max-idle: 200
+        max-active: -1
+        min-idle: 0
+
 hystrix:
   command:
     default:

+ 22 - 12
maxkey-webs/maxkey-web-maxkey/src/main/java/org/maxkey/web/contorller/ThirdLoginController.java

@@ -1,8 +1,8 @@
 package org.maxkey.web.contorller;
 
+import com.alibaba.fastjson.JSON;
 import com.nimbusds.jwt.SignedJWT;
 import io.swagger.annotations.ApiOperation;
-import org.apache.catalina.User;
 import org.maxkey.authn.AbstractAuthenticationProvider;
 import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
 import org.maxkey.authn.support.jwt.JwtLoginService;
@@ -10,6 +10,8 @@ import org.maxkey.authn.support.socialsignon.service.SocialSignOnProviderService
 import org.maxkey.entity.Organizations;
 import org.maxkey.entity.SocialsProvider;
 import org.maxkey.entity.UserInfo;
+import org.maxkey.persistence.redis.RedisConnection;
+import org.maxkey.persistence.redis.RedisConnectionFactory;
 import org.maxkey.persistence.service.OrganizationsService;
 import org.maxkey.persistence.service.UserInfoService;
 import org.maxkey.util.RetCode;
@@ -20,15 +22,15 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.stereotype.Controller;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.*;
 import org.maxkey.util.RetResult;
-import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 @RestController
 @RequestMapping("/thirdLogin")
@@ -60,6 +62,9 @@ public class ThirdLoginController
     SocialSignOnProviderService socialSignOnProviderService;
 
 
+    @Autowired
+    RedisConnectionFactory redisConnectionFactory;
+
 
     /**
      * init login
@@ -84,11 +89,16 @@ public class ThirdLoginController
             String token = jwtLoginService.buildLoginJwt();
             data.put("userInfo", userInfo);
             data.put("token", token);
+            RedisConnection redisConnection = redisConnectionFactory.getConnection();
+            // 默认 30分钟
+            redisConnection.setex(token,1800,JSON.toJSONString(userInfo));
+            redisConnection.close();
             _logger.debug("token >>>" + token);
             result.setData(data);
             result.setCode(RetCode.SUCCESS);
             return result;
         }
+
         result = new RetResult();
         result.setCode(RetCode.FAIL);
         result.setMsg("用户不存在");
@@ -113,8 +123,9 @@ public class ThirdLoginController
         return new RetResult().setMsg("token出错").setCode(RetCode.FAIL);
     }
 
-    @RequestMapping(value={"/usersList"})
-    public RetResult usersList(){
+    @RequestMapping(value = {"/usersList"})
+    public RetResult usersList()
+    {
         List<UserInfo> users = userInfoService.query(null);
 
         return new RetResult().setMsg("请求成功").setData(users).setCode(RetCode.SUCCESS);
@@ -127,7 +138,7 @@ public class ThirdLoginController
      * @return
      */
     @GetMapping({"/organizationsTree"})
-    public List<HashMap<String, Object>> organizationsTree(HttpServletRequest request,@RequestParam(value = "id", required = false) String id)
+    public List<HashMap<String, Object>> organizationsTree(HttpServletRequest request, @RequestParam(value = "id", required = false) String id)
     {
         String authorization = request.getHeader("Authorization");
         _logger.debug("authorization id :" + authorization);
@@ -161,12 +172,13 @@ public class ThirdLoginController
 
 
     /**
-     *  获取第三方登录信息
-     *  dingtalk: /logon/oauth20/authorize/dingtalk
+     * 获取第三方登录信息
+     * dingtalk: /logon/oauth20/authorize/dingtalk
      */
     @GetMapping({"/loadSocials"})
-    public RetResult loadSocials(){
-        List<SocialsProvider> socialsProviderList =  socialSignOnProviderService.getSocialSignOnProviders();
+    public RetResult loadSocials()
+    {
+        List<SocialsProvider> socialsProviderList = socialSignOnProviderService.getSocialSignOnProviders();
         RetResult result = new RetResult();
         result.setCode(RetCode.SUCCESS);
         result.setMsg("查询成功");
@@ -175,6 +187,4 @@ public class ThirdLoginController
     }
 
 
-
-
 }

+ 1 - 1
maxkey-webs/maxkey-web-maxkey/src/main/resources/application-http.properties

@@ -121,7 +121,7 @@ mybatis.table-column-case                       =lowercase
 ############################################################################
 spring.redis.host                               =${REDIS_HOST:61.132.52.38}
 spring.redis.port                               =${REDIS_PORT:20003}
-spring.redis.password                           =${REDIS_PWD:password}
+spring.redis.password                           =${REDIS_PWD:}
 spring.redis.timeout                            =10000
 spring.redis.jedis.pool.max-wait                =1000
 spring.redis.jedis.pool.max-idle                =200