保证数据的安全性用AES加密的。token进行身份认证(通过JwtUtil生产)
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
@Slf4j
public class JwtUtil {
private final static String securt = "fasdfasdfasdfas";
public static String sign() {
//5分钟过期
Date date = new Date(System.currentTimeMillis() + 15 * 60 * 1000);
Algorithm algorithm = Algorithm.HMAC256(securt);
// 附带username信息
return JWT.create()
.withExpiresAt(date)
.withIssuedAt(new Date())
.sign(algorithm);
}
public static boolean verify(String token) {
try {
// 根据密码生成JWT效验器
Algorithm algorithm = Algorithm.HMAC256(securt);
com.auth0.jwt.JWTVerifier verifier = JWT.require(algorithm).build();
// 效验TOKEN
DecodedJWT jwt = verifier.verify(token);
log.info(jwt + ":-token is valid");
return true;
} catch (Exception e) {
log.error("The token is invalid {}", e.getMessage());
return false;
}
}
}
参数加密
import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String ASSETS_DEV_PWD_FIELD = "c81b7d38b57d7dbabcd3c38dc37446f4";
public static String getAssetsDevPwdField() {
return ASSETS_DEV_PWD_FIELD;
}
public static String encrypt(String content) {
try {
// 创建密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = content.getBytes("utf-8");
// 初始化为加密模式的密码器
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(AESUtil.ASSETS_DEV_PWD_FIELD.getBytes(), "AES"));
// 加密
byte[] result = cipher.doFinal(byteContent);
//通过Base64转码返回
return Base64Utils.encodeToString(result);
} catch (Exception ex) {
Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public static String decrypt(String content) {
try {
//实例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(AESUtil.ASSETS_DEV_PWD_FIELD.getBytes(), "AES"));
//执行操作
byte[] result = cipher.doFinal(Base64Utils.decodeFromString(content));
String s = new String(result, "utf-8");
return s;
} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
// public static void main(String[] args) {
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("name","45gtdh");
// jsonObject.put("age","88");
// String encrypt = AESUtil.encrypt(jsonObject.toJSONString());
// System.out.println(encrypt);
// }
}



