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

de.yourinspiration.spring.jwt.JwtServiceBuilder Maven / Gradle / Ivy

The newest version!
package de.yourinspiration.spring.jwt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Builder for {@link JwtService}.
 * 
 * @author Marcel Härle - [email protected]
 *
 */
public class JwtServiceBuilder {

    private static final Logger log = LoggerFactory.getLogger(JwtServiceBuilder.class);

    public static final long DEFAULT_EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000;
    public static final String DEFAULT_ISSUER = "";
    public static final String DEFAULT_HEADER = "x-access-token";
    public static final String DEFAULT_ATTRIBUTE = "jwtSubject";

    private final String secret;
    private final UserService userService;
    private String issuer = DEFAULT_ISSUER;
    private long expirationTime = DEFAULT_EXPIRATION_TIME;
    private String header = DEFAULT_HEADER;
    private String attribute = DEFAULT_ATTRIBUTE;

    /**
     * Create a new JwtService with the given secret and userSerivce.
     * 
     * @param secret
     *            the secret must not be null or empty and should be at least 10
     *            characters long
     * @param userService
     *            the userService must not be null
     */
    public JwtServiceBuilder(final String secret, final UserService userService) {
        if (secret == null || secret.isEmpty()) {
            throw new IllegalArgumentException("the secret must not be null or empty");
        }
        if (userService == null) {
            throw new IllegalArgumentException("the userService must not be null");
        }
        if (secret.length() < 10) {
            log.warn("The secret for the JWT tokens should be at least 10 characters long. Consider using a longer secret.");
        }
        this.secret = secret;
        this.userService = userService;
    }

    /**
     * Sets the issuer.
     * 
     * @param issuer
     *            the issuer
     * @return returns the builder
     */
    public JwtServiceBuilder issuer(final String issuer) {
        if (issuer == null) {
            throw new IllegalArgumentException("the issuer must not be null");
        }
        this.issuer = issuer;
        return this;
    }

    /**
     * Sets the expiration time.
     * 
     * @param expirationTime
     *            the expiration time must be greater than zero
     * @return returns the builder
     */
    public JwtServiceBuilder expirationTime(final long expirationTime) {
        if (expirationTime <= 0) {
            throw new IllegalArgumentException("the expirationTime must be greater than zero");
        }
        this.expirationTime = expirationTime;
        return this;
    }

    /**
     * Sets the header field.
     * 
     * @param header
     *            the header field
     * @return returns the builder
     */
    public JwtServiceBuilder header(final String header) {
        if (header == null || header.isEmpty()) {
            throw new IllegalArgumentException("the header must not be null or empty");
        }
        this.header = header;
        return this;
    }

    /**
     * Sets the request attribute name for the JWT subject.
     * 
     * @param attribute
     *            the name of the attribute
     * @return returns the builder
     */
    public JwtServiceBuilder attribute(final String attribute) {
        if (attribute == null || attribute.isEmpty()) {
            throw new IllegalArgumentException("the attribute must not be null or empty");
        }
        this.attribute = attribute;
        return this;
    }

    /**
     * Build the JwtService.
     * 
     * @return returns an instance of the JwtService
     */
    public JwtService build() {
        return new JwtServiceImpl(this.secret, this.header, this.userService, this.issuer, this.expirationTime,
                this.attribute);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy