zoukankan      html  css  js  c++  java
  • ShiroINI配置及加密(三)

    Shiro InI 配置

    ini语法:

    1.对象名 = 全限定类名 相对于调用 public 无参构造器创建对象

    2.对象名. 属性名 = 值 相当于调用 setter 方法设置常量值

    3.对象名. 属性名 =$ 对象引用 相当于调用 setter 方法设置对象引用

    INI 配置

    [main] 部分

    提供了对根对象 securityManager 及其依赖对象的配置。

    创建对象

    securityManager=org.apache.shiro.mgt.DefaultSecurityManager

    其构造器必须是 public 空参构造器,通过反射创建相应的实例

    常量值 setter 注入

    dataSource.driverClassName=com.mysql.jdbc.Driver
    jdbcRealm.permissionsLookupEnabled=true;

    会自动调用 jdbcRealm.setPermissionsLookupEnabled(true),对于这种常量值会自动类型转换。

    对象引用 setter 注入

    authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
    authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
    authenticator.authenticationStrategy=$authenticationStrategy
    securityManager.authenticator=$authenticator;

    会自动通过 securityManager.setAuthenticator(authenticator) 注入引用依赖。

    [users] 部分

    配置用户名 / 密码及其角色,格式:“用户名 = 密码,角色 1,角色 2”,角色部分可省略。如:

    [users]
    zhang=123,role1,role2
    wang=123

    [roles] 部分

    配置角色及权限之间的关系,格式:“角色 = 权限 1,权限 2”;如:

    [roles]
    role1=user:create,user:update
    role2=*

    如果只有角色没有对应的权限,可以不配 roles,具体规则请参考授权章节。

    [urls] 部分

    配置 url 及相应的拦截器之间的关系,格式:“url = 拦截器 [参数],拦截器 [参数],如:

    [urls]
    /admin/** = authc, roles[admin], perms["permission1"]

    Shiro 编码加密

    散列算法

    散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如 MD5、SHA 等。一般进行散列时最好提供一个 salt(盐),比如加密密码 “admin”,产生的散列值是 “21232f297a57a5a743894a0e4a801fc3”,可以到一些 md5 解密网站很容易的通过散列值得到密码 “admin”,即如果直接对密码进行散列相对来说破解更容易,此时我们可以加一些只有系统知道的干扰数据,如用户名和 ID(即盐);这样散列的对象是 “密码 + 用户名 +ID”,这样生成的散列值相对来说更难破解。

    为了方便使用,Shiro 提供了 HashService,默认提供了 DefaultHashService 实现。

    DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
    hashService.setHashAlgorithmName("SHA-512");
    hashService.setPrivateSalt(new SimpleByteSource("123")); //私盐,默认无
    hashService.setGeneratePublicSalt(true);//是否生成公盐,默认false
    hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
    hashService.setHashIterations(1); //生成Hash值的迭代次数
    HashRequest request = new HashRequest.Builder()
                .setAlgorithmName("MD5").setSource(ByteSource.Util.bytes("hello"))
                .setSalt(ByteSource.Util.bytes("123")).setIterations(2).build();
    String hex = hashService.computeHash(request).toHex();
    1. 首先创建一个 DefaultHashService,默认使用 SHA-512 算法;
    2. 以通过 hashAlgorithmName 属性修改算法;
    3. 可以通过 privateSalt 设置一个私盐,其在散列时自动与用户传入的公盐混合产生一个新盐;
    4. 可以通过 generatePublicSalt 属性在用户没有传入公盐的情况下是否生成公盐;
    5. 可以设置 randomNumberGenerator 用于生成公盐;
    6. 可以设置 hashIterations 属性来修改默认加密迭代次数;
    7. 需要构建一个 HashRequest,传入算法、数据、公盐、迭代次数。

    密码重试次数限制

    如在 1 个小时内密码最多重试 5 次,如果尝试次数超过 5 次就锁定 1 小时,1 小时后可再次重试,如果还是重试失败,可以锁定如 1 天,以此类推,防止密码被暴力破解。我们通过继承 HashedCredentialsMatcher,且使用 Ehcache 记录重试次数和超时时间。

    com.github.zhangkaitao.shiro.chapter5.hash.credentials.RetryLimitHashedCredentialsMatcher:

    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;
    }
  • 相关阅读:

    删与改

    基本操作
    名词解释
    Python内置函数(11)——complex
    Python内置函数(10)——float
    Python内置函数(9)——int
    Python内置函数(8)——bool
    Python内置函数(7)——sum
  • 原文地址:https://www.cnblogs.com/wangxiayun/p/9181748.html
Copyright © 2011-2022 走看看