shiro凭证配置

shiro凭证配置

1、方式一

在自定义realm的构造器中配置凭证匹配器对象

public UserRealm() {
        //凭证匹配器对象
        HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
        //指定加密算法
        matcher.setHashAlgorithmName("md5");
        //指定散列次数
        matcher.setHashIterations(2);
        
        setCredentialsMatcher(matcher);
    }

在用户认证时指定盐

ByteSource byteSource=ByteSource.Util.bytes("yl");
/**
 *用户认证
 *参数说明:
 * 参数1:可以是任意对象,作为用户身份
 * 参数2:数据库查询出来的密码
 * 参数3:盐 
 * 参数4:当前类名
 */
SimpleAuthenticationInfo info=newSimpleAuthenticationInfo(activeUser,user.getPassword(),byteSource,this.getName());

2、方式二

通过ini配置文件配置,指定盐的方式同上

[main]
# 创建凭证匹配器对象
matcher= org.apache.shiro.authc.credential.HashedCredentialsMatcher
matcher.hashAlgorithmName=md5 
matcher.hashIterations=2
# 创建userReaml对象
userRealm= com.yl.shiro.UserRealm
# 注入凭证匹配器到userReaml
userRealm.credentialsMatcher=$matcher
# 把userReaml注入安全管理器
securityManager.realm=$userRealm

3、方式三

注入自定义realm时指定凭证匹配器,指定盐的方式同上

UserRealm userRealm=new UserRealm();//自定义realm对象
HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();//凭证匹配器对象
matcher.setHashAlgorithmName("md5");//指定加密算法
matcher.setHashIterations(2);//指定散列次数
userRealm.setCredentialsMatcher(matcher);//注入凭证匹配器
记得快乐
原文地址:https://www.cnblogs.com/Y-wee/p/13943764.html