如何将HttpServletRequest
转换为OAuth2Request
使用的spring-security-oauth
?
最佳答案
从OAuth2Request
提取HttpServletRequest
构造函数所需的查询参数
private static final String CLIENT_ID_PARAM = "client_id";
private static final String REDIRECT_URI_PARAM = "redirect_uri";
private static final String SCOPE_PARAM = "scope";
private static final String RESPONSE_TYPE_PARAM = "response_type";
private static OAuth2Request httpToOauthRequest(final HttpServletRequest request) {
return new OAuth2Request(
getQueryParameters(request),
request.getParameter(CLIENT_ID_PARAM),
null,
false,
getDelimitedSetFromParameter(request.getParameter(SCOPE_PARAM), ","),
null,
request.getParameter(REDIRECT_URI_PARAM),
getDelimitedSetFromParameter(request.getParameter(RESPONSE_TYPE_PARAM), ","),
null
);
}
private static Map<String, String> getQueryParameters(HttpServletRequest request) {
Map<String, String> queryParameters = new HashMap<>();
String queryString = request.getQueryString();
if (StringUtils.isEmpty(queryString)) {
return queryParameters;
}
String[] parameters = queryString.split("&");
for (String parameter : parameters) {
String[] keyValuePair = parameter.split("=");
queryParameters.put(keyValuePair[0], keyValuePair[1]);
}
return queryParameters;
}
private static Set<String> getDelimitedSetFromParameter(final String parameter, final String delimiter) {
return Sets.newHashSet(Arrays.asList(parameter.split(delimiter)));
}
关于java - 如何将HttpServletRequest转换为OAuth2Request对象?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58755097/