Browse Source

Moved Swagger configuration to TB config file (#1982)

pull/1992/head
VoBa 7 years ago
committed by Igor Kulikov
parent
commit
f087c3fced
  1. 3
      application/src/main/java/org/thingsboard/server/config/SchedulingConfiguration.java
  2. 134
      application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java
  3. 15
      application/src/main/resources/thingsboard.yml
  4. 19
      dao/src/main/java/org/thingsboard/server/dao/cache/TBRedisCacheConfiguration.java

3
application/src/main/java/org/thingsboard/server/config/SchedulingConfiguration.java

@ -23,9 +23,6 @@ import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@Configuration @Configuration
@EnableScheduling @EnableScheduling
public class SchedulingConfiguration implements SchedulingConfigurer { public class SchedulingConfiguration implements SchedulingConfigurer {

134
application/src/main/java/org/thingsboard/server/config/SwaggerConfiguration.java

@ -19,6 +19,7 @@ import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver; import com.fasterxml.classmate.TypeResolver;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.Authority;
@ -43,71 +44,94 @@ import static springfox.documentation.builders.PathSelectors.regex;
@Configuration @Configuration
public class SwaggerConfiguration { public class SwaggerConfiguration {
@Bean @Value("${swagger.api_path_regex}")
public Docket thingsboardApi() { private String apiPathRegex;
TypeResolver typeResolver = new TypeResolver(); @Value("${swagger.security_path_regex}")
final ResolvedType jsonNodeType = private String securityPathRegex;
typeResolver.resolve( @Value("${swagger.non_security_path_regex}")
JsonNode.class); private String nonSecurityPathRegex;
final ResolvedType stringType = @Value("${swagger.title}")
typeResolver.resolve( private String title;
String.class); @Value("${swagger.description}")
private String description;
@Value("${swagger.contact.name}")
private String contactName;
@Value("${swagger.contact.url}")
private String contactUrl;
@Value("${swagger.contact.email}")
private String contactEmail;
@Value("${swagger.license.title}")
private String licenseTitle;
@Value("${swagger.license.url}")
private String licenseUrl;
@Value("${swagger.version}")
private String version;
return new Docket(DocumentationType.SWAGGER_2) @Bean
.groupName("thingsboard") public Docket thingsboardApi() {
.apiInfo(apiInfo()) TypeResolver typeResolver = new TypeResolver();
.alternateTypeRules( final ResolvedType jsonNodeType =
typeResolver.resolve(
JsonNode.class);
final ResolvedType stringType =
typeResolver.resolve(
String.class);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("thingsboard")
.apiInfo(apiInfo())
.alternateTypeRules(
new AlternateTypeRule( new AlternateTypeRule(
jsonNodeType, jsonNodeType,
stringType)) stringType))
.select() .select()
.paths(apiPaths()) .paths(apiPaths())
.build() .build()
.securitySchemes(newArrayList(jwtTokenKey())) .securitySchemes(newArrayList(jwtTokenKey()))
.securityContexts(newArrayList(securityContext())) .securityContexts(newArrayList(securityContext()))
.enableUrlTemplating(true); .enableUrlTemplating(true);
} }
private ApiKey jwtTokenKey() { private ApiKey jwtTokenKey() {
return new ApiKey("X-Authorization", "JWT token", "header"); return new ApiKey("X-Authorization", "JWT token", "header");
} }
private SecurityContext securityContext() { private SecurityContext securityContext() {
return SecurityContext.builder() return SecurityContext.builder()
.securityReferences(defaultAuth()) .securityReferences(defaultAuth())
.forPaths(securityPaths()) .forPaths(securityPaths())
.build(); .build();
} }
private Predicate<String> apiPaths() { private Predicate<String> apiPaths() {
return regex("/api.*"); return regex(apiPathRegex);
} }
private Predicate<String> securityPaths() { private Predicate<String> securityPaths() {
return and( return and(
regex("/api.*"), regex(securityPathRegex),
not(regex("/api/noauth.*")) not(regex(nonSecurityPathRegex))
); );
} }
List<SecurityReference> defaultAuth() { List<SecurityReference> defaultAuth() {
AuthorizationScope[] authorizationScopes = new AuthorizationScope[3]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator"); authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator");
authorizationScopes[1] = new AuthorizationScope(Authority.TENANT_ADMIN.name(), "Tenant administrator"); authorizationScopes[1] = new AuthorizationScope(Authority.TENANT_ADMIN.name(), "Tenant administrator");
authorizationScopes[2] = new AuthorizationScope(Authority.CUSTOMER_USER.name(), "Customer"); authorizationScopes[2] = new AuthorizationScope(Authority.CUSTOMER_USER.name(), "Customer");
return newArrayList( return newArrayList(
new SecurityReference("X-Authorization", authorizationScopes)); new SecurityReference("X-Authorization", authorizationScopes));
} }
private ApiInfo apiInfo() { private ApiInfo apiInfo() {
return new ApiInfoBuilder() return new ApiInfoBuilder()
.title("Thingsboard REST API") .title(title)
.description("For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/reference/rest-api/'>REST API documentation page</a>.") .description(description)
.contact(new Contact("Thingsboard team", "http://thingsboard.io", "info@thingsboard.io")) .contact(new Contact(contactName, contactUrl, contactEmail))
.license("Apache License Version 2.0") .license(licenseTitle)
.licenseUrl("https://github.com/thingsboard/thingsboard/blob/master/LICENSE") .licenseUrl(licenseUrl)
.version("2.0") .version(version)
.build(); .build();
} }
} }

15
application/src/main/resources/thingsboard.yml

@ -488,3 +488,18 @@ transport:
bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}" bind_address: "${COAP_BIND_ADDRESS:0.0.0.0}"
bind_port: "${COAP_BIND_PORT:5683}" bind_port: "${COAP_BIND_PORT:5683}"
timeout: "${COAP_TIMEOUT:10000}" timeout: "${COAP_TIMEOUT:10000}"
swagger:
api_path_regex: "${SWAGGER_API_PATH_REGEX:/api.*}"
security_path_regex: "${SWAGGER_SECURITY_PATH_REGEX:/api.*}"
non_security_path_regex: "${SWAGGER_NON_SECURITY_PATH_REGEX:/api/noauth.*}"
title: "${SWAGGER_TITLE:Thingsboard REST API}"
description: "${SWAGGER_DESCRIPTION:For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/reference/rest-api/'>REST API documentation page</a>.}"
contact:
name: "${SWAGGER_CONTACT_NAME:Thingsboard team}"
url: "${SWAGGER_CONTACT_URL:http://thingsboard.io}"
email: "${SWAGGER_CONTACT_EMAIL:info@thingsboard.io}"
license:
title: "${SWAGGER_LICENSE_TITLE:Apache License Version 2.0}"
url: "${SWAGGER_LICENSE_URL:https://github.com/thingsboard/thingsboard/blob/master/LICENSE}"
version: "${SWAGGER_VERSION:2.0}"

19
dao/src/main/java/org/thingsboard/server/dao/cache/TBRedisCacheConfiguration.java

@ -19,37 +19,18 @@ import lombok.Data;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.ConverterRegistry; import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
@Configuration @Configuration
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis", matchIfMissing = false) @ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis", matchIfMissing = false)

Loading…
Cancel
Save