废话不多说,直接上代码
引入依赖
< dependency>
< groupId> cn.shuibo</ groupId>
< artifactId> rsa-encrypt-body-spring-boot</ artifactId>
< version> 1.0.1.RELEASE</ version>
</ dependency>
配置文件
rsa :
encrypt :
open : true
showLog : true
publicKey : MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn0MVxeKJM36xHVlivHBMfr5hY+UwLGGJ6H32zV5rruX38grA46CeZWncTEKq2QYrrvsyN6FU+gQeA9vXyeXypeAe8Hmv2dfVo2o+5vLO4E13KBoqT6iADEmfPkA/fkg32r/qzxK/DzMUBkkil8cqqH5+QSsLpvTB/F4FalNoB6k7Fov83Pqlc19f6nXDfWoFr4+fAONcwnBzSMr2JhLNIZsM2WPmu3JdxaVUX9a6lpSrlsFU1uDszWmLjI8nwKdudISqqGLDyaATlm88z4xaqVPlbPNeG4Ht+TNNRHGFXfD6XlTb9O9AhQ2S6Rmc/N+lg3pT8oJ9z9KndkdkuphHfQIDAQAB
privateKey : MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCfQxXF4okzfrEdWWK8cEx+vmFj5TAsYYnoffbNXmuu5ffyCsDjoJ5ladxMQqrZBiuu+zI3oVT6BB4D29fJ5fKl4B7wea/Z19Wjaj7m8s7gTXcoGipPqIAMSZ8+QD9+SDfav+rPEr8PMxQGSSKXxyqofn5BKwum9MH8XgVqU2gHqTsWi/zc+qVzX1/qdcN9agWvj58A41zCcHNIyvYmEs0hmwzZY+a7cl3FpVRf1rqWlKuWwVTW4OzNaYuMjyfAp250hKqoYsPJoBOWbzzPjFqpU+Vs814bge35M01EcYVd8PpeVNv070CFDZLpGZz836WDelPygn3P0qd2R2S6mEd9AgMBAAECggEABtuWpmy4x7Te34qny8FyKcr82pZV2rNggthZtkIolOFmKc/4ulSCPY+vYsFEJhAz9VbvtKGHzTQ8LqJaymDSb/4UlvpGbjTzCBRdHQi7i8EKLnnh9Igm62CIMOAHW+K0NQEqlTwTag4jAJp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启加解密
package com. qiangesoft. encryption ;
import cn. shuibo. annotation. EnableSecurity ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
@EnableSecurity
@SpringBootApplication
public class EncryptionApplication {
public static void main ( String [ ] args) {
SpringApplication . run ( EncryptionApplication . class , args) ;
}
}
案例
package com. qiangesoft. encryption. controller ;
import cn. shuibo. annotation. Decrypt ;
import cn. shuibo. annotation. Encrypt ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. web. bind. annotation. * ;
@Slf4j
@RequestMapping ( "/test" )
@RestController
public class TestController {
@Encrypt
@GetMapping ( "/encryption" )
public UserDTO encryption ( ) {
UserDTO userDTO = new UserDTO ( ) ;
userDTO. setUsername ( "admin" ) ;
userDTO. setPassword ( "123456" ) ;
userDTO. setNickname ( "超级管理员" ) ;
userDTO. setSex ( "男" ) ;
userDTO. setAge ( 29 ) ;
userDTO. setTelephone ( "13333333333" ) ;
userDTO. setAddress ( "北京市西城区" ) ;
return userDTO;
}
@Decrypt
@PostMapping ( "/decryption" )
public UserDTO decryption ( @RequestBody UserDTO userDTO) {
return userDTO;
}
}
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
</ head>
< body>
< button onclick = " encryption ( ) " > 加密提交</ button>
< button onclick = " decryption ( ) " > 解密数据</ button>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script src = " https://cdn.bootcdn.net/ajax/libs/jsencrypt/3.0.0-rc.1/jsencrypt.js" > </ script>
< script>
const PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn0MVxeKJM36xHVlivHBMfr5hY+UwLGGJ6H32zV5rruX38grA46CeZWncTEKq2QYrrvsyN6FU+gQeA9vXyeXypeAe8Hmv2dfVo2o+5vLO4E13KBoqT6iADEmfPkA/fkg32r/qzxK/DzMUBkkil8cqqH5+QSsLpvTB/F4FalNoB6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4InZxPDrZKH41OKHoL/eTEpeHoE9MZ90epQX6awKBgQC231IR+Ol8jkmmoDNJspCtFeARjCliGflQwLfFuprPpqhyt1Cg1/x+245sBLoCMWRNm1ZT638cnQsRzB/C2QGqQ8l44pL95Ffj7HAf7NTvM3kFcOK87Cmw5Hae73ARiIKft6cKJoGfuxxWIwGIGLb/WAakZ4viYgW1XLd9MnIPtwKBgAp8DEibi4Tp0QtnNyOCB9x37U2zKEeKyEMXpiRAkgchUdHWBRI32/9h/bo5Ik+Q5JYpgVFoZUMzktGFvskJcEoqKY5EfzdV2sy8d6knaqp6h15XfLHsJSQo2qwELmUd20OKgNwSctN8a6AOwXCrSyHqL5BhDXM5XMFRGRr508AlAoGALNrsG4JtauLPx/Ilgl8tQ3dJJaS2hdyPHbKXoQVbcbGUQccbHMi9ozQhY/do1J7e1nZSn3T1EcSncW8bH0TxNp037tHMckAvGrXk7r+9nwqGFcKRqvtWYpA3VFawq2xLdferHj9BrjdMBQjDGGnPgSRLRMotMpMOiWS7kRbtiVMCgYBmQKXaPHjkXtreFG8XJFsCTLV0aV4yTMF5mqqkxHmXm+PZq+qvJf8F0NZollj7AKOrIkTEcw6waeHwu2zD8j+xQP4AjU8SMZZfwnj9Sr5Vc6AFFZHYHr6n9VfSF+puoDcmaU6MLqcOI9qpJmHKe5F2+HhgDlVGDVH60BrjQwGUnw==' ;
function encryption ( ) {
var data = {
username : "admin" ,
password : "123456" ,
nickname : "超级管理员" ,
sex : "男" ,
age : "29" ,
telephone : "13111111111" ,
address : "北京市西城区"
} ;
var encrypt = new JSEncrypt ( ) ;
encrypt. setPublicKey ( PUBLIC_KEY ) ;
var encrypted = encrypt. encrypt ( JSON . stringify ( data) ) ;
console. log ( '加密前数据:%o' , data) ;
console. log ( '加密后数据:%o' , encrypted) ;
$. ajax ( {
url : "/test/decryption" ,
type : "POST" ,
contentType : "application/json;charset=utf-8" ,
data : encrypted,
success : function ( data ) {
console. log ( JSON . stringify ( data) )
}
} )
}
function decryption ( ) {
$. ajax ( {
url : "/test/encryption" ,
type : "GET" ,
contentType : "application/json;charset=utf-8" ,
success : function ( data ) {
var decrypt = new JSEncrypt ( ) ;
decrypt. setPrivateKey ( PRIVATE_KEY ) ;
var relData = decrypt. decrypt ( data) ;
console. log ( '加密前数据:%o' , data) ;
console. log ( '加密后数据:%o' , relData) ;
}
} )
}
</ script>
</ body>
</ html>
后台解密
2024 - 05 - 22 15 : 40 : 50.778 INFO 5500 -- - [ nio- 8033 - exec- 3 ] c. shuibo. advice. DecryptHttpInputMessage : Encrypted data received:il2zExRKOcC4XMT2TQwSdnejCkPtxOx1K+ T5hrsmFmIohxbS8xwXl3O4ypHwSsgKUnAPPAUJV0OnSaNBuoi / KcoeHAjhJb6GugWm / VJCejw0mC0tg3onoPVUOUjRm + lHlymzSXiculpvz8qee+ WaKjRRRW
后台加密
2024 - 05 - 22 15 : 43 : 17.334 INFO 5500 -- - [ nio- 8033 - exec- 5 ] c. s. advice. EncryptResponseBodyAdvice : Pre - encrypted data:{ "address" : "北京市西城区" , "age" : 29 , "nickname" : "超级管理员" , "password" : "123456" , "sex" : "男" , "telephone" : "13333333333" , "username" : "admin" } ,After encryption:HhZx25AZgh + d7MIu0rh07zZam2boxDFNNMO/ hWssCsYDR4Qf2+ / 6 Z5KVIQcP3Bf73uGEQD81tRznUasMvYIt1Kt8MAomOhz2GIkauJw1ZwnD03kyLVwlGrFWote3giwj8LiJ4knjlFDbHsYgHN/ 3 PWpCbWIm0IjWgv0T98MZFWSyGOaR+ ZBr8ckdQrCAN3XMd3zwICxitQ4aYc0asElTwMybj + 8 u5p4Jla1WwpJ+ VeB7UYXhvNWM06mQK / JwXGrZ / YN3thDUGu1GAxCMtnkIGFk5jNCFzaC8pvAavW4H1WIppO0JFzbrQWcVU2dtr61VKwsn / V3FVGFbG7HmrhZ8yJgGXtzE8eDTBliN / CpOF975BQjQT49i89DYozr76kdDgjIYdLna / 9 P6ESC9fR2hYqaYKs1kOmiWS+ OLwr97Nz + 3 EViYOCgfdYO/ Il6lXMeuGksddBls9m0RWXgKAkxLq6P / UyLM6I3PZk + lkc5YrPg7swaRE3yySRBIzIq1NalELXZ+ 3 yfywbfKhDYrR8dAW7sFbwDkHu3LjqQ7u3cKfYTvxLUK20XgQCr2APWlNnbASkn6/ 3 GXwt3ZeDp9lCl/ bXXsX8GiArcRPiV5/ Qb8CZJUE2 / YpU1haq / J9rPwa8L5y / 1 O3yLyt/ aWQi1r+ G / eQbXJzYACqp954My8bNwDVhg=