diff --git a/blade-gateway/pom.xml b/blade-gateway/pom.xml index aa0911be7..2f08404ec 100644 --- a/blade-gateway/pom.xml +++ b/blade-gateway/pom.xml @@ -42,6 +42,11 @@ + + org.springblade + blade-starter-jwt + ${bladex.project.version} + org.springframework.cloud @@ -140,9 +145,10 @@ + + tofile="${session.executionRootDirectory}/target/${project.artifactId}.jar" + file="${project.build.directory}/${project.artifactId}.jar" /> 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 10afd5c26..cc2e89a0d 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 @@ -18,7 +18,10 @@ package org.springblade.gateway.config; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springblade.gateway.handler.*; +import org.springblade.gateway.handler.SwaggerResourceHandler; +import org.springblade.gateway.props.AuthProperties; +import org.springblade.gateway.props.RouteProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; @@ -45,22 +48,23 @@ import reactor.core.publisher.Mono; @Slf4j @Configuration @AllArgsConstructor +@EnableConfigurationProperties({RouteProperties.class, AuthProperties.class}) public class RouterFunctionConfiguration { + private final SwaggerResourceHandler swaggerResourceHandler; + /** * 这里为支持的请求头,如果有自定义的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_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_ORIGIN = "*"; private static final String ALLOWED_EXPOSE = "*"; private static final String MAX_AGE = "18000L"; - private final HystrixFallbackHandler hystrixFallbackHandler; - private final SwaggerResourceHandler swaggerResourceHandler; - private final SwaggerSecurityHandler swaggerSecurityHandler; - private final SwaggerUiHandler swaggerUiHandler; - + /** + * 跨域配置 + */ @Bean public WebFilter corsFilter() { return (ServerWebExchange ctx, WebFilterChain chain) -> { @@ -83,22 +87,16 @@ public class RouterFunctionConfiguration { }; } + @Bean public RouterFunction routerFunction() { - return RouterFunctions.route( - RequestPredicates.path("/fallback") - .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler) - .andRoute(RequestPredicates.GET("/swagger-resources") - .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); + return RouterFunctions.route(RequestPredicates.GET("/swagger-resources") + .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler); } /** - * 解决springboot2.0.5版本出现的 Only one connection receive subscriber allowed. + * 解决 Only one connection receive subscriber allowed. * 参考:https://github.com/spring-cloud/spring-cloud-gateway/issues/541 */ @Bean diff --git a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteService.java b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteService.java similarity index 98% rename from blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteService.java rename to blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteService.java index 363ada955..aed779ad2 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteService.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteService.java @@ -14,7 +14,7 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.dynamic.service; +package org.springblade.gateway.dynamic; import org.springframework.cloud.gateway.event.RefreshRoutesEvent; import org.springframework.cloud.gateway.route.RouteDefinition; diff --git a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteServiceListener.java b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteServiceListener.java similarity index 97% rename from blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteServiceListener.java rename to blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteServiceListener.java index 5fa7dbc0a..e7996bc3b 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/service/DynamicRouteServiceListener.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/DynamicRouteServiceListener.java @@ -14,7 +14,7 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.dynamic.service; +package org.springblade.gateway.dynamic; import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.cloud.nacos.NacosDiscoveryProperties; @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; +import lombok.extern.slf4j.Slf4j; import org.springblade.core.launch.constant.NacosConstant; import org.springblade.core.launch.props.BladeProperties; import org.springframework.cloud.gateway.route.RouteDefinition; @@ -38,6 +39,7 @@ import java.util.concurrent.Executor; * @author Chill */ @Order +@Slf4j @Component public class DynamicRouteServiceListener { diff --git a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayFilter.java b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayFilter.java similarity index 96% rename from blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayFilter.java rename to blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayFilter.java index b224c6837..a7c64ec82 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayFilter.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayFilter.java @@ -14,7 +14,7 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.dynamic.model; +package org.springblade.gateway.dynamic; import lombok.Data; diff --git a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayPredicate.java b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayPredicate.java similarity index 96% rename from blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayPredicate.java rename to blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayPredicate.java index 0c53a293b..1e8e43512 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayPredicate.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayPredicate.java @@ -14,7 +14,7 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.dynamic.model; +package org.springblade.gateway.dynamic; import lombok.Data; diff --git a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayRoute.java b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayRoute.java similarity index 96% rename from blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayRoute.java rename to blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayRoute.java index a9b6ff61f..c12600930 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/dynamic/model/GatewayRoute.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/dynamic/GatewayRoute.java @@ -14,7 +14,7 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.dynamic.model; +package org.springblade.gateway.dynamic; import lombok.Data; diff --git a/blade-gateway/src/main/java/org/springblade/gateway/endpoint/RouteEndpoint.java b/blade-gateway/src/main/java/org/springblade/gateway/endpoint/RouteEndpoint.java index 4b49902b3..4e811561a 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/endpoint/RouteEndpoint.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/endpoint/RouteEndpoint.java @@ -17,9 +17,9 @@ package org.springblade.gateway.endpoint; import lombok.AllArgsConstructor; -import org.springblade.gateway.dynamic.model.GatewayPredicate; -import org.springblade.gateway.dynamic.model.GatewayRoute; -import org.springblade.gateway.dynamic.service.DynamicRouteService; +import org.springblade.gateway.dynamic.GatewayPredicate; +import org.springblade.gateway.dynamic.GatewayRoute; +import org.springblade.gateway.dynamic.DynamicRouteService; import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; import org.springframework.cloud.gateway.route.RouteDefinition; import org.springframework.cloud.gateway.route.RouteDefinitionLocator; diff --git a/blade-gateway/src/main/java/org/springblade/gateway/filter/AuthFilter.java b/blade-gateway/src/main/java/org/springblade/gateway/filter/AuthFilter.java new file mode 100644 index 000000000..baece8691 --- /dev/null +++ b/blade-gateway/src/main/java/org/springblade/gateway/filter/AuthFilter.java @@ -0,0 +1,98 @@ +/* + * 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.filter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.jsonwebtoken.Claims; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springblade.core.jwt.JwtUtil; +import org.springblade.gateway.props.AuthProperties; +import org.springblade.gateway.provider.AuthProvider; +import org.springblade.gateway.provider.ResponseProvider; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.http.HttpStatus; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.nio.charset.StandardCharsets; + +/** + * 鉴权认证 + * + * @author Chill + */ +@Slf4j +@Component +@AllArgsConstructor +public class AuthFilter implements GlobalFilter, Ordered { + private AuthProperties authProperties; + private ObjectMapper objectMapper; + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + String path = exchange.getRequest().getURI().getPath(); + if (isSkip(path)) { + return chain.filter(exchange); + } + ServerHttpResponse resp = exchange.getResponse(); + String headerToken = exchange.getRequest().getHeaders().getFirst(AuthProvider.AUTH_KEY); + String paramToken = exchange.getRequest().getQueryParams().getFirst(AuthProvider.AUTH_KEY); + if (StringUtils.isAllBlank(headerToken, paramToken)) { + return unAuth(resp, "缺失令牌,鉴权失败"); + } + String auth = StringUtils.isBlank(headerToken) ? paramToken : headerToken; + String token = JwtUtil.getToken(auth); + Claims claims = JwtUtil.parseJWT(token); + if (claims == null) { + return unAuth(resp, "请求未授权"); + } + return chain.filter(exchange); + } + + private boolean isSkip(String path) { + return AuthProvider.getDefaultSkipUrl().stream().map(url -> url.replace(AuthProvider.TARGET, AuthProvider.REPLACEMENT)).anyMatch(path::startsWith) + || authProperties.getSkipUrl().stream().map(url -> url.replace(AuthProvider.TARGET, AuthProvider.REPLACEMENT)).anyMatch(path::startsWith); + } + + private Mono unAuth(ServerHttpResponse resp, String msg) { + resp.setStatusCode(HttpStatus.UNAUTHORIZED); + resp.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); + String result = ""; + try { + result = objectMapper.writeValueAsString(ResponseProvider.unAuth(msg)); + } catch (JsonProcessingException e) { + log.error(e.getMessage(), e); + } + DataBuffer buffer = resp.bufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8)); + return resp.writeWith(Flux.just(buffer)); + } + + @Override + public int getOrder() { + return -100; + } + +} diff --git a/blade-gateway/src/main/java/org/springblade/gateway/filter/RequestGlobalFilter.java b/blade-gateway/src/main/java/org/springblade/gateway/filter/RequestFilter.java similarity index 96% rename from blade-gateway/src/main/java/org/springblade/gateway/filter/RequestGlobalFilter.java rename to blade-gateway/src/main/java/org/springblade/gateway/filter/RequestFilter.java index b3af9d9fc..b351716ca 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/filter/RequestGlobalFilter.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/filter/RequestFilter.java @@ -25,7 +25,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.a * @author lengleng */ @Component -public class RequestGlobalFilter implements GlobalFilter, Ordered { +public class RequestFilter implements GlobalFilter, Ordered { /** * Process the Web request and (optionally) delegate to the next diff --git a/blade-gateway/src/main/java/org/springblade/gateway/handler/ErrorExceptionHandler.java b/blade-gateway/src/main/java/org/springblade/gateway/handler/ErrorExceptionHandler.java index 0baa2cbb5..282ab0c79 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/handler/ErrorExceptionHandler.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/handler/ErrorExceptionHandler.java @@ -16,6 +16,7 @@ */ package org.springblade.gateway.handler; +import org.springblade.gateway.provider.ResponseProvider; import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler; @@ -26,7 +27,6 @@ import org.springframework.http.HttpStatus; import org.springframework.web.reactive.function.server.*; import org.springframework.web.server.ResponseStatusException; -import java.util.HashMap; import java.util.Map; /** @@ -54,7 +54,7 @@ public class ErrorExceptionHandler extends DefaultErrorWebExceptionHandler { if (error instanceof ResponseStatusException) { code = ((ResponseStatusException) error).getStatus().value(); } - return response(code, this.buildMessage(request, error)); + return ResponseProvider.response(code, this.buildMessage(request, error)); } /** @@ -98,19 +98,4 @@ public class ErrorExceptionHandler extends DefaultErrorWebExceptionHandler { return message.toString(); } - /** - * 构建返回的JSON数据格式 - * - * @param status 状态码 - * @param errorMessage 异常信息 - * @return - */ - public static Map response(int status, String errorMessage) { - Map map = new HashMap<>(16); - map.put("code", status); - map.put("message", errorMessage); - map.put("data", null); - return map; - } - } diff --git a/blade-gateway/src/main/java/org/springblade/gateway/handler/HystrixFallbackHandler.java b/blade-gateway/src/main/java/org/springblade/gateway/handler/HystrixFallbackHandler.java deleted file mode 100644 index 5b84ff112..000000000 --- a/blade-gateway/src/main/java/org/springblade/gateway/handler/HystrixFallbackHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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; - -/** - * Hystrix 降级处理 - * - * @author lengleng - */ -@Slf4j -@Component -public class HystrixFallbackHandler implements HandlerFunction { - @Override - public Mono handle(ServerRequest serverRequest) { - log.error("网关执行请求:{}失败,hystrix服务降级处理", serverRequest.uri()); - return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value()) - .contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromObject("服务异常")); - } -} 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 deleted file mode 100644 index 3fe69f289..000000000 --- a/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerSecurityHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.beans.factory.annotation.Autowired; -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.SecurityConfiguration; -import springfox.documentation.swagger.web.SecurityConfigurationBuilder; - -import java.util.Optional; - -/** - * SwaggerSecurityHandler - * - * @author lengleng - */ -@Slf4j -@Component -public class SwaggerSecurityHandler implements HandlerFunction { - @Autowired(required = false) - private SecurityConfiguration securityConfiguration; - - /** - * 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( - Optional.ofNullable(securityConfiguration) - .orElse(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 deleted file mode 100644 index 8a3176e2d..000000000 --- a/blade-gateway/src/main/java/org/springblade/gateway/handler/SwaggerUiHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.beans.factory.annotation.Autowired; -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.UiConfiguration; -import springfox.documentation.swagger.web.UiConfigurationBuilder; - -import java.util.Optional; - -/** - * SwaggerUiHandler - * - * @author lengleng - */ -@Slf4j -@Component -public class SwaggerUiHandler implements HandlerFunction { - @Autowired(required = false) - private UiConfiguration uiConfiguration; - - /** - * 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( - Optional.ofNullable(uiConfiguration) - .orElse(UiConfigurationBuilder.builder().build()))); - } -} diff --git a/blade-gateway/src/main/java/org/springblade/gateway/config/SwaggerRouteConfiguration.java b/blade-gateway/src/main/java/org/springblade/gateway/props/AuthProperties.java similarity index 65% rename from blade-gateway/src/main/java/org/springblade/gateway/config/SwaggerRouteConfiguration.java rename to blade-gateway/src/main/java/org/springblade/gateway/props/AuthProperties.java index 90f8d8aad..efb8d54d4 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/config/SwaggerRouteConfiguration.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/props/AuthProperties.java @@ -14,18 +14,28 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ -package org.springblade.gateway.config; +package org.springblade.gateway.props; -import org.springblade.gateway.props.RouteProperties; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Configuration; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.context.config.annotation.RefreshScope; + +import java.util.ArrayList; +import java.util.List; /** - * Swagger聚合文档配置 + * 权限过滤 * * @author Chill */ -@Configuration -@EnableConfigurationProperties(RouteProperties.class) -public class SwaggerRouteConfiguration { +@Data +@RefreshScope +@ConfigurationProperties("blade.secure") +public class AuthProperties { + + /** + * 放行API集合 + */ + private final List skipUrl = new ArrayList<>(); + } diff --git a/blade-gateway/src/main/java/org/springblade/gateway/provider/AuthProvider.java b/blade-gateway/src/main/java/org/springblade/gateway/provider/AuthProvider.java new file mode 100644 index 000000000..3fa268e8c --- /dev/null +++ b/blade-gateway/src/main/java/org/springblade/gateway/provider/AuthProvider.java @@ -0,0 +1,60 @@ +/* + * 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.provider; + +import java.util.ArrayList; +import java.util.List; + +/** + * 鉴权配置 + * + * @author Chill + */ +public class AuthProvider { + + public static String TARGET = "/**"; + public static String REPLACEMENT = ""; + public static String AUTH_KEY = "Blade-Auth"; + private static List defaultSkipUrl = new ArrayList<>(); + + static { + defaultSkipUrl.add("/client/**"); + defaultSkipUrl.add("/oauth/token/**"); + defaultSkipUrl.add("/token/**"); + defaultSkipUrl.add("/actuator/health/**"); + defaultSkipUrl.add("/v2/api-docs/**"); + defaultSkipUrl.add("/v2/api-docs-ext/**"); + defaultSkipUrl.add("/auth/**"); + defaultSkipUrl.add("/log/**"); + defaultSkipUrl.add("/menu/routes"); + defaultSkipUrl.add("/menu/auth-routes"); + defaultSkipUrl.add("/menu/top-menu"); + defaultSkipUrl.add("/process/resource-view"); + defaultSkipUrl.add("/process/diagram-view"); + defaultSkipUrl.add("/manager/check-upload"); + defaultSkipUrl.add("/error/**"); + defaultSkipUrl.add("/assets/**"); + } + + /** + * 默认无需鉴权的API + */ + public static List getDefaultSkipUrl() { + return defaultSkipUrl; + } + +} diff --git a/blade-gateway/src/main/java/org/springblade/gateway/provider/ResponseProvider.java b/blade-gateway/src/main/java/org/springblade/gateway/provider/ResponseProvider.java new file mode 100644 index 000000000..38f8efb6d --- /dev/null +++ b/blade-gateway/src/main/java/org/springblade/gateway/provider/ResponseProvider.java @@ -0,0 +1,84 @@ +/* + * 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.provider; + +import java.util.HashMap; +import java.util.Map; + +/** + * 请求响应返回 + * + * @author Chill + */ +public class ResponseProvider { + + /** + * 成功 + * + * @param message 信息 + * @return + */ + public static Map success(String message) { + return response(200, message); + } + + /** + * 失败 + * + * @param message 信息 + * @return + */ + public static Map fail(String message) { + return response(400, message); + } + + /** + * 未授权 + * + * @param message 信息 + * @return + */ + public static Map unAuth(String message) { + return response(401, message); + } + + /** + * 服务器异常 + * + * @param message 信息 + * @return + */ + public static Map error(String message) { + return response(500, message); + } + + /** + * 构建返回的JSON数据格式 + * + * @param status 状态码 + * @param message 信息 + * @return + */ + public static Map response(int status, String message) { + Map map = new HashMap<>(16); + map.put("code", status); + map.put("message", message); + map.put("data", null); + return map; + } + +} diff --git a/blade-gateway/src/main/java/org/springblade/gateway/provider/SwaggerProvider.java b/blade-gateway/src/main/java/org/springblade/gateway/provider/SwaggerProvider.java index a6031b20a..098b1286e 100644 --- a/blade-gateway/src/main/java/org/springblade/gateway/provider/SwaggerProvider.java +++ b/blade-gateway/src/main/java/org/springblade/gateway/provider/SwaggerProvider.java @@ -14,7 +14,6 @@ * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ - package org.springblade.gateway.provider; import lombok.AllArgsConstructor; diff --git a/blade-gateway/src/main/resources/bootstrap.yml b/blade-gateway/src/main/resources/bootstrap.yml index 83fb8145c..63cc8f331 100644 --- a/blade-gateway/src/main/resources/bootstrap.yml +++ b/blade-gateway/src/main/resources/bootstrap.yml @@ -15,5 +15,9 @@ spring: blade: document: resources: - - name: 演示模块 - location: /blade-demo + - name: 授权模块 + location: /blade-auth + - name: 工作台模块 + location: /blade-desk + - name: 系统模块 + location: /blade-system