本事例:用到的jar 文件
commons-codec-1.3.jar
spring-security-core-2.0.5.RELEASE.jar
spring security 加密
package com.tht.md5;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
public class THTMD5PasswordEncoder {
/**
* @param args
*/
public static void main(String[] args) {
Md5PasswordEncoder md5=new Md5PasswordEncoder();
/*
* koala<user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
emu <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
wombat <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
opal <user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER_S" />
<user name="think" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER_S" />
* */
/*
* "a" 是key
* "abc" 密码
* 9af7268244164521c43624a92ea963ac 加密后的字节串
*/
//String md5Password=md5.encodePassword("a", "abc");rod
String md5Password=md5.encodePassword("koala", null);
System.out.println("koala:"+md5Password);
md5Password=md5.encodePassword("emu", null);
System.out.println("emu:"+md5Password);
md5Password=md5.encodePassword("wombat", null);
System.out.println("wombat:"+md5Password);
md5Password=md5.encodePassword("opal", null);
System.out.println("opal:"+md5Password);
}
}
输出
koala:a564de63c2d0da68cf47586ee05984d7
emu:65d15fe9156f9c4bbffd98085992a44e
wombat:2b58af6dddbd072ed27ffc86725d7d3a
opal:22b5c9accc6e1ba628cedc63a72d57f8
spring security 密码验证
package com.tht.md5;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
public class THTPasswordValid {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Md5PasswordEncoder md5=new Md5PasswordEncoder();
/*
* koala<user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
emu <user name="dianne" password="65d15fe9156f9c4bbffd98085992a44e" authorities="ROLE_USER,ROLE_TELLER" />
wombat <user name="scott" password="2b58af6dddbd072ed27ffc86725d7d3a" authorities="ROLE_USER" />
opal <user name="peter" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER_S" />
<user name="think" password="22b5c9accc6e1ba628cedc63a72d57f8" authorities="ROLE_USER_S" />
* */
/**
* a564de63c2d0da68cf47586ee05984d7 加密后的串
* koala 密码
*/
boolean b=md5.isPasswordValid("a564de63c2d0da68cf47586ee05984d7", "koala", null);
System.out.println(b);
b=md5.isPasswordValid("65d15fe9156f9c4bbffd98085992a44e", "emu", null);
System.out.println(b);
b=md5.isPasswordValid("2b58af6dddbd072ed27ffc86725d7d3a", "wombat", null);
System.out.println(b);
}
}
输出
true
true
true