ru.foodtechlab.lib.auth.app.config.AppConfig Maven / Gradle / Ivy
The newest version!
package ru.foodtechlab.lib.auth.app.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rcore.domain.security.model.AccessTokenData;
import com.rcore.domain.security.model.RefreshTokenData;
import com.rcore.domain.security.port.TokenGenerator;
import com.rcore.domain.security.port.TokenParser;
import com.rcore.rest.api.spring.commons.jackson.datetime.InstantDeserializer;
import com.rcore.rest.api.spring.commons.jackson.datetime.InstantSerializer;
import com.rcore.rest.api.spring.security.jwt.access.JwtAccessTokenGenerator;
import com.rcore.rest.api.spring.security.jwt.access.JwtAccessTokenParser;
import com.rcore.rest.api.spring.security.jwt.refresh.JwtRefreshTokenGenerator;
import com.rcore.rest.api.spring.security.jwt.refresh.JwtRefreshTokenParser;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.StringUtils;
import ru.foodtechlab.lib.auth.service.domain.migration.config.MigrationConfig;
import ru.foodtechlab.lib.auth.service.domain.migration.port.MigrationIdGenerator;
import ru.foodtechlab.lib.auth.service.domain.migration.port.MigrationRepository;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Configuration
public class AppConfig {
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties,
Environment environment) {
List> allEndpoints = new ArrayList();
Collection webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(
webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,
endpointMediaTypes, corsProperties.toCorsConfiguration(),
new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties,
Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() &&
(StringUtils.hasText(basePath) ||
ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
@Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializerByType(Instant.class, new InstantSerializer());
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.deserializerByType(Instant.class, new InstantDeserializer());
ObjectMapper mapper = builder.build();
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
return mapper;
}
@Bean
public TokenGenerator accessTokenDataTokenGenerator(ObjectMapper objectMapper) {
return new JwtAccessTokenGenerator(objectMapper);
}
@Bean
public TokenGenerator refreshTokenDataTokenGenerator(ObjectMapper objectMapper) {
return new JwtRefreshTokenGenerator(objectMapper);
}
@Bean
public TokenParser accessTokenDataTokenParser(ObjectMapper objectMapper, TokenGenerator accessTokenDataTokenGenerator) {
return new JwtAccessTokenParser(objectMapper, accessTokenDataTokenGenerator);
}
@Bean
public TokenParser refreshTokenDataTokenParser(ObjectMapper objectMapper, TokenGenerator refreshTokenDataTokenGenerator) {
return new JwtRefreshTokenParser(objectMapper, refreshTokenDataTokenGenerator);
}
@Bean
public MigrationConfig migrationConfig(MigrationRepository migrationRepository, MigrationIdGenerator migrationIdGenerator){
return new MigrationConfig(migrationRepository);
}
}