你有没有过这样的经历?在开发 Spring Boot3 项目时,满心以为攻克了业务逻辑就大功告成,结果安全测试时,加密环节的漏洞直接让项目 “卡壳”。从用户密码存储到接口数据传输,加密算法选不好,就像给自家大门装了个形同虚设的锁!别急,今天手把手教你吃透 Spring Boot3 里的常用加密算法,附赠超实用代码示例!
如今,数据泄露事件频发,互联网大厂对后端系统的安全要求近乎严苛。在 Spring Boot3 开发体系下,加密算法是守护数据的最后一道防线。想象一下,用户千辛万苦设置的复杂密码,要是因为加密方式太弱被破解,企业口碑和用户信任都会瞬间崩塌。而 Spring Boot3 凭借丰富的加密工具库,为开发者提供了多种 “安全武器”,但怎么挑、怎么用,学问可大了!
对称加密 “快枪手”:AES
AES(高级加密标准)凭借高速加解密和高安全性,成为对称加密领域的 “顶流”。它支持 128 位、192 位、256 位密钥长度,密钥越长,安全性越高。
使用示例:
在pom.xml添加依赖:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
编写加密工具类:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String KEY = "ThisIsASecretKey12345";
private static final String ALGORITHM = "AES";
public static String encrypt(String data) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
在实际项目中,用它加密用户登录的敏感信息,传输过程更安心。
非对称加密 “安全卫士”:RSA
RSA 作为非对称加密的 “明星”,利用公钥加密、私钥解密的特性,特别适合网络环境下的数据传输。
使用示例:
生成密钥对:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSAKeyGenerator {
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("公钥: " + publicKey);
System.out.println("私钥: " + privateKey);
}
}
实现加密解密:
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAUtil {
private static final String ALGORITHM = "RSA";
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static PublicKey getPublicKey(String key) throws Exception {
byte[] decoded = Base64.getDecoder().decode(key);
X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
return factory.generatePublic(spec);
}
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] decoded = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
return factory.generatePrivate(spec);
}
}
不同微服务间传递核心数据时,RSA 加密能精准守护数据的保密性。
密码加密 “靠谱管家”:BCryptPasswordEncoder
Spring Security 官方力荐的 BCryptPasswordEncoder,自带 “加盐” 功能,能有效防止密码被破解。
使用示例:
在 Spring Boot3 项目中,引入 Spring Security 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
编写代码使用:
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class PasswordEncoderExample {
public static void main(String[] args) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String rawPassword = "user123456";
String encodedPassword = encoder.encode(rawPassword);
System.out.println("加密后的密码: " + encodedPassword);
boolean isMatch = encoder.matches("user123456", encodedPassword);
System.out.println("密码验证结果: " + isMatch);
}
}
将用户密码用它加密存储,即便数据库泄露,黑客也只能对着乱码 “干瞪眼”。
当然,Spring Boot3 的加密 “武器库” 里,还有 Diffie - Hellman 密钥交换、ECC(椭圆曲线密码学)等算法,它们在特定场景下同样 “大显身手”。但只要掌握 AES、RSA、BCryptPasswordEncoder 这几款 “主力选手”,日常开发中的加密需求基本都能轻松应对。
数据安全容不得半点侥幸!看完这篇文章,别再让加密算法成为你项目的 “短板”。赶紧动手实践,把这些代码示例应用到项目中。要是你在使用过程中遇到问题,或者有更巧妙的加密方案,欢迎在评论区留言,咱们一起把 Spring Boot3 项目的安全等级拉满!