如何将HttpServletRequest转换为OAuth2Request对象?

问题描述 投票:0回答:1

如何将HttpServletRequest转换为OAuth2Request使用的spring-security-oauth

java spring spring-security-oauth2
1个回答
0
投票

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)));
    }
© www.soinside.com 2019 - 2024. All rights reserved.