1.建立MD5的接口
public interface Md5Pwd {
public String? encode(String password);
}
2.建立MD5的實(shí)現(xiàn)類
/**
* Md5加密
*
*
*/
public class Md5PwdImpl implements Md5Pwd{
//加密
public String? encode(String password){
String algorithm = “MD5”;
//加鹽所謂加鹽就是在用戶密碼的基礎(chǔ)上按照某一規(guī)則添加不規(guī)律的字符
MessageDigest instance = null;
try {
instance = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//加密
byte[] digest = instance.digest(password.getBytes());
//十六進(jìn)制加密
char[] encodeHex = Hex.encodeHex(digest);
return new String(encodeHex);
}
}
3.測(cè)試一下
//測(cè)試一下Md5
public static void main(String[] args) {
Md5PwdEncoder m = new Md5PwdEncoder();
String encoder = m.encoder(“123456”);
System.out.println(encoder);
}
4.加入或創(chuàng)建utils.xml
5.Controller中使用
@Autowired
private Md5Pwd md5Pwd;