使用Apache Shiro进行身份认证-密码加密
来源:互联网 发布:linux ssh 指定目录 编辑:程序博客网 时间:2024/06/06 21:23
在进行身份认证时,用户的密码一般是用密文的形式存放在数据库中。这样在做比对时不能直接使用用户提交的明文口令。
在Shiro中使用org.apache.shiro.authc.credential.SimpleCredentialsMatcher做凭证信息的比对。SimpleCredentialsMatcher实现了
凭证信息的明文比对,即对凭证信息不做任何处理。
通过HashedCredentialsMatcher子类的扩展Shiro支持一些标准的加密算法,如MD5 和 SHA-1。
在使用时也可以根据自身需求定义自己的加密算法。下面是我实现的凭证信息比对类,加密算法是在网上找的DES算法。
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {@Overridepublic boolean doCredentialsMatch(AuthenticationToken token,AuthenticationInfo info) {Object tokenCredentials = encrypt(toBytes(token.getCredentials()));Object accountCredentials = getCredentials(info);return equals(tokenCredentials, accountCredentials);}private byte[] encrypt(byte[] data) {try {byte[] key = "11111111".getBytes();// DES算法要求有一个可信任的随机数源SecureRandom sr = new SecureRandom();// 从原始密钥数据创建DESKeySpec对象DESKeySpec dks = new DESKeySpec(key);// 创建一个密匙工厂,然后用它把DESKeySpec转换成// 一个SecretKey对象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey secretKey = keyFactory.generateSecret(dks);// using DES in ECB modeCipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 用密匙初始化Cipher对象cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);// 执行加密操作byte encryptedData[] = cipher.doFinal(data);return encryptedData;} catch (Exception e) {System.err.println("DES算法,加密数据出错!");e.printStackTrace();}return null;}}
使用时在Shiro.ini文件中加上这个类:
[main]customMatcher = main.java.name.peter.shiro.realm.CustomCredentialsMatcherjdbcRealm.credentialsMatcher = $customMatcher
- 使用Apache Shiro进行身份认证-密码加密
- 使用Apache Shiro进行身份认证
- 使用Apache Shiro进行身份认证-LDAP认证
- 使用Apache Shiro进行身份认证-LDAP两次绑定认证
- 使用Apache Shiro进行身份认证-Active Directory认证
- 使用Apache Shiro进行身份认证-多数据源认证
- 使用Apache Shiro进行身份认证-LDAP认证
- 使用Apache Shiro进行身份认证-proxool配置
- Apache Shiro 身份认证例子
- Apache Shiro 身份认证例子
- 使用Apache Shiro进行java认证说明
- Apache shiro 笔记整理之身份认证
- Apache Shiro身份认证(Authentication)
- 2 Apache Shiro 身份认证(登录)
- 【Shiro】Apache Shiro架构之身份认证(Authentication)
- 【Shiro】Apache Shiro架构之身份认证(Authentication)
- 【Shiro】Apache Shiro架构之身份认证(Authentication)
- HTTPS接口加密和身份认证 使用AFNetworking进行双向认证
- labview生成安装程序前的路径问题
- 修改upload 按钮 样式
- Zen Cart 二次开发学习笔记(一)
- Android 动画框架详解,第 1 部分
- iOS 录音功能的实现
- 使用Apache Shiro进行身份认证-密码加密
- Windows在Eclipse和MyEclipse中离线搭建Android开发环境
- 今天开始学习c++&java
- Android APK签名对比及说明
- iOS Framework 制作教程
- 查询日期范围时SQL条件的写法
- Eclipse中查看Android源代码
- python socket错误描述
- Android中SharedPreferences和Properties的一些组合使用