spring security 加密和,密码验证例子

本事例:用到的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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值