|
@@ -45,11 +45,59 @@ import java.io.IOException;
|
|
|
public class ImageCodeGatewayFilter extends AbstractGatewayFilterFactory {
|
|
|
public static final String DEFAULT_CODE_KEY = "DEFAULT_CODE_KEY";
|
|
|
public static final String OAUTH_TOKEN_URL = "/oauth/token";
|
|
|
+ private static final String BASIC_ = "Basic ";
|
|
|
@Autowired
|
|
|
private RedisTemplate redisTemplate;
|
|
|
@Autowired
|
|
|
private FilterIgnorePropertiesConfig filterIgnorePropertiesConfig;
|
|
|
|
|
|
+ /**
|
|
|
+ * 从header 请求中的clientId/clientsecect
|
|
|
+ *
|
|
|
+ * @param header header中的参数
|
|
|
+ * @throws CheckedException if the Basic header is not present or is not valid
|
|
|
+ * Base64
|
|
|
+ */
|
|
|
+ public static String[] extractAndDecodeHeader(String header)
|
|
|
+ throws IOException, CheckedException {
|
|
|
+
|
|
|
+ byte[] base64Token = header.substring(6).getBytes("UTF-8");
|
|
|
+ byte[] decoded;
|
|
|
+ try {
|
|
|
+ decoded = Base64.decode(base64Token);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ throw new CheckedException(
|
|
|
+ "Failed to decode basic authentication token");
|
|
|
+ }
|
|
|
+
|
|
|
+ String token = new String(decoded, CharsetUtil.UTF_8);
|
|
|
+
|
|
|
+ int delim = token.indexOf(":");
|
|
|
+
|
|
|
+ if (delim == -1) {
|
|
|
+ throw new CheckedException("Invalid basic authentication token");
|
|
|
+ }
|
|
|
+ return new String[]{token.substring(0, delim), token.substring(delim + 1)};
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * *从header 请求中的clientId/clientsecect
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String[] extractAndDecodeHeader(ServerHttpRequest request)
|
|
|
+ throws IOException, CheckedException {
|
|
|
+ String header = request.getHeaders().getFirst("Authorization");
|
|
|
+
|
|
|
+ if (header == null || !header.startsWith(BASIC_)) {
|
|
|
+ throw new CheckedException("请求头中client信息为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ return extractAndDecodeHeader(header);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public GatewayFilter apply(Object config) {
|
|
|
return (exchange, chain) -> {
|
|
@@ -91,7 +139,6 @@ public class ImageCodeGatewayFilter extends AbstractGatewayFilterFactory {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 检查code
|
|
|
*
|
|
@@ -134,53 +181,4 @@ public class ImageCodeGatewayFilter extends AbstractGatewayFilterFactory {
|
|
|
|
|
|
redisTemplate.delete(key);
|
|
|
}
|
|
|
-
|
|
|
- private static final String BASIC_ = "Basic ";
|
|
|
-
|
|
|
- /**
|
|
|
- * 从header 请求中的clientId/clientsecect
|
|
|
- *
|
|
|
- * @param header header中的参数
|
|
|
- * @throws CheckedException if the Basic header is not present or is not valid
|
|
|
- * Base64
|
|
|
- */
|
|
|
- public static String[] extractAndDecodeHeader(String header)
|
|
|
- throws IOException, CheckedException {
|
|
|
-
|
|
|
- byte[] base64Token = header.substring(6).getBytes("UTF-8");
|
|
|
- byte[] decoded;
|
|
|
- try {
|
|
|
- decoded = Base64.decode(base64Token);
|
|
|
- } catch (IllegalArgumentException e) {
|
|
|
- throw new CheckedException(
|
|
|
- "Failed to decode basic authentication token");
|
|
|
- }
|
|
|
-
|
|
|
- String token = new String(decoded, CharsetUtil.UTF_8);
|
|
|
-
|
|
|
- int delim = token.indexOf(":");
|
|
|
-
|
|
|
- if (delim == -1) {
|
|
|
- throw new CheckedException("Invalid basic authentication token");
|
|
|
- }
|
|
|
- return new String[]{token.substring(0, delim), token.substring(delim + 1)};
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * *从header 请求中的clientId/clientsecect
|
|
|
- *
|
|
|
- * @param request
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public static String[] extractAndDecodeHeader(ServerHttpRequest request)
|
|
|
- throws IOException, CheckedException {
|
|
|
- String header = request.getHeaders().getFirst("Authorization");
|
|
|
-
|
|
|
- if (header == null || !header.startsWith(BASIC_)) {
|
|
|
- throw new CheckedException("请求头中client信息为空");
|
|
|
- }
|
|
|
-
|
|
|
- return extractAndDecodeHeader(header);
|
|
|
- }
|
|
|
}
|