通过单例生成公钥和私钥(单例生成的好处除了优化程序外,也可以防止多IP登录造成干扰),前台只接受明文,由redis保存,每次登录失败、退出等只要是跳转到登录页面的都要将明文传过来。
在这里封装成一个方法:getRsaKey()
public RSAPrivateKey getRsaKey() throws NoSuchAlgorithmException{
//判断redis中有就不再生成,没有就再生成一次
RSAPrivateKey key = RedisCluster.use().get("privateKey");
if(key==null){
HashMap<String, Object> map = RSAUtils.getKeys();
//生成公钥和私钥
RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
//私钥保存在session中,用于解密
//公钥信息保存在页面,用于加密
String publicKeyExponent = publicKey.getPublicExponent().toString(16);
String publicKeyModulus = publicKey.getModulus().toString(16);
RedisCluster.use().set("privateKey", privateKey);
RedisCluster.use().set("publicKeyExponent", publicKeyExponent);
RedisCluster.use().set("publicKeyModulus", publicKeyModulus);
}
return null;
}
在加载登录页面前执行该方法:
public void index() throws NoSuchAlgorithmException {
this.getRsaKey();
JSession jsession = getJSession();
logger.info("jsession=" + jsession);
logger.info("USER_FLAG=" + jsession.getAttribute(BaseConst.USER_FLAG));
if (jsession == null
|| jsession.ge