Преглед на файлове

验证码oauth2.0放行操作

e преди 5 години
родител
ревизия
97667dae87

+ 184 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/web/WebUtils.java

@@ -0,0 +1,184 @@
1
+package com.ruoyi.common.core.utils.web;
2
+
3
+import com.alibaba.fastjson.JSON;
4
+import com.ruoyi.common.core.exception.CheckedException;
5
+import org.bouncycastle.util.encoders.Base64;
6
+import org.slf4j.Logger;
7
+import org.slf4j.LoggerFactory;
8
+import org.springframework.http.HttpHeaders;
9
+import org.springframework.http.MediaType;
10
+import org.springframework.http.server.reactive.ServerHttpRequest;
11
+import org.springframework.util.Assert;
12
+import org.springframework.web.bind.annotation.ResponseBody;
13
+import org.springframework.web.context.request.RequestContextHolder;
14
+import org.springframework.web.context.request.ServletRequestAttributes;
15
+import org.springframework.web.method.HandlerMethod;
16
+
17
+import javax.servlet.http.Cookie;
18
+import javax.servlet.http.HttpServletRequest;
19
+import javax.servlet.http.HttpServletResponse;
20
+import java.io.IOException;
21
+import java.io.PrintWriter;
22
+import java.io.UnsupportedEncodingException;
23
+import java.nio.charset.StandardCharsets;
24
+
25
+public class WebUtils extends org.springframework.web.util.WebUtils
26
+{
27
+    private final Logger logger = LoggerFactory.getLogger(WebUtils.class);
28
+
29
+    private static final String BASIC_ = "Basic ";
30
+
31
+//    /**
32
+//     * 判断是否ajax请求 spring ajax 返回含有 ResponseBody 或者 RestController注解
33
+//     *
34
+//     * @param handlerMethod HandlerMethod
35
+//     * @return 是否ajax请求
36
+//     */
37
+//    public boolean isBody(HandlerMethod handlerMethod)
38
+//    {
39
+//        ResponseBody responseBody = ClassUtils.getAnnotation(handlerMethod, ResponseBody.class);
40
+//        return responseBody != null;
41
+//    }
42
+
43
+    /**
44
+     * 读取cookie
45
+     *
46
+     * @param name cookie name
47
+     * @return cookie value
48
+     */
49
+    public String getCookieVal(String name)
50
+    {
51
+        HttpServletRequest request = WebUtils.getRequest();
52
+        Assert.notNull(request, "request from RequestContextHolder is null");
53
+        return getCookieVal(request, name);
54
+    }
55
+
56
+    /**
57
+     * 读取cookie
58
+     *
59
+     * @param request HttpServletRequest
60
+     * @param name cookie name
61
+     * @return cookie value
62
+     */
63
+    public String getCookieVal(HttpServletRequest request, String name)
64
+    {
65
+        Cookie cookie = getCookie(request, name);
66
+        return cookie != null ? cookie.getValue() : null;
67
+    }
68
+
69
+    /**
70
+     * 清除 某个指定的cookie
71
+     *
72
+     * @param response HttpServletResponse
73
+     * @param key cookie key
74
+     */
75
+    public void removeCookie(HttpServletResponse response, String key)
76
+    {
77
+        setCookie(response, key, null, 0);
78
+    }
79
+
80
+    /**
81
+     * 设置cookie
82
+     *
83
+     * @param response HttpServletResponse
84
+     * @param name cookie name
85
+     * @param value cookie value
86
+     * @param maxAgeInSeconds maxage
87
+     */
88
+    public void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds)
89
+    {
90
+        Cookie cookie = new Cookie(name, value);
91
+        cookie.setPath("/");
92
+        cookie.setMaxAge(maxAgeInSeconds);
93
+        cookie.setHttpOnly(true);
94
+        response.addCookie(cookie);
95
+    }
96
+
97
+    /**
98
+     * 获取 HttpServletRequest
99
+     *
100
+     * @return {HttpServletRequest}
101
+     */
102
+    public static HttpServletRequest getRequest()
103
+    {
104
+        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
105
+    }
106
+
107
+    /**
108
+     * 获取 HttpServletResponse
109
+     *
110
+     * @return {HttpServletResponse}
111
+     */
112
+    public HttpServletResponse getResponse()
113
+    {
114
+        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
115
+    }
116
+
117
+    /**
118
+     * 返回json
119
+     *
120
+     * @param response HttpServletResponse
121
+     * @param result 结果对象
122
+     */
123
+    public void renderJson(HttpServletResponse response, Object result)
124
+    {
125
+        renderJson(response, result, MediaType.APPLICATION_JSON_VALUE);
126
+    }
127
+
128
+    /**
129
+     * 返回json
130
+     *
131
+     * @param response HttpServletResponse
132
+     * @param result 结果对象
133
+     * @param contentType contentType
134
+     */
135
+    public void renderJson(HttpServletResponse response, Object result, String contentType)
136
+    {
137
+        response.setCharacterEncoding("UTF-8");
138
+        response.setContentType(contentType);
139
+        try (PrintWriter out = response.getWriter())
140
+        {
141
+            out.append(JSON.toJSONString(result));
142
+        }
143
+        catch (IOException e)
144
+        {
145
+            logger.error(e.getMessage(), e);
146
+        }
147
+    }
148
+
149
+    /**
150
+     * 从request 获取CLIENT_ID
151
+     *
152
+     * @return
153
+     * @throws UnsupportedEncodingException
154
+     */
155
+    public static String[] getClientId(ServerHttpRequest request) throws UnsupportedEncodingException
156
+    {
157
+        String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
158
+
159
+        if (header == null || !header.startsWith(BASIC_))
160
+        {
161
+            throw new CheckedException("请求头中client信息为空");
162
+        }
163
+        byte[] base64Token = header.substring(6).getBytes("UTF-8");
164
+        byte[] decoded;
165
+        try
166
+        {
167
+            decoded = Base64.decode(base64Token);
168
+        }
169
+        catch (IllegalArgumentException e)
170
+        {
171
+            throw new CheckedException("Failed to decode basic authentication token");
172
+        }
173
+
174
+        String token = new String(decoded, StandardCharsets.UTF_8);
175
+
176
+        int delim = token.indexOf(":");
177
+
178
+        if (delim == -1)
179
+        {
180
+            throw new CheckedException("Invalid basic authentication token");
181
+        }
182
+        return new String[] { token.substring(0, delim), token.substring(delim + 1) };
183
+    }
184
+}

+ 12 - 0
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java

@@ -1,5 +1,7 @@
1 1
 package com.ruoyi.gateway.filter;
2 2
 
3
+import com.ruoyi.common.core.utils.web.WebUtils;
4
+import com.ruoyi.gateway.config.properties.IgnoreClientProperties;
3 5
 import org.springframework.beans.factory.annotation.Autowired;
4 6
 import org.springframework.cloud.gateway.filter.GatewayFilter;
5 7
 import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
@@ -25,6 +27,9 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
25 27
     @Autowired
26 28
     private ValidateCodeService validateCodeService;
27 29
 
30
+    @Autowired
31
+    private IgnoreClientProperties ignoreClient;
32
+
28 33
     @Override
29 34
     public GatewayFilter apply(Object config)
30 35
     {
@@ -38,6 +43,13 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
38 43
             }
39 44
             try
40 45
             {
46
+                // swagger的oauth2.0验证码放行操作
47
+                String[] clientInfos = WebUtils.getClientId(request);
48
+                if (ignoreClient.getClients().contains(clientInfos[0]))
49
+                {
50
+                    return chain.filter(exchange);
51
+                }
52
+
41 53
                 validateCodeService.checkCapcha(request.getQueryParams().getFirst("code"),
42 54
                         request.getQueryParams().getFirst("uuid"));
43 55
             }