关于后端:shiro框架的使用

10次阅读

共计 1052 个字符,预计需要花费 3 分钟才能阅读完成。

首先是引入依赖

         <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.7.0</version>
        </dependency>

1. 要拿到一个盐值

String salt = UUID.randomUUID().toString();

产生一个随机字符串作为加密盐应用

2. 对明码进行加密

 SimpleHash simpleHash = new SimpleHash("MD5",entity.getPassword(),salt,1);

须要传入的参数,从左往右的程序别离是: 加密算法、对谁进行加密、加密应用的盐值是谁、加密次数

3. 把加密完的后果改为十六进制

String hashedPassword = simpleHash.toHex();

MD5 加密算法的特点:
1. 不可逆;
2. 雷同内容加密后果也雷同; 例如: 第一次设置完明码,加密完了的密文; 下次登录的时候,会把登录时输出的明码再次加密,而后比照两个加密后的密文值,如果是统一的阐明明码统一。

@Test
    void testMD502() {
        String password="12345";
        String salt= UUID.randomUUID().toString();
        System.out.println(salt);//d5a3cfb5-b952-40cc-af62-fe79c76ad157
        SimpleHash simpleHash=new SimpleHash("MD5",password,salt,1);
        System.out.println(simpleHash.toHex());//9f23b14e61787294cf4dc5934e04f594

    }
    @Test
    void testMD503() {
        String password="12345";
//        String salt= UUID.randomUUID().toString();
        String salt="d5a3cfb5-b952-40cc-af62-fe79c76ad157";
        SimpleHash simpleHash=new SimpleHash("MD5",password,salt,1);
        System.out.println(simpleHash.toHex());//9f23b14e61787294cf4dc5934e04f594

    }

为什么要加盐值,是因为随机的字符串和明码放一起加密的安全性更高。

正文完
 0