|
@@ -28,11 +28,18 @@ import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 基于oauth2.0相关的授权相关操作
|
|
@@ -131,15 +138,38 @@ public class OauthController {
|
|
|
String redirectUri = request.getParameter("redirect_uri");
|
|
|
String status = request.getParameter("status");
|
|
|
String authorizationCode = authorizationService.createAuthorizationCode(clientIdStr, scopeStr, sysUser);
|
|
|
- String params =
|
|
|
- redirectUri + "?code=" + authorizationCode;
|
|
|
- if (StringUtils.isNoneBlank(status)) {
|
|
|
- params = params + "&status=" + status;
|
|
|
+ String UrlString = "";
|
|
|
+ try {
|
|
|
+ URL url = new URL(redirectUri);
|
|
|
+ String queryString = url.getQuery();
|
|
|
+ // 解析查询参数
|
|
|
+ Map<String, String> params = parseQueryParameters(queryString);
|
|
|
+
|
|
|
+ // 修改或添加参数
|
|
|
+ params.put("code", authorizationCode);
|
|
|
+ params.put("status", status);
|
|
|
+ // 封装新的查询参数为字符串
|
|
|
+ String newQueryString = buildQueryString(params);
|
|
|
+ // 构造新的URL(这里仅演示字符串拼接,不实际创建URL对象)
|
|
|
+ UrlString = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort() + url.getPath() + "?" + newQueryString;
|
|
|
+
|
|
|
+ // redisService.delete(key);
|
|
|
+ return "redirect:" + UrlString;
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
// redisService.delete(key);
|
|
|
- return "redirect:" + params;
|
|
|
+ return "redirect:" + REDIRECT_URL + "/login?tenantCode=" + tenantCode;
|
|
|
+
|
|
|
} else {
|
|
|
return "redirect:" + REDIRECT_URL + "/login?tenantCode=" + tenantCode;
|
|
|
+ /*
|
|
|
+ 作为一个参数
|
|
|
+ redirect_uri:http://nbxl.tpddns.cn:4000/index?hideTitle=1&third=1
|
|
|
+ 浏览器解析之后就变成两个参数了
|
|
|
+ redirect_uri:http://nbxl.tpddns.cn:4000/index?hideTitle=1
|
|
|
+ third:1
|
|
|
+ */
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -322,4 +352,43 @@ public class OauthController {
|
|
|
result.put("error_description", errorCodeEnum.getErrorDescription());
|
|
|
}
|
|
|
|
|
|
+ // 解析URL查询参数
|
|
|
+ public static Map<String, String> parseQueryParameters(String queryString) {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ if (queryString != null && !queryString.isEmpty()) {
|
|
|
+ String[] pairs = queryString.split("&");
|
|
|
+ for (String pair : pairs) {
|
|
|
+ int idx = pair.indexOf("=");
|
|
|
+ if (idx > 0) {
|
|
|
+ try {
|
|
|
+ String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
|
|
|
+ String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
|
|
|
+ params.put(key, value);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 封装查询参数为字符串
|
|
|
+ public static String buildQueryString(Map<String, String> params) {
|
|
|
+ StringBuilder queryBuilder = new StringBuilder();
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ if (queryBuilder.length() > 0) {
|
|
|
+ queryBuilder.append("&");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ queryBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
|
|
|
+ .append("=")
|
|
|
+ .append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return queryBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
}
|