Skip to content

Commit 85daa7e

Browse files
author
smallchill
committed
🎉 3.7.0.RELEASE Token加密传输
1 parent abc4122 commit 85daa7e

File tree

3 files changed

+229
-4
lines changed

3 files changed

+229
-4
lines changed

blade-gateway/src/main/java/org/springblade/gateway/filter/AuthFilter.java

+9
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import io.jsonwebtoken.Claims;
2222
import lombok.AllArgsConstructor;
2323
import lombok.extern.slf4j.Slf4j;
24+
import org.springblade.core.launch.props.BladeProperties;
2425
import org.springblade.gateway.props.AuthProperties;
2526
import org.springblade.gateway.provider.AuthProvider;
2627
import org.springblade.gateway.provider.ResponseProvider;
28+
import org.springblade.gateway.utils.JwtCrypto;
2729
import org.springblade.gateway.utils.JwtUtil;
2830
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
2931
import org.springframework.cloud.gateway.filter.GlobalFilter;
@@ -39,6 +41,8 @@
3941

4042
import java.nio.charset.StandardCharsets;
4143

44+
import static org.springblade.gateway.utils.JwtCrypto.BLADE_CRYPTO_AES_KEY;
45+
4246
/**
4347
* 鉴权认证
4448
*
@@ -50,6 +54,7 @@
5054
public class AuthFilter implements GlobalFilter, Ordered {
5155
private final AuthProperties authProperties;
5256
private final ObjectMapper objectMapper;
57+
private final BladeProperties bladeProperties;
5358
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
5459

5560
@Override
@@ -66,6 +71,10 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
6671
}
6772
String auth = StringUtils.isBlank(headerToken) ? paramToken : headerToken;
6873
String token = JwtUtil.getToken(auth);
74+
//校验 加密Token 合法性
75+
if (JwtUtil.isCrypto(auth)) {
76+
token = JwtCrypto.decryptToString(token, bladeProperties.getEnvironment().getProperty(BLADE_CRYPTO_AES_KEY));
77+
}
6978
Claims claims = JwtUtil.parseJWT(token);
7079
if (claims == null) {
7180
return unAuth(resp, "请求未授权");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* Neither the name of the dreamlu.net developer nor the names of its
13+
* contributors may be used to endorse or promote products derived from
14+
* this software without specific prior written permission.
15+
* Author: Chill 庄骞 ([email protected])
16+
*/
17+
package org.springblade.gateway.utils;
18+
19+
import lombok.SneakyThrows;
20+
import org.springframework.util.Assert;
21+
import org.springframework.util.Base64Utils;
22+
import org.springframework.util.StringUtils;
23+
import reactor.util.annotation.Nullable;
24+
25+
import javax.crypto.Cipher;
26+
import javax.crypto.spec.IvParameterSpec;
27+
import javax.crypto.spec.SecretKeySpec;
28+
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.Arrays;
31+
import java.util.Objects;
32+
33+
/**
34+
* JwtCrypto
35+
*
36+
* @author Chill
37+
*/
38+
public class JwtCrypto {
39+
40+
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
41+
public static final String BLADE_CRYPTO_AES_KEY = "blade.token.aes-key";
42+
43+
44+
/**
45+
* Base64加密
46+
*
47+
* @param content 文本内容
48+
* @param aesTextKey 文本密钥
49+
* @return {String}
50+
*/
51+
public static String encryptToString(String content, String aesTextKey) {
52+
return Base64Utils.encodeToString(encrypt(content, aesTextKey));
53+
}
54+
55+
/**
56+
* Base64加密
57+
*
58+
* @param content 内容
59+
* @param aesTextKey 文本密钥
60+
* @return {String}
61+
*/
62+
public static String encryptToString(byte[] content, String aesTextKey) {
63+
return Base64Utils.encodeToString(encrypt(content, aesTextKey));
64+
}
65+
66+
/**
67+
* 加密
68+
*
69+
* @param content 文本内容
70+
* @param aesTextKey 文本密钥
71+
* @return byte[]
72+
*/
73+
public static byte[] encrypt(String content, String aesTextKey) {
74+
return encrypt(content.getBytes(DEFAULT_CHARSET), aesTextKey);
75+
}
76+
77+
/**
78+
* 加密
79+
*
80+
* @param content 文本内容
81+
* @param charset 编码
82+
* @param aesTextKey 文本密钥
83+
* @return byte[]
84+
*/
85+
public static byte[] encrypt(String content, Charset charset, String aesTextKey) {
86+
return encrypt(content.getBytes(charset), aesTextKey);
87+
}
88+
89+
/**
90+
* 加密
91+
*
92+
* @param content 内容
93+
* @param aesTextKey 文本密钥
94+
* @return byte[]
95+
*/
96+
public static byte[] encrypt(byte[] content, String aesTextKey) {
97+
return encrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET));
98+
}
99+
100+
/**
101+
* Base64解密
102+
*
103+
* @param content 文本内容
104+
* @param aesTextKey 文本密钥
105+
* @return {String}
106+
*/
107+
@Nullable
108+
public static String decryptToString(@Nullable String content, @Nullable String aesTextKey) {
109+
if (!StringUtils.hasText(content) || !StringUtils.hasText(aesTextKey)) {
110+
return null;
111+
}
112+
byte[] hexBytes = decrypt(Base64Utils.decode(content.getBytes(DEFAULT_CHARSET)), aesTextKey);
113+
return new String(hexBytes, DEFAULT_CHARSET);
114+
}
115+
116+
117+
/**
118+
* 解密
119+
*
120+
* @param content 内容
121+
* @param aesTextKey 文本密钥
122+
* @return byte[]
123+
*/
124+
public static byte[] decrypt(byte[] content, String aesTextKey) {
125+
return decrypt(content, Objects.requireNonNull(aesTextKey).getBytes(DEFAULT_CHARSET));
126+
}
127+
128+
129+
/**
130+
* 解密
131+
*
132+
* @param content 内容
133+
* @param aesKey 密钥
134+
* @return byte[]
135+
*/
136+
public static byte[] encrypt(byte[] content, byte[] aesKey) {
137+
return aes(Pkcs7Encoder.encode(content), aesKey, Cipher.ENCRYPT_MODE);
138+
}
139+
140+
/**
141+
* 加密
142+
*
143+
* @param encrypted 内容
144+
* @param aesKey 密钥
145+
* @return byte[]
146+
*/
147+
public static byte[] decrypt(byte[] encrypted, byte[] aesKey) {
148+
return Pkcs7Encoder.decode(aes(encrypted, aesKey, Cipher.DECRYPT_MODE));
149+
}
150+
151+
/**
152+
* ase加密
153+
*
154+
* @param encrypted 内容
155+
* @param aesKey 密钥
156+
* @param mode 模式
157+
* @return byte[]
158+
*/
159+
@SneakyThrows
160+
private static byte[] aes(byte[] encrypted, byte[] aesKey, int mode) {
161+
Assert.isTrue(aesKey.length == 32, "IllegalAesKey, aesKey's length must be 32");
162+
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
163+
SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
164+
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
165+
cipher.init(mode, keySpec, iv);
166+
return cipher.doFinal(encrypted);
167+
}
168+
169+
/**
170+
* 提供基于PKCS7算法的加解密接口.
171+
*/
172+
private static class Pkcs7Encoder {
173+
private static final int BLOCK_SIZE = 32;
174+
175+
private static byte[] encode(byte[] src) {
176+
int count = src.length;
177+
// 计算需要填充的位数
178+
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
179+
// 获得补位所用的字符
180+
byte pad = (byte) (amountToPad & 0xFF);
181+
byte[] pads = new byte[amountToPad];
182+
for (int index = 0; index < amountToPad; index++) {
183+
pads[index] = pad;
184+
}
185+
int length = count + amountToPad;
186+
byte[] dest = new byte[length];
187+
System.arraycopy(src, 0, dest, 0, count);
188+
System.arraycopy(pads, 0, dest, count, amountToPad);
189+
return dest;
190+
}
191+
192+
private static byte[] decode(byte[] decrypted) {
193+
int pad = decrypted[decrypted.length - 1];
194+
if (pad < 1 || pad > BLOCK_SIZE) {
195+
pad = 0;
196+
}
197+
if (pad > 0) {
198+
return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
199+
}
200+
return decrypted;
201+
}
202+
}
203+
}

blade-gateway/src/main/java/org/springblade/gateway/utils/JwtUtil.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.jsonwebtoken.Claims;
1919
import io.jsonwebtoken.Jwts;
20+
import lombok.Getter;
2021
import org.springblade.core.launch.constant.TokenConstant;
2122
import org.springblade.gateway.props.JwtProperties;
2223

@@ -31,17 +32,15 @@
3132
public class JwtUtil {
3233

3334
public static String BEARER = TokenConstant.BEARER;
35+
public static String CRYPTO = TokenConstant.CRYPTO;
3436
public static Integer AUTH_LENGTH = 7;
3537

3638
/**
3739
* jwt配置
3840
*/
41+
@Getter
3942
private static JwtProperties jwtProperties;
4043

41-
public static JwtProperties getJwtProperties() {
42-
return jwtProperties;
43-
}
44-
4544
public static void setJwtProperties(JwtProperties properties) {
4645
if (JwtUtil.jwtProperties == null) {
4746
JwtUtil.jwtProperties = properties;
@@ -55,6 +54,20 @@ public static String getBase64Security() {
5554
return Base64.getEncoder().encodeToString(getJwtProperties().getSignKey().getBytes(StandardCharsets.UTF_8));
5655
}
5756

57+
/**
58+
* 判断token类型为crypto
59+
*
60+
* @param auth token
61+
* @return String
62+
*/
63+
public static Boolean isCrypto(String auth) {
64+
if ((auth != null) && (auth.length() > AUTH_LENGTH)) {
65+
String headStr = auth.substring(0, 6).toLowerCase();
66+
return headStr.compareTo(CRYPTO) == 0;
67+
}
68+
return false;
69+
}
70+
5871
/**
5972
* 获取token串
6073
*

0 commit comments

Comments
 (0)