给我一个java springboot的消息推送加密例子。代码示例
时间: 2024-09-07 13:02:18 浏览: 40
基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)
在Java Spring Boot中实现消息推送的加密,可以使用对称加密算法,比如AES。以下是一个简单的加密和解密消息的例子:
首先,需要添加一个依赖到你的`pom.xml`文件中,以使用Spring Boot:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
接着,创建一个工具类`EncryptionUtil`来实现消息的加密和解密功能:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class EncryptionUtil {
private static final String ALGORITHM = "AES";
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(256, new SecureRandom());
return keyGen.generateKey();
}
public static String encrypt(String data, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String encryptedData, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedData = Base64.getDecoder().decode(encryptedData);
byte[] decryptedData = cipher.doFinal(decodedData);
return new String(decryptedData);
}
}
```
在`application.properties`中添加加密密钥:
```properties
encryption.key=your-encryption-key-here
```
然后,在你的服务类中使用这个工具类来加密和解密消息:
```java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
private final SecretKey secretKey;
public MessageService(@Value("${encryption.key}") String key) throws Exception {
this.secretKey = EncryptionUtil.generateKey();
}
public String encryptMessage(String message) throws Exception {
return EncryptionUtil.encrypt(message, secretKey);
}
public String decryptMessage(String encryptedMessage) throws Exception {
return EncryptionUtil.decrypt(encryptedMessage, secretKey);
}
}
```
在控制器中,你可以这样使用`MessageService`:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/message")
public class MessageController {
private final MessageService messageService;
@Autowired
public MessageController(MessageService messageService) {
this.messageService = messageService;
}
@PostMapping("/encrypt")
public String encryptMessage(@RequestBody String message) {
try {
return messageService.encryptMessage(message);
} catch (Exception e) {
e.printStackTrace();
return "Error during encryption";
}
}
@PostMapping("/decrypt")
public String decryptMessage(@RequestBody String encryptedMessage) {
try {
return messageService.decryptMessage(encryptedMessage);
} catch (Exception e) {
e.printStackTrace();
return "Error during decryption";
}
}
}
```
阅读全文