使用Shiro的加密与解密实现简单用户注册与登录验证

编码与解码

Shiro 提供了 base64 和 16 进制字符串编码、解码的 API 支持,方便一些编码解码操作。Shiro 内部的一些数据的存储 、表示都使用了 base64 和 16 进制字符串。

1、base64 编码与解码

String str = "admin";

String base64Encoded = Base64.encodeToString(str.getBytes());

String str2 = Base64.decodeToString(base64Encoded);

logger.info(str+"<==="+base64Encoded+"==>"+str2);

2、16 进制字符串编码与解码

String str3 = "test";

String hexEncoded2 = Hex.encodeToString(str3.getBytes());

String str4 = new String(Hex.decode(hexEncoded2.getBytes()));

logger.info(str3+"<==="+hexEncoded2+"==>"+str4);

散列算法加密

散列算法一般用于生成数据的重要信息,是一种不可逆的算法,适合存储密码之类的数据,常见的散列算法如 MD5、SHA 等。

1、 MD5算法盐值加密

String pswd= "123456";

String salt = "123";

String md5 = new Md5Hash(pswd, salt).toString();

另外散列时还可以指定散列次数,new Md5Hash(pswd, salt, 2).toString(),数字2代表加密次数。

2、 SHA256 算法盐值加密

String pswd= "123456";

String salt = "123";

String sha = new Sha256Hash(pswd, salt).toString();

3、 SHA1算法盐值加密

String pswd= "123456";

String salt = "123";

String sha = new Sha1Hash(pswd, salt).toString();

4、SHA512算法盐值加密

String pswd = "123456";

String salt = "123";

String sha = new Sha512Hash(pswd, salt).toString();

5、 Shiro 还提供了通用的散列SimpleHash

String pswd = "123456";

String salt = "123";

//内部使用MessageDigest

String simpleHash=

new SimpleHash("SHA-1",pswd,salt,4).toString();

Shiro 还提供对称式加密、解密算法的支持

1、 AES 算法

//生成key

Key key = aesCipherService.generateNewKey();

String pswd = "123456";

//加密

String encrptText = aesCipherService.encrypt(pswd.getBytes(), key.getEncoded()).toHex();

//解密

String text2 =

new String(aesCipherService.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes());

logger.info(pswd+"<==="+encrptText+"==>"+text2);

简单模拟用户注册

1、首先使用SHA256算法加密密码明文,UserService层实现。

UserDaoImpl userDaoimpl ;

public static void main(String[] args) {

String email ="[email protected]";

String password = "123";

String sha = new Sha256Hash(password, ByteSource.Util.bytes(email)).toString();//使用邮箱作为盐值

User user = new User(4L,"管理员",email,sha,new Date(),new Date(),1);

UserService userService = new UserService();

userService.insert(user);

}

public int insert(User user){

userDaoimpl =new UserDaoImpl();

return userDaoimpl.insert(user);

}

2、Dao插入数据

@Override

public int insert(User record) {

String sql = "insert into u_user(id,nickname,email,pswd,create_time,last_login_time,status) values (?,?,?,?,?,?,?)";

return insert(sql,record.getId(),record.getNickname(),record.getEmail(),record.getPswd(),record.getCreateTime(),record.getLastLoginTime(),record.getStatus());

}

HashedCredentialsMatcher 实现密码验证服务

1、 自定义JdbcRealm实现AuthorizingRealm接口,重写doGetAuthenticationInfo(,,,)方法。

使用Shiro的加密与解密实现简单用户注册与登录验证

2、 配置ini文件内容

主要配置HashedCredentialsMatcher实现密码验证服务。

credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher


credentialsMatcher.hashAlgorithmName=SHA-256#加密算法


credentialsMatcher.hashIterations=1#迭代次数

credentialsMatcher.storedCredentialsHexEncoded=true

jdbcRealm=com.shiro.test.JdbcRealm

jdbcRealm.credentialsMatcher=$credentialsMatcher

securityManager.realms=$jdbcRealm

3、 用邮箱、密码实现登录认证

使用Shiro的加密与解密实现简单用户注册与登录验证

4、结果

使用Shiro的加密与解密实现简单用户注册与登录验证


分享到:


相關文章: