shiro(五)散列算法(加密算法)

1.在身份认证过程中往往会涉及加密。如果不加密那么数据部不安全。shiro内部实现了比较多的散列算法。如MD5,SHA等。并且提供了加盐功能,比如“1111”的MD5码为“b59c67bf196a4758191e42f76670ceba”,这个MD5码可以在很多破解网站上找到相对应的原密码。但是如果为“1111”+姓名 那么找到原密码的难度会增加。
2.测试MD5案例

package com.sxt.shiro;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

public class MD5Demo {
	public static void main(String[] args) {
		//使用MD5加密算法
		Md5Hash md5 = new Md5Hash("1111");
		System.out.println("1111===="+md5.toString());
		//加盐
		md5 = new Md5Hash("1111", "ldf");
		System.out.println("1111===="+md5.toString());
		//散列次数
		md5 = new Md5Hash("1111", "ldf",2);
		System.out.println("1111===="+md5.toString());
		SimpleHash hash = new SimpleHash("md5", "1111", "ldf", 2);
		System.out.println(hash.toString());
	}
}

3.在自定义的Realm中使用散列算法:
realm的实现:

package com.sxt.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
/**
 * 自定义realm的实现,该realm类提供了两个方法
 * doGetAuthenticationInfo获取认证信息
 * doGetAuthorizationInfo获取权限信息
 * @author Lee
 *
 */
public class UserRealm extends AuthorizingRealm{
	//授权的信息
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	//完成身份认证,并且返回认证信息
	//如果身份认证失败返回null
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		//获取用户输入的用户名
		String username = (String) token.getPrincipal();//获取身份信息
		System.out.println("username========="+username);
		//根据用户名到数据库查询密码信息-----模拟
		//假定从数据库中获取的密码为123和盐值
		String pwd = "1c7e3d68884894ebeb954f75f865fb80";
		String salt = "aaa";
		//将从数据库查询的信息封装到SimpleAuthenticationInfo中
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,ByteSource.Util.bytes(salt),getName());
		return info;
	}

}

配置文件
[main]
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2
userRealm=com.sxt.realm.UserRealm
userRealm.credentialsMatcher=$credentialsMatcher
securityManager.realm=$userRealm

测试类:

package com.sxt.shiro;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;

/**
 * 完成用户认证demo
 * @author Lee
 *
 */
public class UserNameDemo {
	public static void main(String[] args) {
		//1.创建获取 SecurityManager 工厂,读取相应配置文件
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//2.通过SecurityManager工厂获取SecurityManager的实例
		SecurityManager securityManager = factory.getInstance();
		//3.将SecurityManager对象设置到运行环境中
		SecurityUtils.setSecurityManager(securityManager);
		//4.通过SecurityManager主体获取Subject
		Subject subject = SecurityUtils.getSubject();
		//5.假如登录的用户名zhangsan和123,这个地方的zhangsan和123表示用户登录时输入的信息
		//而shiro.ini文件中信息相当于数据库中存放的用户信息
		UsernamePasswordToken token = new UsernamePasswordToken("aaa", "123");	
		try {
			//进行用户身份验证
			subject.login(token);
			//通过subject来判断用户是否通过验证
			if(subject.isAuthenticated()){
				System.out.println("用户登录成功!!!");
			}
		} catch (UnknownAccountException e) {
			System.out.println("用户名或密码不正确!!!!!!");
		}
		catch (IncorrectCredentialsException e) {
			System.out.println("用户名或密码不正确!!!!!!");
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值