SpringCloud Gateway API接口加解密 - 掘金 (juejin.cn)
CryptoProperties
@Data
@RefreshScope
@ConfigurationProperties("gateway.crypto")
@Component
public class CryptoProperties {
/**
* 是否启用
*/
private boolean enabled;
/**
* 加密参数 key
*/
private String paramName = "data";
/**
* aes key
*/
private String aesKey;
/**
* 加密版本请求头
*/
private String cryptoVersionHeader = "Crypto-Version";
/**
* 默认加密版本
*/
private String cryptoVersion = "1.0.0";
private List<SkipPathPattern> skipPathPattern = new ArrayList<>();
/**
* 加密API集合
*/
private final List<String> includePathPattern = new ArrayList<>();
@Data
public static class SkipPathPattern{
public String path;
public Map<String ,String> queryParamInclude = new HashMap<>();
}
}
RequestProvider
public class RequestProvider {
/**
* 获取原始url
*
* @param exchange
* @return
*/
public static String getOriginalRequestUrl(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
LinkedHashSet<URI> uris = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
URI requestUri = uris.stream().findFirst().orElse(request.getURI());
MultiValueMap<String, String> queryParams = request.getQueryParams();
return UriComponentsBuilder.fromPath(requestUri.getRawPath()).queryParams(queryParams).build().toUriString();
}
}
ResponseProvider
/**
* 请求响应返回
*
*/
public class ResponseProvider {
/**
* 成功
*
* @param message 消息
* @return
*/
public static Map<String, Object> success(String message) {
return response(200, message);
}
/**
* 失败
*
* @param message 消息
* @return
*/
public static Map<String, Object> fail(String message) {
return response(400, message);
}
/**
* 未授权
*
* @param message 消息
* @return
*/
public static Map<String, Object> unAuth(String message) {
return response(401, message);
}
/**
* 服务器异常
*
* @param message 消息
* @return
*/
public static Map<String, Object> error(String message) {
return response(500, message);
}
/**
* 构建返回的JSON数据格式
*
* @param status 状态码
* @param message 消息
* @return
*/
public static Map<String, Object> response(int status, String message) {
Map<String, Object> map = new HashMap<>(16);
map.put("code", status);
map.put("msg", message);
map.put("data", null);
return map;
}
}
StringPool
public interface StringPool {
String AMPERSAND = "&";
String AND = "and";
String AT = "@";
String ASTERISK = "*";
String STAR = ASTERISK;
String SLASH = "/";
String BACK_SLASH = "\";
String DOUBLE_SLASH = "#//";
String COLON = ":";
String COMMA = ",";
String DASH = "-";
String DOLLAR = "$";
String DOT = ".";
String EMPTY = "";
String EMPTY_JSON = "{}";
String EQUALS = "=";
String FALSE = "false";
String HASH = "#";
String HAT = "^";
String LEFT_BRACE = "{";
String LEFT_BRACKET = "(";
String LEFT_CHEV = "<";
String NEWLINE = "\n";
}