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

com.github.yingzhuo.spring.security.jwt.parser.DefaultJwtTokenParser Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
/*                 _                                            _ _                           _       _
 *  ___ _ __  _ __(_)_ __   __ _       ___  ___  ___ _   _ _ __(_) |_ _   _       _ __   __ _| |_ ___| |__
 * / __| '_ \| '__| | '_ \ / _` |_____/ __|/ _ \/ __| | | | '__| | __| | | |_____| '_ \ / _` | __/ __| '_ \
 * \__ \ |_) | |  | | | | | (_| |_____\__ \  __/ (__| |_| | |  | | |_| |_| |_____| |_) | (_| | || (__| | | |
 * |___/ .__/|_|  |_|_| |_|\__, |     |___/\___|\___|\__,_|_|  |_|\__|\__, |     | .__/ \__,_|\__\___|_| |_|
 *     |_|                 |___/                                      |___/      |_|
 *
 *  https://github.com/yingzhuo/spring-security-patch
 */
package com.github.yingzhuo.spring.security.jwt.parser;

import com.github.yingzhuo.spring.security.jwt.JwtToken;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @author 应卓
 */
public class DefaultJwtTokenParser implements JwtTokenParser {

    private static final String AUTHORIZATION = "Authorization";
    private static final String BEARER = "Bearer ";
    private static final int BEARER_LEN = BEARER.length();

    @Override
    public Optional parse(HttpServletRequest request) {

        final String headerValue = request.getHeader(AUTHORIZATION);

        if (headerValue == null) {
            return Optional.empty();
        }

        if (!headerValue.startsWith(BEARER)) {
            return Optional.empty();
        }

        String rawToken = headerValue.substring(BEARER_LEN);

        return Optional.of(
                JwtToken.of(rawToken)
        );
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy