|
@ -2,34 +2,43 @@ package com.win.module.system.util; |
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64; |
|
|
import cn.hutool.core.codec.Base64; |
|
|
import cn.hutool.json.JSONObject; |
|
|
import cn.hutool.json.JSONObject; |
|
|
|
|
|
import com.win.module.system.mq.message.licences.LicencesMessage; |
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
import javax.crypto.Cipher; |
|
|
import javax.crypto.Cipher; |
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
@Component |
|
|
public class ModuleAuthenUtils { |
|
|
public class ModuleAuthenUtils { |
|
|
public static final String LICENCES_REDIS_KEY = "system.licences"; |
|
|
public static final String LICENCES_REDIS_KEY = "system.licences"; |
|
|
public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message"; |
|
|
public static final String LICENCES_UPDATE_MESSAGE = "system.licences.message"; |
|
|
public static final String ALGORITHM = "AES"; |
|
|
public static final String ALGORITHM = "AES"; |
|
|
public static String secretKey; |
|
|
public static String secretKey; |
|
|
public static Map<String,String> module; |
|
|
public static Map<String,String> module; |
|
|
public static String decrypt(String encryptedData,String secretKey) throws Exception { |
|
|
@Resource RedisTemplate redisTemplate; |
|
|
|
|
|
public String decrypt(String encryptedData,String secretKey) throws Exception { |
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); |
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); |
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); |
|
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); |
|
|
byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData)); |
|
|
byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedData)); |
|
|
return new String(decryptedBytes, StandardCharsets.UTF_8); |
|
|
return new String(decryptedBytes, StandardCharsets.UTF_8); |
|
|
} |
|
|
} |
|
|
public static String encrypt(String data,String secretKey) throws Exception { |
|
|
public String encrypt(String data,String secretKey) throws Exception { |
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); |
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decode(secretKey), ALGORITHM); |
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
Cipher cipher = Cipher.getInstance(ALGORITHM); |
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
|
|
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
|
|
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
|
|
return Base64.encode(encryptedBytes); |
|
|
return Base64.encode(encryptedBytes); |
|
|
} |
|
|
} |
|
|
public static void updateLicences(RedisTemplate<String, String> redisTemplate){ |
|
|
public void updateLicences(LicencesMessage ...message){ |
|
|
|
|
|
if(message != null){ |
|
|
|
|
|
ModuleAuthenUtils.secretKey = message[0].getSecretKey(); |
|
|
|
|
|
ModuleAuthenUtils.module = message[0].getData(); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
JSONObject json = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_REDIS_KEY)); |
|
|
JSONObject json = new JSONObject(redisTemplate.opsForValue().get(ModuleAuthenUtils.LICENCES_REDIS_KEY)); |
|
|
ModuleAuthenUtils.secretKey = json.getStr("secretKey"); |
|
|
ModuleAuthenUtils.secretKey = json.getStr("secretKey"); |
|
|
ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class); |
|
|
ModuleAuthenUtils.module = json.getJSONObject("data").toBean(Map.class); |
|
|