Skip to content

Commit 36eae41

Browse files
committed
remove org.springframework.util.Base64Utils
1 parent 9927777 commit 36eae41

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/crypto/MyAES.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.codingapi.springboot.framework.crypto.AES;
44
import lombok.SneakyThrows;
5-
import org.springframework.util.Base64Utils;
65

76
import java.nio.charset.StandardCharsets;
7+
import java.util.Base64;
88

99
public class MyAES {
1010

@@ -25,12 +25,12 @@ public static MyAES getInstance() {
2525

2626
@SneakyThrows
2727
public String encode(String input) {
28-
return Base64Utils.encodeToString(aes.encrypt(input.getBytes(StandardCharsets.UTF_8)));
28+
return Base64.getEncoder().encodeToString(aes.encrypt(input.getBytes(StandardCharsets.UTF_8)));
2929
}
3030

3131
@SneakyThrows
3232
public String decode(String input) {
33-
return new String(aes.decrypt(Base64Utils.decodeFromString(input)),StandardCharsets.UTF_8);
33+
return new String(aes.decrypt(Base64.getDecoder().decode(input)),StandardCharsets.UTF_8);
3434
}
3535

3636
@SneakyThrows

springboot-starter-security-jwt/src/main/java/com/codingapi/springboot/security/crypto/MyCryptoConfiguration.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.util.Base64Utils;
8+
9+
import java.util.Base64;
910

1011
@Configuration
1112
public class MyCryptoConfiguration {
1213

1314
@Bean
1415
@ConditionalOnMissingBean
1516
public AES aes(SecurityJwtProperties properties) throws Exception {
16-
AES aes = new AES(Base64Utils.decodeFromString(properties.getAseKey()), Base64Utils.decodeFromString(properties.getAseIv()));
17+
AES aes = new AES(Base64.getDecoder().decode(properties.getAseKey().getBytes()),
18+
Base64.getDecoder().decode(properties.getAseIv()));
1719
MyAES.getInstance().init(aes);
1820
return aes;
1921
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/AESUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.codingapi.springboot.framework.crypto;
22

33
import lombok.SneakyThrows;
4-
import org.springframework.util.Base64Utils;
54

65
import java.nio.charset.StandardCharsets;
6+
import java.util.Base64;
77

88
public class AESUtils {
99

@@ -24,19 +24,19 @@ public class AESUtils {
2424

2525
@SneakyThrows
2626
private AESUtils() {
27-
this.aes = new AES(Base64Utils.decodeFromString(key),Base64Utils.decodeFromString(iv));
27+
this.aes = new AES(Base64.getDecoder().decode(key),Base64.getDecoder().decode(iv));
2828
}
2929

3030
public static AESUtils getInstance() {
3131
return instance;
3232
}
3333

3434
public String encode(String input) throws Exception {
35-
return Base64Utils.encodeToString(aes.encrypt(input.getBytes(StandardCharsets.UTF_8)));
35+
return Base64.getEncoder().encodeToString(aes.encrypt(input.getBytes(StandardCharsets.UTF_8)));
3636
}
3737

3838
public String decode(String input) throws Exception {
39-
return new String(aes.decrypt(Base64Utils.decodeFromString(input)),StandardCharsets.UTF_8);
39+
return new String(aes.decrypt(Base64.getDecoder().decode(input)),StandardCharsets.UTF_8);
4040
}
4141

4242
public byte[] encode(byte[] input) throws Exception {

springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/DESUtils.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.codingapi.springboot.framework.crypto;
22

33
import lombok.SneakyThrows;
4-
import org.springframework.util.Base64Utils;
54

65
import java.nio.charset.StandardCharsets;
6+
import java.util.Base64;
77

88
public class DESUtils {
99

@@ -18,19 +18,21 @@ public class DESUtils {
1818

1919
@SneakyThrows
2020
private DESUtils() {
21-
this.des = new DES(Base64Utils.decodeFromString(key));
21+
this.des = new DES(Base64.getDecoder().decode(key));
2222
}
2323

2424
public static DESUtils getInstance() {
2525
return instance;
2626
}
2727

2828
public String encode(String input) throws Exception {
29-
return Base64Utils.encodeToString(des.encrypt(input.getBytes(StandardCharsets.UTF_8)));
29+
return Base64.getEncoder().encodeToString(des.encrypt(input.getBytes(StandardCharsets.UTF_8)));
3030
}
3131

32+
3233
public String decode(String input) throws Exception {
33-
return new String(des.decrypt(Base64Utils.decodeFromString(input)),StandardCharsets.UTF_8);
34+
Base64.Decoder decoder = Base64.getDecoder();
35+
return new String(des.decrypt(decoder.decode(input)),StandardCharsets.UTF_8);
3436
}
3537

3638
public byte[] encode(byte[] input) throws Exception {

springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/RSAUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.codingapi.springboot.framework.crypto;
22

33
import lombok.SneakyThrows;
4-
import org.springframework.util.Base64Utils;
54

65
import java.nio.charset.StandardCharsets;
6+
import java.util.Base64;
77

88
public class RSAUtils {
99

@@ -22,19 +22,19 @@ public class RSAUtils {
2222

2323
@SneakyThrows
2424
private RSAUtils() {
25-
this.rsa = new RSA(Base64Utils.decodeFromString(privateKey),Base64Utils.decodeFromString(publicKey));
25+
this.rsa = new RSA(Base64.getDecoder().decode(privateKey),Base64.getDecoder().decode(publicKey));
2626
}
2727

2828
public static RSAUtils getInstance() {
2929
return instance;
3030
}
3131

3232
public String encode(String input) throws Exception {
33-
return Base64Utils.encodeToString(rsa.encrypt(input.getBytes(StandardCharsets.UTF_8)));
33+
return Base64.getEncoder().encodeToString(rsa.encrypt(input.getBytes(StandardCharsets.UTF_8)));
3434
}
3535

3636
public String decode(String input) throws Exception {
37-
return new String(rsa.decrypt(Base64Utils.decodeFromString(input)),StandardCharsets.UTF_8);
37+
return new String(rsa.decrypt(Base64.getDecoder().decode(input)),StandardCharsets.UTF_8);
3838
}
3939

4040
public byte[] encode(byte[] input) throws Exception {

springboot-starter/src/test/java/com/codingapi/springboot/framework/crypto/AESTest.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.junit.jupiter.api.BeforeAll;
44
import org.junit.jupiter.api.Test;
5-
import org.springframework.util.Base64Utils;
5+
6+
import java.util.Base64;
67

78
import static org.junit.jupiter.api.Assertions.assertEquals;
89

@@ -17,19 +18,22 @@ static void before() throws Exception {
1718
key = aes.getKey();
1819
iv = aes.getIv();
1920

20-
System.out.println("keys:" + Base64Utils.encodeToString(key));
21-
System.out.println("ivs:" + Base64Utils.encodeToString(iv));
21+
Base64.Encoder encoder = Base64.getEncoder();
22+
System.out.println("keys:" + encoder.encodeToString(key));
23+
System.out.println("ivs:" + encoder.encodeToString(iv));
2224

2325
}
2426

2527
@Test
2628
void aes1() throws Exception {
2729
AES aes = new AES();
2830
String content = "hello world";
29-
String encrypt = Base64Utils.encodeToString(aes.encrypt(content.getBytes()));
31+
Base64.Encoder encoder = Base64.getEncoder();
32+
String encrypt = encoder.encodeToString(aes.encrypt(content.getBytes()));
3033
System.out.println("encrypt:" + encrypt);
3134

32-
String decrypt = new String(aes.decrypt(Base64Utils.decodeFromString(encrypt)));
35+
Base64.Decoder decoder = Base64.getDecoder();
36+
String decrypt = new String(aes.decrypt(decoder.decode(encrypt)));
3337
System.out.println("decrypt:" + decrypt);
3438

3539
assertEquals(content, decrypt, "AES encrypt error");
@@ -39,7 +43,8 @@ void aes1() throws Exception {
3943
void aes2() throws Exception {
4044
AES aes = new AES(key, iv);
4145
String content = "hello world";
42-
String encrypt = Base64Utils.encodeToString(aes.encrypt(content.getBytes()));
46+
Base64.Encoder encoder = Base64.getEncoder();
47+
String encrypt = encoder.encodeToString(aes.encrypt(content.getBytes()));
4348
System.out.println("encrypt:" + encrypt);
4449
}
4550

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.codingapi.springboot.framework.crypto;
22

33
import org.junit.jupiter.api.Test;
4-
import org.springframework.util.Base64Utils;
54

65
import java.security.InvalidKeyException;
76
import java.security.NoSuchAlgorithmException;
7+
import java.util.Base64;
88

99
import static org.junit.jupiter.api.Assertions.assertEquals;
1010

1111
class HmacSHA256Test {
1212

1313
@Test
1414
void sha256() throws NoSuchAlgorithmException, InvalidKeyException {
15-
String data = Base64Utils.encodeToString(HmacSHA256.sha256("123456".getBytes(),"123456".getBytes()));
15+
String data = Base64.getEncoder().encodeToString(HmacSHA256.sha256("123456".getBytes(),"123456".getBytes()));
1616
assertEquals("uK0Io6VH41gpuCG3U3AwHdjEsGvdd3H5tUGnWRQGhxg=",data);
1717
}
1818
}

0 commit comments

Comments
 (0)