Java Code Examples for org.springframework.web.reactive.function.server.ServerRequest#Headers

The following examples show how to use org.springframework.web.reactive.function.server.ServerRequest#Headers . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LookoutTokenResolveUtils.java    From sofa-lookout with Apache License 2.0 6 votes vote down vote up
/**
 * <p>从headers中解析出 LookoutToken, 按如下的规则:
 * <ol>
 * <li>检查头 X-Lookout-Token, 如果非空则它的值就是token</li>
 * <li>检查头 Authorization, 如果是一个basic认证, 那么username就是token. 即 "Basic base64(token:)" 的格式</li>
 * </ol>
 * </p>
 *
 * @param headers
 * @return
 */
public static String getLookoutToken(ServerRequest.Headers headers) {
    String token = WebfluxUtils.getHeaderValue(headers, Constants.TOKEN_HEADER_NAME);
    if (token == null) {
        String authorization = WebfluxUtils.getHeaderValue(headers, "Authorization");
        // 按照下面的方式反解出token即可
        // Authorization: Basic: base64(token:)
        if (StringUtils.startsWith(authorization, "Basic ")) {
            String base64 = authorization.substring(6);
            String usernameAndPassword = new String(Base64.getUrlDecoder().decode(base64),
                StandardCharsets.UTF_8);
            String username = StringUtils.substringBefore(usernameAndPassword, ":");
            // 如果 username 是 lookout 那么以 password 作为token
            // 否则 username 用户名作为token
            if ("lookout".equals(username)) {
                String password = StringUtils.substringAfter(usernameAndPassword, ":");
                token = password;
            } else {
                token = username;
            }
        }
    }
    return token;
}
 
Example 2
Source File: WebfluxUtils.java    From sofa-lookout with Apache License 2.0 4 votes vote down vote up
public static String getHeaderValue(ServerRequest.Headers headers, String headerName) {
    List<String> valueList = headers.header(headerName);
    return valueList == null || valueList.isEmpty() ? null : valueList.get(0);
}