feat: 升级SpringBoot和SpringCloud版本
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<description>spring cloud gateway网关</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-config</artifactId>
|
||||
@@ -18,6 +19,7 @@
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-log-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-loadbalancer-spring-boot-starter</artifactId>
|
||||
@@ -26,26 +28,10 @@
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-sentinel-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!--<dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-redis-spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
@@ -58,6 +44,21 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-redis-spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
@@ -74,7 +75,8 @@
|
||||
<!-- swagger -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
+5
-12
@@ -2,15 +2,15 @@ package com.central.gateway.config;
|
||||
|
||||
import com.central.gateway.auth.*;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.SecurityWebFiltersOrder;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.server.resource.web.server.ServerBearerTokenAuthenticationConverter;
|
||||
import org.springframework.security.oauth2.server.resource.web.server.authentication.ServerBearerTokenAuthenticationConverter;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
|
||||
import org.springframework.security.web.server.authentication.ServerAuthenticationEntryPointFailureHandler;
|
||||
@@ -25,18 +25,11 @@ import org.springframework.security.web.server.authentication.ServerAuthenticati
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebFluxSecurity
|
||||
public class ResourceServerConfiguration {
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
@Autowired
|
||||
private TokenStore tokenStore;
|
||||
|
||||
@Autowired
|
||||
private PermissionAuthManager permissionAuthManager;
|
||||
|
||||
@Bean
|
||||
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http
|
||||
, SecurityProperties securityProperties, TokenStore tokenStore, PermissionAuthManager permissionAuthManager) {
|
||||
//认证处理器
|
||||
ReactiveAuthenticationManager customAuthenticationManager = new CustomAuthenticationManager(tokenStore);
|
||||
JsonAuthenticationEntryPoint entryPoint = new JsonAuthenticationEntryPoint();
|
||||
|
||||
+14
-36
@@ -1,7 +1,7 @@
|
||||
package com.central.gateway.error;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
@@ -12,16 +12,11 @@ import org.springframework.boot.web.reactive.error.ErrorAttributes;
|
||||
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 自定义异常处理
|
||||
*
|
||||
@@ -31,45 +26,28 @@ import java.util.List;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfiguration(before = WebFluxAutoConfiguration.class)
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
|
||||
@ConditionalOnClass(WebFluxConfigurer.class)
|
||||
@AutoConfigureBefore(WebFluxAutoConfiguration.class)
|
||||
@EnableConfigurationProperties({ServerProperties.class, WebProperties.class})
|
||||
@EnableConfigurationProperties({ ServerProperties.class, WebProperties.class })
|
||||
public class CustomErrorWebFluxAutoConfiguration {
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
private final WebProperties webProperties;
|
||||
|
||||
private final List<ViewResolver> viewResolvers;
|
||||
|
||||
private final ServerCodecConfigurer serverCodecConfigurer;
|
||||
|
||||
public CustomErrorWebFluxAutoConfiguration(ServerProperties serverProperties,
|
||||
WebProperties webProperties,
|
||||
ObjectProvider<List<ViewResolver>> viewResolversProvider,
|
||||
ServerCodecConfigurer serverCodecConfigurer,
|
||||
ApplicationContext applicationContext) {
|
||||
public CustomErrorWebFluxAutoConfiguration(ServerProperties serverProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
this.applicationContext = applicationContext;
|
||||
this.webProperties = webProperties;
|
||||
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
|
||||
this.serverCodecConfigurer = serverCodecConfigurer;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
|
||||
JsonErrorWebExceptionHandler exceptionHandler = new JsonErrorWebExceptionHandler(
|
||||
errorAttributes,
|
||||
this.webProperties.getResources(),
|
||||
this.serverProperties.getError(),
|
||||
this.applicationContext);
|
||||
exceptionHandler.setViewResolvers(this.viewResolvers);
|
||||
exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
|
||||
exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
|
||||
@Order(-1)
|
||||
public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes,
|
||||
WebProperties webProperties, ObjectProvider<ViewResolver> viewResolvers,
|
||||
ServerCodecConfigurer serverCodecConfigurer, ApplicationContext applicationContext) {
|
||||
JsonErrorWebExceptionHandler exceptionHandler = new JsonErrorWebExceptionHandler(errorAttributes,
|
||||
webProperties.getResources(), this.serverProperties.getError(), applicationContext);
|
||||
exceptionHandler.setViewResolvers(viewResolvers.orderedStream().toList());
|
||||
exceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
|
||||
exceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
|
||||
return exceptionHandler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -80,7 +80,7 @@ public class JsonErrorWebExceptionHandler extends DefaultErrorWebExceptionHandle
|
||||
*/
|
||||
private Map<String, Object> responseError(ServerRequest request, Throwable error) {
|
||||
String errorMessage = buildMessage(request, error);
|
||||
int httpStatus = getHttpStatus(error);
|
||||
int httpStatus = 200;//getHttpStatus(error);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("resp_code", 1);
|
||||
map.put("resp_msg", errorMessage);
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package com.central.gateway.feign;
|
||||
|
||||
import com.central.common.model.SysMenu;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.AsyncResult;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.util.List;
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = MenuServiceFallbackFactory.class, decode404 = true)
|
||||
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = MenuServiceFallbackFactory.class, dismiss404 = true)
|
||||
public interface MenuService {
|
||||
/**
|
||||
* 角色菜单列表
|
||||
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package com.central.gateway.swagger;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* swagger聚合配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/5
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties("zlt.swagger-agg")
|
||||
@RefreshScope
|
||||
public class SwaggerAggProperties {
|
||||
/**
|
||||
* Swagger返回JSON文档的接口路径(全局配置)
|
||||
*/
|
||||
private String apiDocsPath = "/v2/api-docs";
|
||||
|
||||
/**
|
||||
* Swagger文档版本(全局配置)
|
||||
*/
|
||||
private String swaggerVersion = "2.0";
|
||||
|
||||
/**
|
||||
* 自动生成文档的路由名称,设置了generateRoutes之后,ignoreRoutes不生效
|
||||
*/
|
||||
private Set<String> generateRoutes = new HashSet<>();
|
||||
|
||||
/**
|
||||
* 不自动生成文档的路由名称,设置了generateRoutes之后,本配置不生效
|
||||
*/
|
||||
private Set<String> ignoreRoutes = new HashSet<>();
|
||||
|
||||
/**
|
||||
* 是否显示该路由
|
||||
*/
|
||||
public boolean isShow(String route) {
|
||||
int generateRoutesSize = generateRoutes.size();
|
||||
int ignoreRoutesSize = ignoreRoutes.size();
|
||||
|
||||
if(generateRoutesSize > 0 && !generateRoutes.contains(route)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(ignoreRoutesSize > 0 && ignoreRoutes.contains(route)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.central.gateway.swagger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import springfox.documentation.swagger.web.SecurityConfiguration;
|
||||
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
import springfox.documentation.swagger.web.UiConfiguration;
|
||||
import springfox.documentation.swagger.web.UiConfigurationBuilder;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/10/5
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-resources" )
|
||||
public class SwaggerHandler {
|
||||
private final SwaggerResourcesProvider swaggerResources;
|
||||
@Autowired(required = false)
|
||||
private SecurityConfiguration securityConfiguration;
|
||||
@Autowired(required = false)
|
||||
private UiConfiguration uiConfiguration;
|
||||
|
||||
@Autowired
|
||||
public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
|
||||
this.swaggerResources = swaggerResources;
|
||||
}
|
||||
|
||||
@GetMapping("/configuration/security" )
|
||||
public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
|
||||
return Mono.just(new ResponseEntity<>(
|
||||
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
|
||||
}
|
||||
|
||||
@GetMapping("/configuration/ui" )
|
||||
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
|
||||
return Mono.just(new ResponseEntity<>(
|
||||
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Mono<ResponseEntity> swaggerResources() {
|
||||
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package com.central.gateway.swagger;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.support.NameUtils;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 跨域配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/5
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Component
|
||||
@EnableConfigurationProperties(SwaggerAggProperties.class)
|
||||
@Primary
|
||||
public class SwaggerProvider implements SwaggerResourcesProvider {
|
||||
private final RouteLocator routeLocator;
|
||||
private final GatewayProperties gatewayProperties;
|
||||
|
||||
@Resource
|
||||
private SwaggerAggProperties swaggerAggProperties;
|
||||
|
||||
public SwaggerProvider(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
|
||||
this.routeLocator = routeLocator;
|
||||
this.gatewayProperties = gatewayProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwaggerResource> get() {
|
||||
List<SwaggerResource> resources = new ArrayList<>();
|
||||
Set<String> routes = new HashSet<>();
|
||||
//取出Spring Cloud Gateway中的route
|
||||
routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
|
||||
//结合application.yml中的路由配置,只获取有效的route节点
|
||||
gatewayProperties.getRoutes().stream().filter(
|
||||
routeDefinition -> (
|
||||
routes.contains(routeDefinition.getId()) && swaggerAggProperties.isShow(routeDefinition.getId())
|
||||
)
|
||||
).forEach(routeDefinition -> routeDefinition.getPredicates().stream()
|
||||
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
|
||||
.forEach(predicateDefinition -> resources.add(
|
||||
swaggerResource(
|
||||
routeDefinition.getId(),
|
||||
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", swaggerAggProperties.getApiDocsPath())
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
return resources;
|
||||
}
|
||||
|
||||
private SwaggerResource swaggerResource(String name, String location) {
|
||||
SwaggerResource swaggerResource = new SwaggerResource();
|
||||
swaggerResource.setName(name);
|
||||
swaggerResource.setLocation(location);
|
||||
swaggerResource.setSwaggerVersion(swaggerAggProperties.getSwaggerVersion());
|
||||
return swaggerResource;
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,6 @@ server:
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: sc-gateway
|
||||
name: sc-gateway
|
||||
main:
|
||||
web-application-type: reactive
|
||||
Reference in New Issue
Block a user