4.shiro的加密和解密

shiro的加密和解密

1.PasswordService 和CredentialsMatcher

1.Shiro 提供了 PasswordService 及 CredentialsMatcher 用于提供加密密码及验证密码服务。
2.Shiro 默认提供了 PasswordService 实现 DefaultPasswordService;CredentialsMatcher 实现 PasswordMatcher 及 HashedCredentialsMatcher(更强大,重点掌握)。

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

2测试

2.1使用DefaultPasswordService进行加密和解密

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

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

#credentialsMatcher配置也可以使用jdbcRealm
myRealm=com.github.shiro.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 异常。

2.2使用HashedCredentialsMatcher进行加密和解密

[main]
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
credentialsMatcher.hashAlgorithmName=md5
credentialsMatcher.hashIterations=2
credentialsMatcher.storedCredentialsHexEncoded=true

myRealm=com.github.shiro.hash.realm.MyRealm
myRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$myRealm

3.密码重试次数限制

实体类

public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher {
    private Ehcache passwordRetryCache;
    public RetryLimitHashedCredentialsMatcher() {
        CacheManager cacheManager = CacheManager.newInstance(CacheManager.class.getClassLoader().getResource("ehcache.xml"));
        passwordRetryCache = cacheManager.getCache("passwordRetryCache");
    }
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        String username = (String)token.getPrincipal();
        //retry count + 1
        Element element = passwordRetryCache.get(username);
        if(element == null) {
            element = new Element(username , new AtomicInteger(0));
            passwordRetryCache.put(element);
        }
        AtomicInteger retryCount = (AtomicInteger)element.getObjectValue();
        if(retryCount.incrementAndGet() > 5) {
            //if retry count > 5 throw
            throw new ExcessiveAttemptsException();
        }

        boolean matches = super.doCredentialsMatch(token, info);
        if(matches) {
            //clear retry count
            passwordRetryCache.remove(username);
        }
        return matches;
    }
}
。在收到退出通知后,其他股东有 [设定期限] 的时间来行使优先收购权这个异常是Shiro框架中的加密操作抛出的异常,表示在执行加密解密操作时出。 b. 若其他股东放弃优先收购权或未在设定期限内行使,退出股东现问题。 这个异常的原因可能是以下之一: 1. 密钥不匹配:执行加密解密可以向第三方出售其股权。 6. 争议解决: a. 任何因本协议操作时,可能使用了不正确的密钥。请确保在加密解密过程中使用相同的密引起的争议,应通过友好协商解决。如协商不成,应提交至 [仲裁机构钥。 2. 算法不支持:执行加密解密操作时,可能使用了不支持的加密算/仲裁委员会] 进行仲裁。 b. 仲裁裁决是终局的,对各方均法。请确保使用的加密算法是Java密码提供程序支持的。 3. 输入数据错误:执行加密解密操作时,可能传入了无效的输入数据。请确保输入数据符合预期的格式和有约束力。 7. 其他条款: a. 本协议的任何修改或补充应经过要求。 4. 加密库配置问题:如果您在使用Shiro框架之前进行了一些自定义的加三方书面同意并签署。 b. 本协议对三方均具有约束力,在协议密库配置,可能会导致此异常。请检查您的加密库配置是否正确,并与Shiro框架的签署后生效。 本协议签署地点:[协议签署地点] 本协议签署日期:要求相匹配。 要解决此问题,您可以采取以下步骤: 1. 检查密钥:确保[协议签署日期] 甲方:________________ 乙方:________________ 丙方:________________ 请注意,这只是一个在加密解密过程中使用相同的密钥,并且密钥是正确的。 2. 确认算法示例,具体的对赌协议内容应根据公司的具体情况进行调整和修改。建议在支持:查看您所使用的加密算法是否受Java密码提供程序支持。您可以查看Java密码起草和签署之前咨询法律专业人士以确保合规性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值