Shiro-----散列算法(加密算法)

散列算法

3.在自定义的Realm中使用散列算法

package com.kennosaur.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;

public class UserRealm extends AuthorizingRealm{
	@Override
	public String getName() {		
		return "userrealm";
	}

	/**
	 * 认证信息
	 * 完成身份认证并且返回认证信息
	 * 如果身份认证失败返回null
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取身份信息
		String username = (String)token.getPrincipal();
		System.out.println("username======"+username);
		//根据用户名到数据库中查询密码信息----模拟
		//假定从数据库中获取的密码为123

		String password = "5dbf2d01bf694d7c218c1ea456c51241";
		String salt = "kennosaur";
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password,ByteSource.Util.bytes(salt), getName());
		return info;
	}

	/**
	 * 授权信息
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}
}

配置文件

#自定义Realm
userRealm=com.kennosaur.realm.UserRealm
#配置散列算法
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2
userRealm.credentialsMatcher=$credentialsMatcher
#配置验证器
securityManager.realms=$userRealm

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值