1、Shiro 提供了base64和16进制字符串编码/解码的API支持:java
String str = "h"; //base64 编码 String base64Encoded = Base64.encodeToString(str.getBytes()); //base64 编码解码 String str2 = Base64.decodeToString(base64Encoded); System.out.println(str.equals(str2)); String str3 = "y"; //16进制字符串编码 String hexEncoded = Hex.encodeToString(str3.getBytes()); //16进制字符串解码 String str4 = new String(Hex.decode(hexEncoded)); System.out.println(str3.equals(str4)); //byte数组与String转换 CodecSupport.toBytes(str, "utf-8"); CodecSupport.toString(str.getBytes(), "utf-8");
2、散列算法算法
散列算法通常用于生成数据的摘要信息,是一种不可逆的算法,通常适合存储密码之类的数据,常见的散列算法如MD五、SHA等。数据库
String str = "h"; String salt = "123"; //盐 //单纯md5加密,易破解 System.out.println(new Md5Hash(str).toString()); //加干扰数据,即盐 System.out.println(new Md5Hash(str,salt).toString()); //指定散列次数,如2次::md5(md5(str)) System.out.println(new Md5Hash(str,salt,2).toString()); //SHA256 算法 还有如SHA一、SHA512算法 System.out.println(new Sha256Hash(str,salt).toString()); //shiro通用散列支持,内部使用了Java 的MessageDigest实现 System.out.println(new SimpleHash("md5",str,salt).toString());
Shiro 提供了HashService,默认提供了DefaultHashService实现数组
//建立一个DefaultHashService,默认使用SHA-512 算法; DefaultHashService hashService = new DefaultHashService(); //修改算法 hashService.setHashAlgorithmName("md5"); //设置一个私盐,其在散列时自动与用户传入的公盐混合产生一个新盐 hashService.setPrivateSalt(new SimpleByteSource("123")); //在用户没有传入公盐的状况下是否生成公盐,默认false hashService.setGeneratePublicSalt(true); //用于生成公盐 hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator()); //修改默认加密迭代次数 hashService.setHashIterations(2); //构建一个HashRequest,传入算法、数据、公盐、迭代次数。 HashRequest request = new HashRequest.Builder().setAlgorithmName("md5") .setSource(ByteSource.Util.bytes("h")) .setSalt(ByteSource.Util.bytes("123")) .setIterations(2).build(); System.out.println(hashService.computeHash(request).toString()); //SecureRandomNumberGenerator用于生成一个随机数 SecureRandomNumberGenerator srng = new SecureRandomNumberGenerator(); srng.setSeed("123".getBytes()); System.out.println(srng.nextBytes().toHex());
3、加密、解密dom
Shiro 还提供对称式加密/解密算法的支持,如AES、Blowfish 等ui
//AES算法 AesCipherService acs = new AesCipherService(); acs.setKeySize(128);//key长度 Key key = acs.generateNewKey();//生成key String str = "h"; //加密 String encrptText = acs.encrypt(str.getBytes(), key.getEncoded()).toHex(); System.out.println(encrptText); //解密 String str2 = new String(acs.decrypt(Hex.decode(encrptText), key.getEncoded()).getBytes()); System.out.println(str2);
4、PasswordService/CredentialsMatcherthis
Shiro 提供了PasswordService及CredentialsMatcher用于提供加密密码及验证密码服务。编码
public interface PasswordService { //输入明文密码获得密文密码 String encryptPassword(Object plaintextPassword) throws IllegalArgumentException; }
public interface CredentialsMatcher { //匹配用户输入的token 的凭证(未加密)与系统提供的凭证(已加密) boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info); }
Shiro 默认提供了PasswordService 实现DefaultPasswordService;CredentialsMatcher 实现PasswordMatcher及HashedCredentialsMatcher加密
简单使用:code
service层加密密码保存到数据库
//加密密码 user.setPassword(passwordService.encryptPassword(user.getPassword()));
登陆时realm验证
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( account,user.getPassword(),this.getName()); System.out.println("匹配=>"+credentialsMatcher.doCredentialsMatch(token,authenticationInfo)); if( !credentialsMatcher.doCredentialsMatch(token,authenticationInfo)){ throw new IncorrectCredentialsException(); }