|
|
@@ -1,18 +1,25 @@
|
|
|
package com.kxs.common.security.util;
|
|
|
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
-import org.springframework.security.oauth2.core.*;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2AccessToken;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2Error;
|
|
|
+import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
|
|
|
-import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
|
|
-import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
|
|
|
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -27,20 +34,60 @@ public class OAuth2EndpointUtils {
|
|
|
public final String ACCESS_TOKEN_REQUEST_ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc6749#section-5.2";
|
|
|
|
|
|
public MultiValueMap<String, String> getParameters(HttpServletRequest request) {
|
|
|
+
|
|
|
+ String bodyString = getBodyString(request);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(bodyString);
|
|
|
+
|
|
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
|
|
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(parameterMap.size());
|
|
|
parameterMap.forEach((key, values) -> {
|
|
|
- if (values.length > 0) {
|
|
|
- for (String value : values) {
|
|
|
- parameters.add(key, value);
|
|
|
- }
|
|
|
+ for (String value : values) {
|
|
|
+ parameters.add(key, value);
|
|
|
}
|
|
|
});
|
|
|
+ if(jsonObject != null && !jsonObject.isEmpty()){
|
|
|
+ for(Object key:jsonObject.keySet()){
|
|
|
+ String value = jsonObject.get(key).toString();
|
|
|
+ parameters.add(key.toString(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return parameters;
|
|
|
}
|
|
|
+ private String getBodyString(HttpServletRequest request) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ InputStream inputStream = null;
|
|
|
+ BufferedReader reader = null;
|
|
|
+ try {
|
|
|
+ inputStream = request.getInputStream();
|
|
|
+ reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
|
|
|
+ String line = "";
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (reader != null) {
|
|
|
+ try {
|
|
|
+ reader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
|
|
|
public void throwError(String errorCode, String parameterName, String errorUri) {
|
|
|
- OAuth2Error error = new OAuth2Error(errorCode, "OAuth 2.0 Parameter: " + parameterName, errorUri);
|
|
|
+ OAuth2Error error = new OAuth2Error(errorCode, "OAuth 2.0 参数错误: " + parameterName, errorUri);
|
|
|
throw new OAuth2AuthenticationException(error);
|
|
|
}
|
|
|
|