AOP的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.project2.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
AOP类
package com.project2.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.project2.Beans.userBean;
@Aspect
@Component
public class armAspectAop {
armBeans armer=null;
public armAspectAop() {
armer=new armBeans();
}
//com.project2.aop.jdbcProcess.insertUser(userBean)
@Pointcut(value="execution(public * com.project2.aop.jdbcProcess.insertUser(..))")
public void fun1() {};
@Pointcut(value="execution(public * com.project2.aop.jdbcProcess.checkUser(..))")
public void fun2() {}
@Around(value="fun1()")
public boolean aroundFun1(ProceedingJoinPoint pjp) {
Object[] args=pjp.getArgs();
userBean bean=(userBean) args[0];
args[0]=armer.arm(bean);
try {
boolean flag=(boolean) pjp.proceed(args);
System.out.println("环绕函数执行完毕");
return flag;
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//@Around(value="fun2()")
public int aroundFun2(ProceedingJoinPoint pjp) {
Object[]args=pjp.getArgs();
userBean bean=(userBean) args[0];
args[0]=armer.disArm(bean);
try {
int i=(int) pjp.proceed(args);
System.out.println("环绕函数执行完毕");
return i;
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
RSA加密
package com.project2.aop;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import com.project2.Beans.userBean;
import com.project2.rsa.rsaMain;
@SuppressWarnings({"WeakerAccess", "unused"})
public class armBeans {
/**
* 缺省的1024位密钥对,可处理117个字节的数据
*/
private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3nL3e0ESJSALcXfS3uazX3z/VcepVDpjK/9D90PSYhvm2PIiOmchmxbXLTmg+PwikNMZTU/9OKrkVwBI13tF4ku1Lim6AnvalkDjOcbRfbcyuto8j0gAEYoKbnDAlxAK0tqGg7Q9Vt6Dobn/bjlfP52KDTbnKaUkf4dN4heoQcQIDAQAB";
private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALecvd7QRIlIAtxd9Le5rNffP9Vx6lUOmMr/0P3Q9JiG+bY8iI6ZyGbFtctOaD4/CKQ0xlNT/04quRXAEjXe0XiS7UuKboCe9qWQOM5xtF9tzK62jyPSAARigpucMCXEArS2oaDtD1W3oOhuf9uOV8/nYoNNucppSR/h03iF6hBxAgMBAAECgYAuYabI4R1e/FTUq9tEid+yjE+IsIT8RXqtVN/LLfyLhxUPRs1MEGbCJ1Fb8Qt+Jdgd+sLai85jRYGJkAJJn2aUPCRfLTZJl4KD5R4K2l6Xb5npgfBnoE1RYBd6sb2pwKHSFTeXx516pAAhaTjorF+nGzl9qig1xarRQunaKY0kwQJBANyJc/2Zx1V/3qzpXgCYM1fjCN6AXBh75FXBM7EFz+fU1RSi4Pzpbo3aLilobLG3MaKjrGYs2s9+3Z7KjXJFxjkCQQDVI0LT3RPAkCGGm2Gm+nbtLRkwdus32snJmWuca05LNuihzJIF/n9tB/JdzCRyU1bUHWK7W4Pt9v8DyXRXRlv5AkEAubmoTTJshLyjMst1ttp2otU4aul+pb/wk6eZeMOES6EZsb9Z6qRLznqn1YdzwladHnLohUPRIl2dFlVOuaW5QQJBAKg6fLTc+oozOUMVwa4aY2QxBIDMXDxp5HvLLLT5vYKAhH2xClDQ36Mfcp22U84pGY+y4gsf4LIjEoXarIl2b4ECQHU9JR6EZVmCQHKfHPOPauqRkRv81tXsWJSomJF1OfixUvRBDeDj/skme7tB1XxJGOLJGACSXDvqVvcAbRXN9oQ=";
/**
* 算法
*/
public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
public static final String RSA_ALGORITHM_NO_PADDING = "RSA";
/**
* 默认的密钥长度
*/
private static final Integer KEY_LENGTH = 1024;
/**
* 字符集
*/
private static final String CHARSET = "utf-8";
/**
* 签名算法
*/
private static final String SIGNATURE_INSTANCE = "SHA1WithRSA";
/**
* 获取默认公钥
*
* @return 默认公钥实例
* @throws Exception e
*/
public PublicKey getDefaultPublicKey() throws Exception {
return getPublicKey(PUBLIC_KEY);
}
/**
* 获取默认私钥
*
* @return 默认私钥实例
* @throws Exception e
*/
public PrivateKey getDefaultPrivateKey() throws Exception {
return getPrivateKey(PRIVATE_KEY);
}
/**
* 生成密钥对
*
* @param keyLength 密钥长度
*
* @return 密钥对
* @throws Exception e
*/
public static KeyPair getKeyPair(int keyLength) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM_NO_PADDING);
keyPairGenerator.initialize(keyLength);
return keyPairGenerator.generateKeyPair();
}
/**
* 生成默认长度的密钥对
*
* @return 密钥对
* @throws Exception e
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM_NO_PADDING);
keyPairGenerator.initialize(KEY_LENGTH);
return keyPairGenerator.generateKeyPair();
}
/**
* 公钥字符串转PublicKey实例
*
* @param publicKey 公钥字符串
*
* @return PublicKey实例
* @throws Exception e
*/
public static PublicKey getPublicKey(String publicKey) throws Exception {
// java8 的Base64.getDecoder()编码规范和java7不同,也和jsencrypt不兼容,因此要用Base64.getMimeDecoder()
byte[] publicKeyBytes = Base64.getMimeDecoder().decode(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM_NO_PADDING);
return keyFactory.generatePublic(keySpec);
}
/**
* 私钥字符串转PrivateKey实例
*
* @param privateKey 私钥字符串
*
* @return PrivateKey实例
* @throws Exception e
*/
public static PrivateKey getPrivateKey(String privateKey) throws Exception {
byte[] privateKeyBytes = Base64.getMimeDecoder().decode(privateKey.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM_NO_PADDING);
return keyFactory.generatePrivate(keySpec);
}
/**
* 公钥加密
*
* @param content 明文
* @param publicKey 公钥
*
* @return 密文
* @throws Exception e
*/
public static byte[] encryptByPublicKey(byte[] content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
/**
* 公钥加密
*
* @param content 明文
* @param publicKey 公钥字符串
*
* @return 密文
* @throws Exception e
*/
public static String encryptByPublicKey(String content, String publicKey) throws Exception {
return new String(Base64.getMimeEncoder().encode(
encryptByPublicKey(content.getBytes(CHARSET), getPublicKey(publicKey))));
}
/**
* 使用默认公钥加密-字节
*
* @param content 明文字节数组
*
* @return 密文字节数组
* @throws Exception e
*/
public static byte[] encryptByPublicKey(byte[] content) throws Exception {
return encryptByPublicKey(content, getPublicKey(PUBLIC_KEY));
}
/**
* 使用默认公钥加密-字符串
*
* @param content 明文字符串
*
* @return 密文字符串
* @throws Exception e
*/
public static String encryptByPublicKey(String content) throws Exception {
return new String(Base64.getMimeEncoder().encode(encryptByPublicKey(content.getBytes(CHARSET))));
}
/**
* 私钥解密
*
* @param content 密文
* @param privateKey 私钥
*
* @return 解密后的明文
* @throws Exception e
*/
public static byte[] decryptByPrivateKey(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
/**
* 使用私钥字符串解密
*
* @param content 密文
* @param privateKey 私钥字符串
*
* @return 明文
* @throws Exception e
*/
public static String decryptByPrivateKey(String content, String privateKey) throws Exception {
return new String(decryptByPrivateKey(
Base64.getMimeDecoder().decode(content), getPrivateKey(privateKey)), CHARSET);
}
/**
* 使用默认私钥解密-字节
*
* @param content 密文字节数组
*
* @return 明文字节数组
* @throws Exception e
*/
public static byte[] decryptByPrivateKey(byte[] content) throws Exception {
return decryptByPrivateKey(content, getPrivateKey(PRIVATE_KEY));
}
/**
* 使用默认私钥解密-字符串
*
* @param content 密文
*
* @return 明文
* @throws Exception e
*/
public static String decryptByPrivateKey(String content) throws Exception {
return new String(decryptByPrivateKey(Base64.getMimeDecoder().decode(content)), CHARSET);
}
/**
* 私钥加密
*
* @param content 明文
* @param privateKey 私钥
*
* @return 密文
* @throws Exception e
*/
public static byte[] encryptByPrivateKey(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
/**
* 私钥加密
*
* @param content 明文
* @param privateKey 私钥字符串
*
* @return 密文
* @throws Exception e
*/
public static String encryptByPrivateKey(String content, String privateKey) throws Exception {
return new String(Base64.getMimeEncoder().encode(
encryptByPrivateKey(content.getBytes(CHARSET), getPrivateKey(privateKey))), CHARSET);
}
/**
* 使用默认私钥加密-字节
*
* @param content 明文字节数组
* @return 密文字节数组
*
* @throws Exception e
*/
public static byte[] encryptByPrivateKey(byte[] content) throws Exception {
return encryptByPrivateKey(content, getPrivateKey(PRIVATE_KEY));
}
/**
* 使用默认私钥加密-字符串
*
* @param content 明文字符串
* @return 密文字符串
*
* @throws Exception e
*/
public static String encryptByPrivateKey(String content) throws Exception {
return new String(Base64.getMimeEncoder().encode(
encryptByPrivateKey(content.getBytes(CHARSET))), CHARSET);
}
/**
* 公钥解密
*
* @param content 密文
* @param publicKey 公钥
*
* @return 明文
* @throws Exception e
*/
public static byte[] decryptByPublicKey(byte[] content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
/**
* 公钥解密
*
* @param content 密文
* @param publicKey 公钥字符串
*
* @return 明文
* @throws Exception e
*/
public static String decryptByPublicKey(String content, String publicKey) throws Exception {
return new String(decryptByPublicKey(
Base64.getMimeDecoder().decode(content), getPublicKey(publicKey)), CHARSET);
}
/**
* 使用默认私钥解密-字节
*
* @param content 密文字节数组
*
* @return 明文字节数组
* @throws Exception e
*/
public static byte[] decryptByPublicKey(byte[] content) throws Exception {
return decryptByPublicKey(content, getPublicKey(PUBLIC_KEY));
}
/**
* 使用默认私钥解密-字符串
*
* @param content 密文字符串
*
* @return 明文字符串
* @throws Exception e
*/
public static String decryptByPublicKey(String content) throws Exception {
return new String(decryptByPublicKey(Base64.getMimeDecoder().decode(content)), CHARSET);
}
/**
* 签名
*
* @param content 明文
* @param privateKey 私钥
*
* @return 签名后的密文
* @throws Exception e
*/
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_INSTANCE);
signature.initSign(privateKey);
signature.update(content);
return signature.sign();
}
/**
* 签名
*
* @param content 明文
* @param privateKey 密钥字符串
*
* @return 签名后的密文
* @throws Exception e
*/
public static String sign(String content, String privateKey) throws Exception {
return new String(Base64.getMimeEncoder().encode(
sign(content.getBytes(CHARSET), getPrivateKey(privateKey))), CHARSET);
}
/**
* 使用默认私钥签名
*
* @param content 明文
*
* @return 签名后的密文
* @throws Exception e
*/
public static byte[] sign(byte[] content) throws Exception {
return sign(content, getPrivateKey(PRIVATE_KEY));
}
/**
* 使用默认私钥签名
*
* @param content 明文
*
* @return 签名后的密文
* @throws Exception e
*/
public static String sign(String content) throws Exception {
return new String(Base64.getMimeEncoder().encode(
sign(content.getBytes(CHARSET))), CHARSET);
}
/**
* 验签
*
* @param content 明文
* @param sign 签名
* @param publicKey 公钥
*
* @return true or false
* @throws Exception e
*/
public static boolean verify(byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_INSTANCE);
signature.initVerify(publicKey);
signature.update(content);
return signature.verify(sign);
}
/**
* 验签
*
* @param content 明文
* @param sign 签名
* @param publicKey 公钥字符串
*
* @return true or false
* @throws Exception e
*/
public static boolean verify(String content, String sign, String publicKey) throws Exception {
return verify(content.getBytes(CHARSET),
Base64.getMimeDecoder().decode(sign), getPublicKey(publicKey));
}
/**
* 使用默认公钥验签
*
* @param content 明文
* @param sign 签名
*
* @return true or false
* @throws Exception e
*/
public static boolean verify(byte[] content, byte[] sign) throws Exception {
return verify(content, sign, getPublicKey(PUBLIC_KEY));
}
/**
* 使用默认公钥验签
*
* @param content 明文
* @param sign 签名
*
* @return true or false
* @throws Exception e
*/
public static boolean verify(String content, String sign) throws Exception {
return verify(content.getBytes(CHARSET),
Base64.getMimeDecoder().decode(sign), getPublicKey(PUBLIC_KEY));
}
//公钥加密方法
public userBean arm(userBean bean) {
try {
//公钥
String pkStr="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBGA7uaFXjHVqQrFSF275xAPti+Hg/qUlLdg0kNuGj712C9RLyuzVfDdiK12Sh6ld8f/7nk3XynMO05ZHwHaMH5AIkwd7t9yoL/C1BXaBpvjehCPys1laBBYwVflnbm4cZC/ykIGaZFAp9JWmuGde6JrcfgKjBzSPYDEjIyCu51QIDAQAB";
String p=bean.getPassword();
// 公钥加密
String s = rsaMain.encryptByPublicKey(p, pkStr);
bean.setPassword(s);
}
catch (Exception e) {
e.printStackTrace();
}
return bean;
}
//密文解密的方法
public userBean disArm(userBean bean) {
try {
//密钥
String skStr="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIEYDu5oVeMdWpCsVIXbvnEA+2L4eD+pSUt2DSQ24aPvXYL1EvK7NV8N2IrXZKHqV3x//ueTdfKcw7TlkfAdowfkAiTB3u33Kgv8LUFdoGm+N6EI/KzWVoEFjBV+WdubhxkL/KQgZpkUCn0laa4Z17omtx+AqMHNI9gMSMjIK7nVAgMBAAECgYB4zBdktVLxA420ZN1uqHIq8f1J3jE/eNwx9ZRUd9pqeViqa0NAyTcm7CDdwrHS5SLaSDcxYQX0cQacplkKM+FR3+n+43kDgZgdlhp0i4hpgnZnegzD/MXJz3S4WCK0ybfzQg331gabIfsWAIQvI9fvXs40FHjKbtOtSy2ANhSlIQJBAP8y3zvyj7w+CCQTir6qVwzwxFMPGUtQmi103jWonbM1oGh6UKDfCK/SlFGTWvTPIaIiGsBt0UJfcgaysycWbF0CQQCBf9LdaqmzxUIluHZDVfK4fZxXqtFhrhBmaLYDcADcv2gB71Sla2Gmy9hDyrRX2FRcu/q96fe2E7Nb3YMsa2vZAkByV6TWA1SLj2+uWmMsEr52Odp9LKK0sCSkgVyexjICfeClb0h5C/WkccGq209BqRz0vDsoRQwhpQo+HgtPVCixAkAmvStCS0cq3QaVrCHMsOhkIkdjvljWhLCpQge47YeEgcnGUmca+Q2ETRSL6xaZ9L//4cf3igctM6WDBLfZ0OS5AkEA7Jtmwp9ezgVMjMrptYNgw5QiPq4X92tUsk2xK9qOAA8qFCOY+6R+0NjpObokG8GPWdtzWAfSGY/xr0SQXI3x5A==";
//String p=bean.getPassword();
String s=bean.getPassword();
// 密钥解密获得明文
String sp = rsaMain.decryptByPrivateKey(s, skStr);
//将明文写入POJO中
bean.setPassword(s);
// 签名
String name = "luerdan";
String nameSign = rsaMain.sign(name);
// 验签
System.out.println(rsaMain.verify(name, nameSign));
}
catch(Exception e) {
}
return bean;
}
}