第五讲 散列算法(加密算法)

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

2、测试MD5案例

 1 package com.sun123.shiro;
 2 
 3 import org.apache.shiro.crypto.hash.Md5Hash;
 4 import org.apache.shiro.crypto.hash.SimpleHash;
 5 
 6 public class Md5Demo {
 7 
 8     public static void main(String[] args) {
 9         //使用MD5加密算法 加密
10         Md5Hash md5 = new Md5Hash("1111");
11         System.out.println("1111=="+md5.toString());
12         
13         //加盐
14         md5 = new Md5Hash("1111","sxt");
15         System.out.println("1111=="+md5.toString());
16         
17         //迭代次数
18         md5 = new Md5Hash("1111","sxt",2);
19         System.out.println("1111=="+md5.toString());
20         
21         SimpleHash hash = new SimpleHash("md5","1111","sxt",2);
22         System.out.println("1111=="+hash.toString());
23     }
24 }

  运行结果:

  

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

  Realm的实现:

 1 package com.sun123.realm;
 2 
 3 import org.apache.shiro.authc.AuthenticationException;
 4 import org.apache.shiro.authc.AuthenticationInfo;
 5 import org.apache.shiro.authc.AuthenticationToken;
 6 import org.apache.shiro.authc.SimpleAuthenticationInfo;
 7 import org.apache.shiro.authz.AuthorizationInfo;
 8 import org.apache.shiro.realm.AuthorizingRealm;
 9 import org.apache.shiro.subject.PrincipalCollection;
10 import org.apache.shiro.util.ByteSource;
11 
12 public class UserRealm extends AuthorizingRealm {
13 
14     /**
15      * 自定义realm的实现    该realm类提供了两个方法
16      * doGetAuthorizationInfo    获取认证信息
17      * doGetAuthenticationInfo    获取权限信息
18      */
19     @Override
20     public String getName() {
21         // 自定义
22         return "userRealm";
23     }
24 
25     // 完成身份认证,并且返回认证信息
26     // 如果身份认证失败,返回null
27     @Override
28     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
29         // 获取用户输入的用户名
30         String username = (String) token.getPrincipal();// 获取身份信息
31         System.out.println("username:" + username);
32         // 根据用户名到数据库查询密码信息——模拟
33         // 假定从数据库获取的密码为1111和盐值
34         String pwd = "e41cd85110c7533e3f93b729b25235c3";
35         String salt = "sxt";
36         // 将从数据库中查询的信息封装到SimpleAuthenticationInfo中
37         SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, pwd,ByteSource.Util.bytes(salt),getName());
38         return info;
39     }
40 
41     // 授权的信息
42     @Override
43     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
44         // TODO Auto-generated method stub
45         return null;
46     }
47 
48 }

shiro.ini

1 [main]
2 credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
3 credentialsMatcher.hashAlgorithmName=md5
4 credentialsMatcher.hashIterations=2
5 userRealm=com.sun123.realm.UserRealm
6 userRealm.credentialsMatcher=$credentialsMatcher
7 securityManager.realm=$userRealm

 

转载于:https://www.cnblogs.com/116970u/p/10952763.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值