以下内容是基于springboot security进行加密的示例:
生成工具jar包:
1 pom.xml文件添加以下内容:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2 指定jar包名称:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>pwdtool</finalName>
</configuration>
</plugin>
</plugins>
</build>
3 提供加密工具类:
package bz.sunlight.security;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
*
*/
public class CryptManager {
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
/**
* 加密字符串并返回
* @param rawData
* @return
*/
public String getCrpyt(String rawData){
return bCryptPasswordEncoder.encode(rawData);
}
/**
* 判断元数据是否加密后数据匹配
* @param rawData
* @param encodedData
* @return
*/
public boolean matched(String rawData, String encodedData){
return bCryptPasswordEncoder.matches(rawData, encodedData);
}
}
4 提供命令行启动入口类:
package bz.sunlight.security;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PwdTool {
private static CryptManager cryptManager = new CryptManager();
public static void main(String[] args) throws Exception{
int idx = 0;
while (idx >=0){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入需要加密的数据(输入空可以直接退出, 如包含','会自动进行加密匹配): ");
String rawData = br.readLine();
if(StringUtils.isEmpty(rawData)){
idx = -1;
System.out.println("BYE!");
}else {
if(rawData.contains(",")){
System.out.println("匹配结果: " + cryptManager.matched(rawData.split(",")[0], rawData.split(",")[1]));
}else {
System.out.println("加密后数据: " + cryptManager.getCrpyt(rawData));
}
idx++;
}
}
}
}
5 mvn clean package
6 java -jar pwdtool.jar
7 效果如下: