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

io.github.nichetoolkit.rest.configure.RestJwtProperties Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.configure;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.github.nichetoolkit.rest.util.GeneralUtils;
import io.github.nichetoolkit.rest.worker.RadixWorker;
import io.github.nichetoolkit.rest.worker.jwt.JwtAlgorithm;
import io.github.nichetoolkit.rest.worker.jwt.JwtBuilder;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * RestJwtProperties
 * 

The rest jwt properties class.

* @author Cyan ([email protected]) * @see lombok.Getter * @see lombok.Setter * @see org.springframework.stereotype.Component * @see org.springframework.boot.context.properties.ConfigurationProperties * @since Jdk1.8 */ @Getter @Setter @Component @ConfigurationProperties(prefix = "nichetoolkit.rest.jwt") public class RestJwtProperties { /** * radixWorker * {@link io.github.nichetoolkit.rest.worker.RadixWorker}

The radixWorker field.

* @see io.github.nichetoolkit.rest.worker.RadixWorker * @see com.fasterxml.jackson.annotation.JsonIgnore */ @JsonIgnore private final RadixWorker radixWorker; /** * enabled *

The enabled field.

*/ private boolean enabled; /** * algorithm * {@link io.github.nichetoolkit.rest.worker.jwt.JwtAlgorithm}

The algorithm field.

* @see io.github.nichetoolkit.rest.worker.jwt.JwtAlgorithm */ private JwtAlgorithm algorithm = JwtAlgorithm.HS256; /** * secret * {@link java.lang.String}

The secret field.

* @see java.lang.String */ private String secret; /** * kid * {@link java.lang.String}

The kid field.

* @see java.lang.String */ private String kid; /** * issuer * {@link java.lang.String}

The issuer field.

* @see java.lang.String */ private String issuer; /** * audiences * {@link java.lang.String}

The audiences field.

* @see java.lang.String */ private String[] audiences; /** * expireTime * {@link java.lang.Long}

The expireTime field.

* @see java.lang.Long */ private Long expireTime = 0L; /** * expireUnit * {@link java.time.temporal.ChronoUnit}

The expireUnit field.

* @see java.time.temporal.ChronoUnit */ private ChronoUnit expireUnit = ChronoUnit.MILLIS; /** * issuedDelayTime * {@link java.lang.Long}

The issuedDelayTime field.

* @see java.lang.Long */ private Long issuedDelayTime = 0L; /** * issuedDelayUnit * {@link java.time.temporal.ChronoUnit}

The issuedDelayUnit field.

* @see java.time.temporal.ChronoUnit */ private ChronoUnit issuedDelayUnit = ChronoUnit.MILLIS; /** * notBeforeEnabled *

The notBeforeEnabled field.

*/ private boolean notBeforeEnabled = false; /** * RestJwtProperties *

Instantiates a new rest jwt properties.

*/ public RestJwtProperties() { this.radixWorker = null; } /** * RestJwtProperties *

Instantiates a new rest jwt properties.

* @param radixWorker {@link io.github.nichetoolkit.rest.worker.RadixWorker}

The radix worker parameter is RadixWorker type.

* @see io.github.nichetoolkit.rest.worker.RadixWorker * @see org.springframework.beans.factory.annotation.Autowired */ @Autowired(required = false) public RestJwtProperties(RadixWorker radixWorker) { this.radixWorker = radixWorker; } /** * algorithmInit *

The algorithm init method.

* @see javax.annotation.PostConstruct */ @PostConstruct public void algorithmInit() { if (algorithm == JwtAlgorithm.NONE) { this.algorithm.signer(); } else { if (GeneralUtils.isNotEmpty(this.secret)) { this.algorithm.verifier(this.secret); if (GeneralUtils.isNotEmpty(this.kid)) { this.algorithm.signer(this.secret, this.kid); } else { this.algorithm.signer(this.secret); } } else if (GeneralUtils.isNotEmpty(radixWorker)) { this.secret = radixWorker.encrypt(System.currentTimeMillis()); this.algorithm.signer(this.secret); this.algorithm.verifier(this.secret); } } } /** * getAudiences *

The get audiences getter method.

* @return {@link java.util.List}

The get audiences return object is List type.

* @see java.util.List * @see java.lang.SuppressWarnings */ @SuppressWarnings("MixedMutabilityReturnType") public List getAudiences() { if (GeneralUtils.isNotEmpty(this.audiences)) { return new ArrayList<>(Arrays.asList(this.audiences)); } return Collections.emptyList(); } /** * toBuilder *

The to builder method.

* @return {@link io.github.nichetoolkit.rest.worker.jwt.JwtBuilder}

The to builder return object is JwtBuilder type.

* @see io.github.nichetoolkit.rest.worker.jwt.JwtBuilder */ public JwtBuilder toBuilder() { if (this.isEnabled()) { JwtBuilder builder = JwtBuilder.builder(); ZonedDateTime nowDateTime = ZonedDateTime.now(ZoneId.systemDefault()); if (GeneralUtils.isNotEmpty(this.getIssuer())) { builder.issuer(this.getIssuer()); } if (GeneralUtils.isNotEmpty(this.getAudiences())) { builder.audience(this.getAudiences()); } ZonedDateTime issuedDateTime = nowDateTime; if (GeneralUtils.isNotEmpty(this.getIssuedDelayTime()) && GeneralUtils.isNotEmpty(this.getIssuedDelayUnit())) { issuedDateTime = nowDateTime.plus(this.getIssuedDelayTime(), this.getIssuedDelayUnit()); } builder.issuedAt(issuedDateTime); if (GeneralUtils.isNotEmpty(this.getExpireTime()) && GeneralUtils.isNotEmpty(this.getExpireUnit())) { builder.expiration(issuedDateTime.plus(this.getExpireTime(), this.getExpireUnit())); } if (this.isNotBeforeEnabled()) { builder.notBefore(issuedDateTime); } return builder; } else { return JwtBuilder.builder(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy