Browse Source

新增自贡水务权限逻辑

linzhiwei 2 years ago
parent
commit
2fc45c6ea9

+ 35 - 11
tofly-auth/src/main/java/com/tofly/auth/oauth/ToflyAuthenticationProvider.java

@@ -1,16 +1,20 @@
 package com.tofly.auth.oauth;
 
+import com.tofly.auth.util.AESUtil;
 import com.tofly.auth.util.RSAUtils;
 import com.tofly.common.core.constant.CommonConstants;
 import com.tofly.common.core.constant.SecurityConstants;
 import com.tofly.common.core.entity.ResultRespone;
 import com.tofly.common.core.util.IpUtil;
+import com.tofly.common.core.util.PasswordUtil;
 import com.tofly.common.core.util.SpringContextHolder;
 import com.tofly.common.core.util.StringUtil;
 import com.tofly.common.oauth.auth.ToflyUser;
 import com.tofly.entity.pojo.User;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.Cache;
 import org.springframework.cache.CacheManager;
 import org.springframework.security.authentication.AuthenticationProvider;
@@ -34,17 +38,23 @@ import java.util.Map;
 import java.util.Objects;
 
 @Component
-@AllArgsConstructor
 @Slf4j
 public class ToflyAuthenticationProvider implements AuthenticationProvider {
-    private final UserDetailsService userDetailService;
+    @Autowired
+    private  UserDetailsService userDetailService;
 
-
-    private final CacheManager cacheManager;
+    @Autowired
+    private  CacheManager cacheManager;
 
     //private final AuthTokenService authTokenService;
+    @Autowired
+    private  TokenStore tokenStore;
+
+    @Value("${aes.key}")
+    private  String key;
+    @Value("${aes.iv}")
+    private  String iv;
 
-    private final TokenStore tokenStore;
     /**
      * 进行身份认证
      *
@@ -81,7 +91,7 @@ public class ToflyAuthenticationProvider implements AuthenticationProvider {
         log.info("界面传过来的password:{}",password);
         log.info("数据库查询出来的password:{}",userDetails.getPassword());
         try {
-            password = RSAUtils.priKeyDecryption(password);
+            password = AESUtil.decryptData(password,key,iv);
         } catch (Exception exception) {
             exception.printStackTrace();
         }
@@ -125,11 +135,11 @@ public class ToflyAuthenticationProvider implements AuthenticationProvider {
 //            params.put("client_id",(String)((Map) authentication.getDetails()).get("client_id"));
 //            params.put("username",authentication.getName());
 
-//            RedisTokenStore redisTokenStore=(RedisTokenStore)tokenStore;
-//            Collection<OAuth2AccessToken> client_id = redisTokenStore.findTokensByClientIdAndUserName((String)((Map) authentication.getDetails()).get("client_id"), authentication.getName());
-//            client_id.forEach(oAuth2AccessToken -> {
-//                tokenStore.removeAccessToken(oAuth2AccessToken);
-//            });
+            RedisTokenStore redisTokenStore=(RedisTokenStore)tokenStore;
+            Collection<OAuth2AccessToken> client_id = redisTokenStore.findTokensByClientIdAndUserName((String)((Map) authentication.getDetails()).get("client_id"), authentication.getName());
+            client_id.forEach(oAuth2AccessToken -> {
+                tokenStore.removeAccessToken(oAuth2AccessToken);
+            });
             //ResultRespone resultRespone=authTokenService.removeTokenByUser(params,SecurityConstants.FROM_IN);
             //如果密码错误次数不为0,则将缓存中的次数修改为0
             if(pwdt!=0){
@@ -188,4 +198,18 @@ public class ToflyAuthenticationProvider implements AuthenticationProvider {
             super(msg);
         }
     }
+
+//    public static void main(String[] args) {
+//        try {
+//            String s = AESUtil.encryptData("93c419c4094f27039f7843d3aca4912c", "Tofly028@zgswgis", "giszgsw@028Tofly");
+//            String s1 = PasswordUtil.passwordEncode("93c419c4094f27039f7843d3aca4912c");
+//            System.out.println(s);
+//            System.out.println(s1);
+//            String q1 = "$2a$2a$10$7PCQT7m4MSO5eZd3CI8Daeukon.3gqShOXPfnGzTE6dnck0cjZtAi";
+//            String q2 = "$2a$10$oiHgRX8xwtNiSGZqeMFFNOgBIfMeI9auqiT1vZ69QCCXst2D55NVy";
+//
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//    }
 }

+ 90 - 0
tofly-auth/src/main/java/com/tofly/auth/util/AESUtil.java

@@ -0,0 +1,90 @@
+package com.tofly.auth.util;
+
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ * AES加解密
+ */
+public class AESUtil {
+    // 加密
+    public static String encry(String content, String key) throws Exception {
+        String IV = key;
+        if (key.length() > 16) {
+            // IV为商户MD5密钥后16位
+            IV = key.substring(key.length() - 16);
+            // RES的KEY 为商户MD5密钥的前16位
+            key = key.substring(0, 16);
+        }
+
+        return encryptData(content, key, IV);
+    }
+
+    // 解密
+    public static String desEncry(String content, String key) throws Exception {
+        String IV = key;
+        if (key.length() > 16) {
+            // IV为商户MD5密钥后16位
+            IV = key.substring(key.length() - 16);
+            // RES的KEY 为商户MD5密钥的前16位
+            key = key.substring(0, 16);
+        }
+        return decryptData(content, key, IV);
+    }
+
+    /**
+     * aes 加密
+     *
+     * @param data
+     * @return
+     */
+    public static String encryptData(String data, String key, String IV) throws Exception {
+        try {
+            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+            byte[] dataBytes = data.getBytes("UTF-8");
+            int plaintextLength = dataBytes.length;
+            // if (plaintextLength % blockSize != 0) {
+            // plaintextLength = plaintextLength + (blockSize - (plaintextLength
+            // % blockSize));
+            // }
+            byte[] plaintext = new byte[plaintextLength];
+            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
+            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
+            IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
+            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
+            byte[] encrypted = cipher.doFinal(plaintext);
+            return new String(Base64.encodeBase64(encrypted));
+        } catch (Exception e) {
+            throw e;
+        }
+
+    }
+
+    /**
+     * aes 解密
+     *
+     * @param data
+     *            密文
+     * @return
+     */
+    public static String decryptData(String data, String key, String IV)  {
+        try {
+            byte[] encrypted1 = Base64.decodeBase64(data.getBytes("UTF-8"));
+            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
+            IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
+            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
+            byte[] original = cipher.doFinal(encrypted1);
+            String originalString = new String(original, "UTF-8");
+            return originalString;
+        } catch (Exception e) {
+           e.printStackTrace();
+           return "";
+        }
+    }
+
+}
+

+ 2 - 2
tofly-auth/src/main/resources/bootstrap-loc.yml

@@ -3,7 +3,7 @@ spring:
     nacos:
       config:
         # nacos配置中心指定空间
-        namespace: hnls
+        namespace: zgsw
         # nacos部署IP
         server-addr: localhost:8848
         # 配置后缀
@@ -13,7 +13,7 @@ spring:
         shared-configs[1]:
           dataId: public_config_redis.yaml
       discovery:
-        namespace: hnls
+        namespace: zgsw
         server-addr: localhost:8848
   application:
     # 该应用在nacos配置中的名称

+ 2 - 2
tofly-auth/src/main/resources/bootstrap-pro.yml

@@ -3,7 +3,7 @@ spring:
     nacos:
       config:
         # nacos配置中心指定空间
-        namespace: tofly-xrty-pro
+        namespace: zgsw
         # nacos部署IP
         server-addr: tofly-nacos:8848
         # 配置后缀
@@ -13,7 +13,7 @@ spring:
         shared-configs[1]:
           dataId: public_config_redis.yaml
       discovery:
-        namespace: tofly-xrty-pro
+        namespace: zgsw
         server-addr: tofly-nacos:8848
   application:
     # 该应用在nacos配置中的名称

+ 1 - 1
tofly-auth/src/main/resources/bootstrap.yml

@@ -1,3 +1,3 @@
 spring:
   profiles:
-    active: loc
+    active: pro

+ 2 - 2
tofly-getway/src/main/resources/bootstrap-loc.yml

@@ -3,7 +3,7 @@ spring:
     nacos:
       config:
         # nacos配置中心指定空间
-        namespace: hnls
+        namespace: zgsw
         # nacos部署IP
         server-addr: localhost:8848
         # 配置后缀
@@ -11,7 +11,7 @@ spring:
         shared-configs[0]:
           dataId: public_config_redis.yaml
       discovery:
-        namespace: hnls
+        namespace: zgsw
         server-addr: localhost:8848
   application:
     # 该应用在nacos配置中的名称

+ 1 - 1
tofly-getway/src/main/resources/bootstrap-pro.yml

@@ -3,7 +3,7 @@ spring:
     nacos:
       config:
         # nacos配置中心指定空间
-        namespace: tofly-xrty-pro
+        namespace: zgsw
         # nacos部署IP
         server-addr: tofly-nacos:8848
         # 配置后缀