以下内容是在看了涛哥的《跟我一起学shiro》 和 视频《一头扎入进shiro》 后整理出来备忘和方便自己和其他人学习。
个人主页:http://www.itit123.cn/ 更多干货等你来拿
shiro加密
Base64加密:
package com.shiro.utils;
import org.apache.shiro.codec.Base64;
public class CryptographyUtil {
/**
* base64 加密
* @param string
* @return
*/
public static String encBase64(String string) {
return Base64.encodeToString(string.getBytes());
}
/**
* Base64 解密
* @param string
* @return
*/
public static String decBase64(String string) {
return Base64.decodeToString(string);
}
public static void main(String[] args) {
String password = "ITDragon博客";
String encBase64 = encBase64(password);
String decBase64 = decBase64(encBase64);
System.out.println("加密密码: " + encBase64 + " \n解密密码: " + decBase64);
}
}
这样不好,因为容易被破解
md5加密:
加盐加密,不可逆,如果对方不知道盐,是很难破解的。推荐使用
/**
* md5 加密
* @param string
* @param salt
* @return
*/
public static String md5(String string , String salt) {
return new Md5Hash(string, salt).toString();
}
实际开发中:保存在用户表中的密码是加密后的密码,这样可以保障数据的安全性。用户输入的密码为password;而保存到数据库的密码为saltPassword。但用户访问时,将password进行md5加盐加密后与数据库中的saltPassword匹配,若一致则登入。