Java Web 登录采用非对称加密(RSA算法)+单例模式+redis+登录锁定5次

本文介绍了一种Java Web应用中使用RSA非对称加密进行登录验证的方法,结合单例模式确保公钥和私钥的安全生成,并利用Redis存储及管理。登录失败达到5次将进行锁定,确保系统安全。
摘要由CSDN通过智能技术生成

通过单例生成公钥和私钥(单例生成的好处除了优化程序外,也可以防止多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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值