From e9a60a7940a32d9b9456b111c830340cf599569e Mon Sep 17 00:00:00 2001 From: smallchill Date: Thu, 19 Dec 2019 20:40:38 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20swagger-bootstrap-ui-1.9.6=20?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E8=87=B3=20knife4j-2.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blade-gateway/pom.xml | 8 +-- .../config/RouterFunctionConfiguration.java | 31 ++++------- .../handler/SwaggerSecurityHandler.java | 52 +++++++++++++++++++ .../gateway/handler/SwaggerUiHandler.java | 52 +++++++++++++++++++ .../demo/controller/DynamicController.java | 2 +- .../demo/controller/NoticeController.java | 1 + 6 files changed, 121 insertions(+), 25 deletions(-) create mode 100644 blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerSecurityHandler.java create mode 100644 blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerUiHandler.java diff --git a/blade-gateway/pom.xml b/blade-gateway/pom.xml index 3c545968a..0a4236bc0 100644 --- a/blade-gateway/pom.xml +++ b/blade-gateway/pom.xml @@ -92,15 +92,15 @@ io.swagger swagger-models + + com.github.xiaoymin + knife4j-spring-ui + - - com.github.xiaoymin - swagger-bootstrap-ui - diff --git a/blade-gateway/src/main/java/org/springblade/gateway/config/RouterFunctionConfiguration.java b/blade-gateway/src/main/java/org/springblade/gateway/config/RouterFunctionConfiguration.java index cc2e89a0d..385458c6e 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/config/RouterFunctionConfiguration.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/config/RouterFunctionConfiguration.java @@ -19,6 +19,8 @@ package org.springblade.gateway.config; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.gateway.handler.SwaggerResourceHandler; +import org.springblade.gateway.handler.SwaggerSecurityHandler; +import org.springblade.gateway.handler.SwaggerUiHandler; import org.springblade.gateway.props.AuthProperties; import org.springblade.gateway.props.RouteProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -31,7 +33,6 @@ import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.web.cors.reactive.CorsUtils; -import org.springframework.web.filter.reactive.HiddenHttpMethodFilter; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; @@ -52,12 +53,14 @@ import reactor.core.publisher.Mono; public class RouterFunctionConfiguration { private final SwaggerResourceHandler swaggerResourceHandler; + private final SwaggerSecurityHandler swaggerSecurityHandler; + private final SwaggerUiHandler swaggerUiHandler; /** * 这里为支持的请求头,如果有自定义的header字段请自己添加 */ - private static final String ALLOWED_HEADERS = "X-Requested-With, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client"; - private static final String ALLOWED_METHODS = "*"; + private static final String ALLOWED_HEADERS = "X-Requested-With, Tenant-Id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client"; + private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD"; private static final String ALLOWED_ORIGIN = "*"; private static final String ALLOWED_EXPOSE = "*"; private static final String MAX_AGE = "18000L"; @@ -87,26 +90,14 @@ public class RouterFunctionConfiguration { }; } - @Bean public RouterFunction routerFunction() { return RouterFunctions.route(RequestPredicates.GET("/swagger-resources") - .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler); - - } - - /** - * 解决 Only one connection receive subscriber allowed. - * 参考:https://github.com/spring-cloud/spring-cloud-gateway/issues/541 - */ - @Bean - public HiddenHttpMethodFilter hiddenHttpMethodFilter() { - return new HiddenHttpMethodFilter() { - @Override - public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { - return chain.filter(exchange); - } - }; + .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler) + .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler); } } diff --git a/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerSecurityHandler.java b/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerSecurityHandler.java new file mode 100644 index 000000000..72c97093a --- /dev/null +++ b/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerSecurityHandler.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ + +package org.springblade.gateway.handler; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.HandlerFunction; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; +import springfox.documentation.swagger.web.SecurityConfigurationBuilder; + +/** + * SwaggerSecurityHandler + * + * @author lengleng + */ +@Slf4j +@Component +public class SwaggerSecurityHandler implements HandlerFunction { + + /** + * Handle the given request. + * + * @param request the request to handler + * @return the response + */ + @Override + public Mono handle(ServerRequest request) { + return ServerResponse.status(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .body(BodyInserters.fromObject(SecurityConfigurationBuilder.builder().build())); + } +} diff --git a/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerUiHandler.java b/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerUiHandler.java new file mode 100644 index 000000000..5e928ca71 --- /dev/null +++ b/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerUiHandler.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ + +package org.springblade.gateway.handler; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.server.HandlerFunction; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.core.publisher.Mono; +import springfox.documentation.swagger.web.UiConfigurationBuilder; + +/** + * SwaggerUiHandler + * + * @author lengleng + */ +@Slf4j +@Component +public class SwaggerUiHandler implements HandlerFunction { + + /** + * Handle the given request. + * + * @param request the request to handler + * @return the response + */ + @Override + public Mono handle(ServerRequest request) { + return ServerResponse.status(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .body(BodyInserters.fromObject(UiConfigurationBuilder.builder().build())); + } +} diff --git a/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java b/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java index 47e129df8..212e836bc 100644 --- a/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java +++ b/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java @@ -18,9 +18,9 @@ package com.example.demo.controller; import com.example.demo.entity.Notice; import com.example.demo.service.IDynamicService; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiOperationSupport; import lombok.AllArgsConstructor; import org.springblade.core.tool.api.R; import org.springframework.web.bind.annotation.GetMapping; diff --git a/blade-service/blade-demo/src/main/java/com/example/demo/controller/NoticeController.java b/blade-service/blade-demo/src/main/java/com/example/demo/controller/NoticeController.java index 50d901bb4..88b8d21cb 100644 --- a/blade-service/blade-demo/src/main/java/com/example/demo/controller/NoticeController.java +++ b/blade-service/blade-demo/src/main/java/com/example/demo/controller/NoticeController.java @@ -19,6 +19,7 @@ package com.example.demo.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.example.demo.entity.Notice; import com.example.demo.service.INoticeService; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.*; import lombok.AllArgsConstructor; import org.springblade.common.cache.CacheNames;