Shiro 加密与解密 util
一:环境
需要的环境已标红,其他的酌情选择
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<shiro.version>1.4.0</shiro.version>
</properties>
<!--shiro begin-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-cas</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
<!--shiro end-->
二:CryptographyUtil.java 加密封装类
import时一定要引入org.apache.shiro.下的包,jdk和common都有加密的包,容易引入错误
package com.kexin.common.util.encry; import org.apache.shiro.codec.Base64; import org.apache.shiro.crypto.hash.Md5Hash; /** * shiro 加密方法 */ public class CryptographyUtil { private static final String DEFAULT_URL_ENCODING = "UTF-8"; //默认的加密编码 /** * Md5加密,带盐 * @param str * @param salt * @return */ public static String md5(String str, String salt){ return new Md5Hash(str,salt).toString(); } /** * Md5加密,不带盐 * @param str * @return */ public static String md5NotSalt(String str){ return new Md5Hash(str).toString(); } /** * base加密 * @param input * @return */ public static String encodeBase64(String input) { return Base64.encodeToString(input.getBytes()); } /** * base64解密 * @param input * @return */ public static String decodeBase64String(String input) { return Base64.decodeToString(input.getBytes()); } public static void main(String[] args) { String password="123456"; System.out.println("Md5加密带盐:"+ CryptographyUtil.md5(password, "Admin")); System.out.println("Md5加密不带盐:"+ CryptographyUtil.md5NotSalt("Admin")); String encodeBase64String=CryptographyUtil.encodeBase64(password); System.out.println("base64加密:"+ encodeBase64String); System.out.println("base64解密:"+ CryptographyUtil.decodeBase64String(encodeBase64String)); } }