• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

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);//注入凭证匹配器
记得快乐
posted @ 2020-11-08 11:26  Y-wee  阅读(217)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3