All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flyfish.oauth.utils.OAuthRequestParser Maven / Gradle / Ivy

package com.flyfish.oauth.utils;

import com.flyfish.oauth.common.OAuthContext;
import com.flyfish.oauth.domain.OAuth2AccessToken;
import com.flyfish.oauth.domain.data.Tuple;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 请求工具类
 *
 * @author wangyu
 */
public class OAuthRequestParser {

    private HttpServletRequest request;

    private OAuthRequestParser(HttpServletRequest request) {
        this.request = request;
    }

    public static OAuthRequestParser parse(HttpServletRequest request) {
        return new OAuthRequestParser(request);
    }

    public String getStringParam(String key) {
        if (request != null) {
            return request.getParameter(key);
        }
        return null;
    }

    public String getHeader(String key) {
        if (request != null) {
            return request.getHeader(key);
        }
        return null;
    }

    public String getAccessToken() {
        String value = getHeader("Authorization");
        // 提前优先级,优先读取cookie
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie != null && cookie.getName().equals(OAuth2AccessToken.ACCESS_TOKEN)) {
                    return cookie.getValue();
                }
            }
        }
        // 尝试读取Authorization
        if (StringUtils.isNotBlank(value)) {
            if (value.contains(OAuth2AccessToken.BEARER_TYPE)) {
                return value.substring(OAuth2AccessToken.BEARER_TYPE.length());
            }
            return value;
        }
        return null;
    }

    public boolean isAuthRequest() {
        return getURI().contains(OAuth2AccessToken.AUTH_PARAM_VALUE);
    }

    /**
     * 是否需要鉴权
     *
     * @return 结果
     */
    public boolean needRedirect() {
        return StringUtils.isNotBlank(getStringParam(OAuth2AccessToken.AUTH_PARAM_VALUE));
    }

    public String getRefreshToken() {
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie != null && cookie.getName().equals(OAuth2AccessToken.REFRESH_TOKEN)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public String getLocation(String localUrl) {
        Map query = request.getParameterMap();
        URI uri = URI.create(localUrl);
        String current = getURI();
        if (current.contains("/oauth/callback")) {
            current = current.replace("/oauth/callback", "");
        }
        String url = uri.resolve(current).toString();
        if (MapUtils.isNotEmpty(query)) {
            return url + "?" + joinQuery(query);
        }
        return url;
    }

    private String joinQuery(Map query) {
        List entryString = new ArrayList<>();
        for (Map.Entry entry : query.entrySet()) {
            if (!OAuth2AccessToken.AUTH_PARAM_VALUE.equals(entry.getKey())) {
                String value = StringUtils.join(entry.getValue(), ",");
                if (StringUtils.isNotBlank(value)) {
                    entryString.add(entry.getKey() + "=" + value);
                }
            }
        }
        return StringUtils.join(entryString, "&");
    }

    public String getURI() {
        Tuple rewrite = OAuthContext.getProperties().getRewrite();
        if (null != rewrite) {
            return request.getRequestURI().replace(rewrite.getFirst(), rewrite.getSecond());
        }
        return request.getRequestURI();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy