• Shiro简单加密服务


    编码/加密

    在涉及到密码存储问题上,应该加密 / 生成密码摘要存储,而不是存储明文密码。比如之前的 600w csdn 账号泄露对用户可能造成很大损失,因此应加密 / 生成不可逆的摘要方式存储。

    Shiro简单加密服务PasswordService/CredentialsMatcher

    Shiro 提供了 PasswordService 及 CredentialsMatcher 用于提供加密密码及验证密码服务。

    public interface PasswordService {
        //输入明文密码得到密文密码
        String encryptPassword(Object plaintextPassword) throws IllegalArgumentException;
    }
    public interface CredentialsMatcher {
        //匹配用户输入的token的凭证(未加密)与系统提供的凭证(已加密)
        boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info);
    }

    Shiro 默认提供了 PasswordService 实现 DefaultPasswordService;CredentialsMatcher 实现 PasswordMatcher 及 HashedCredentialsMatcher(更强大)。

    DefaultPasswordService 配合 PasswordMatcher 实现简单的密码加密与验证服务

    1、定义 Realm(com.github.zhangkaitao.shiro.chapter5.hash.realm.MyRealm)

    public class MyRealm extends AuthorizingRealm {
        private PasswordService passwordService;
        public void setPasswordService(PasswordService passwordService) {
            this.passwordService = passwordService;
        }
         //省略doGetAuthorizationInfo,具体看代码 
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            return new SimpleAuthenticationInfo(
                    "wu",                passwordService.encryptPassword("123"),
                    getName());
        }
    }

    为了方便,直接注入一个 passwordService 来加密密码,实际使用时需要在 Service 层使用 passwordService 加密密码并存到数据库。

    2、ini 配置(shiro-passwordservice.ini)

    [main]
    passwordService=org.apache.shiro.authc.credential.DefaultPasswordService
    hashService=org.apache.shiro.crypto.hash.DefaultHashService
    passwordService.hashService=$hashService
    hashFormat=org.apache.shiro.crypto.hash.format.Shiro1CryptFormat
    passwordService.hashFormat=$hashFormat
    hashFormatFactory=org.apache.shiro.crypto.hash.format.DefaultHashFormatFactory
    passwordService.hashFormatFactory=$hashFormatFactory
    passwordMatcher=org.apache.shiro.authc.credential.PasswordMatcher
    passwordMatcher.passwordService=$passwordService
    myRealm=com.github.zhangkaitao.shiro.chapter5.hash.realm.MyRealm
    myRealm.passwordService=$passwordService
    myRealm.credentialsMatcher=$passwordMatcher
    securityManager.realms=$myRealm
    • passwordService 使用 DefaultPasswordService,如果有必要也可以自定义;

    • hashService 定义散列密码使用的 HashService,默认使用 DefaultHashService(默认 SHA-256 算法);

    • hashFormat 用于对散列出的值进行格式化,默认使用 Shiro1CryptFormat,另外提供了 Base64Format 和 HexFormat,对于有 salt 的密码请自定义实现 ParsableHashFormat 然后把 salt 格式化到散列值中;

    • hashFormatFactory 用于根据散列值得到散列的密码和 salt;因为如果使用如 SHA 算法,那么会生成一个 salt,此 salt 需要保存到散列后的值中以便之后与传入的密码比较时使用;默认使用 DefaultHashFormatFactory;

    • passwordMatcher 使用 PasswordMatcher,其是一个 CredentialsMatcher 实现;

    • 将 credentialsMatcher 赋值给 myRealm,myRealm 间接继承了 AuthenticatingRealm,其在调用 getAuthenticationInfo 方法获取到 AuthenticationInfo 信息后,会使用 credentialsMatcher 来验证凭据是否匹配,如果不匹配将抛出 IncorrectCredentialsException 异常。




    查看原文:http://www.coder306.cn/?p=205
  • 相关阅读:
    thinkphp中的验证码的实现
    js深入研究之牛逼的类封装设计
    js深入研究之函数内的函数
    js深入研究之初始化验证
    js深入研究之Person类案例
    js深入研究之匿名函数
    js深入研究之类定义与使用
    sublime text3 自动编译php 适合用于简单的php文件执行
    PHP实现四种基本排序算法 得多消化消化
    thinkphp中的类库与引用import引入机制
  • 原文地址:https://www.cnblogs.com/coder306/p/13087674.html
Copyright © 2020-2023  润新知