ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Shiro 基础认证实现

2021-10-29 17:32:59  阅读:153  来源: 互联网

标签:defaultSecurityManager realm 基础 认证 hashedCredentialsMatcher new principal Shiro 


//Shiro 认证基本实现流程

public class TestCustomerMD5 { public static void main(String[] args) {       //创建安全管理器对象 DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
      //创建 自定义 realm CustomerMD5Realm realm = new CustomerMD5Realm(); //设置realm 使用hash凭证匹配器 HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); //使用算法 hashedCredentialsMatcher.setHashAlgorithmName("md5"); //散列次数 hashedCredentialsMatcher.setHashIterations(2048); realm.setCredentialsMatcher(hashedCredentialsMatcher); defaultSecurityManager.setRealm(realm); //将安全管理器注入安全工具 SecurityUtils.setSecurityManager(defaultSecurityManager); //通过安全工具类获取subject Subject subject = SecurityUtils.getSubject(); //认证 UsernamePasswordToken token = new UsernamePasswordToken("xiaochen", "48694869");
try { subject.login(token); System.out.println(subject.isAuthenticated()); System.out.println("登录成功"); } catch (AuthenticationException e) { e.printStackTrace(); } } }

 

public class CustomerMD5Realm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

  //调用login方法将进入该方法 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    //传递token对象,获取用户名 String principal = (String) authenticationToken.getPrincipal();       //模拟jdbc mybatis
if("xiaochen".equals(principal)){         //用户名正确进来,创建对象,切把查询到的username,password,salt,传递进去。 //参数1:数据库名字 参数2:数据库md5+salt之后的密码, 参数3:salt 参数4:realm名称 SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, "8b43662b349c05514580fd738e8ff382", ByteSource.Util.bytes("islove*021"), //如果有盐校验时默认加上 this.getName()); return simpleAuthenticationInfo;    //这里返回会去自动校验密码,这里的方法是查询name传递密码数据 } return null; } }

 

标签:defaultSecurityManager,realm,基础,认证,hashedCredentialsMatcher,new,principal,Shiro,
来源: https://www.cnblogs.com/hylr/p/15481501.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有