美文网首页
(shiro加密)2019-03-27

(shiro加密)2019-03-27

作者: _麻辣香锅不要辣 | 来源:发表于2019-03-27 09:57 被阅读0次

参考:spring-shiro的密码加密

配置凭证匹配器

@Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("SHA-256");//散列算法:MD2、MD5、SHA-1、SHA-256、SHA-384、SHA-512等。
        hashedCredentialsMatcher.setHashIterations(1);//散列的次数,默认1次, 设置两次相当于 md5(md5(""));
        System.out.println("hasedCredentialMather:"+hashedCredentialsMatcher);
        return hashedCredentialsMatcher;
    }

之前的realm类和securityManager类需要稍作修改

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm.
        securityManager.setRealm(customRealm(hashedCredentialsMatcher()));
        //securityManager.setRealms(Arrays.asList(customRealm(hashedCredentialsMatcher()),jwtRealm()));
        System.out.println("securityManager生成成功");
        return securityManager;
    }

   @Bean
    public CustomRealm customRealm(HashedCredentialsMatcher hashedCredentialsMatcher) {
        CustomRealm customRealm=new CustomRealm();
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        System.out.println("customrealm生成成功");
        return  customRealm;
    }

这样就把凭证匹配器注册到身份验证的 Realm 中,在用户进行登陆操作的时候,在 Realm 中的 doGetAuthenticationInfo 方法中使用这种方法进行用户身份认证:

   @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("customrealm----身份认证");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        // 从数据库获取对应用户名密码的用户
            User user = userMapper.selectbyUsername(token.getUsername());
            String password="";
            if(user!=null)
            {password=user.getPassword();}
        return new SimpleAuthenticationInfo(token.getPrincipal(), password, ByteSource.Util.bytes(token.getUsername()), getName());
    }

shiroUtils工具类(生成加密密码)

/**
     * 随机生成 salt 需要指定 它的字符串的长度
     *
     * @param len 字符串的长度
     * @return salt
     */
    public static String generateSalt(int len) {
        //一个Byte占两个字节
        int byteLen = len >> 1;
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        return secureRandom.nextBytes(byteLen).toHex();
    }
    /**
     * 获取加密后的密码,使用默认hash迭代的次数 1 次
     *
     * @param hashAlgorithm hash算法名称 MD2、MD5、SHA-1、SHA-256、SHA-384、SHA-512、etc。
     * @param password      需要加密的密码
     * @param salt          盐
     * @return 加密后的密码
     */
    public static String encryptPassword(String hashAlgorithm, String password, String salt) {
        return encryptPassword(hashAlgorithm, password, salt, 1);
    }
    /**
     * 获取加密后的密码,需要指定 hash迭代的次数
     *
     * @param hashAlgorithm  hash算法名称 MD2、MD5、SHA-1、SHA-256、SHA-384、SHA-512、etc。
     * @param password       需要加密的密码
     * @param salt           盐
     * @param hashIterations hash迭代的次数
     * @return 加密后的密码
     */
    public static String encryptPassword(String hashAlgorithm, String password, String salt, int hashIterations) {
        SimpleHash hash = new SimpleHash(hashAlgorithm, password, salt, hashIterations);
        return hash.toString();
    }

测试

  @RequestMapping("regist")
    public int regist()
    {
        User user=new User();
        user.setUsername("test7");
        user.setPassword(ShiroUtils.encryptPassword("SHA-256", "123456","test7"));
        return userMapper.insertSelective(user);
    }

这里为了方便起见,没有把使用shiroUtils中generateSalt方法生成盐值,而是直接用username作为了盐值,如果要用generateSalt方法的话,还必须要在数据库中将生成的盐值保存。

相关文章

  • (shiro加密)2019-03-27

    参考:spring-shiro的密码加密 配置凭证匹配器 之前的realm类和securityManager类需要...

  • Apache Shiro——浅析

    1、什么是 Apache Shiro?Apache Shiro是Java安全框架,其提供认证,授权,加密,和会话管...

  • Apache Shiro反序列化识别那些事

    1.1 关于Apache Shiro Apache shiro是一个Java安全框架,提供了认证、授权、加密和会话...

  • Apache Shiro

    Shiro安全框架简介 什么是Shiro? Apache的强大灵活的开源框架 认证、授权、企业会话管理、安全加密 ...

  • SpringMVC整合Shiro

    摘要: SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话...

  • Shiro学习(六) Shiro 加密

    1、创建HashedCredentialsMatcher 2、自定义Realm(普通MD5加密) 3、自定义Rea...

  • 一.Shiro简介

    一.是什么? Apache Shiro是Java的一个安全框架。Shiro可以帮助我们完成:认证、授权、加密、会话...

  • shiro简介

    简介 Apache Shiro是Java的一个安全框架,Shiro可以帮助我们完成:认证、授权、加密、会话管理、与...

  • 【Spring Boot】常见问题记录(持续更新..)

    问题1: Java 安全框架 shiro shiro是一个强大的Java安全框架,提供了认证、授权、加密、会话等功...

  • Shiro框架

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功...

网友评论

      本文标题:(shiro加密)2019-03-27

      本文链接:https://www.haomeiwen.com/subject/wpeavqtx.html