如何将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)));
}