增加全功能spring-cloud-gateway,优化部分公共模块适配webFlux
This commit is contained in:
@@ -109,6 +109,7 @@ central-platform -- 父项目,公共依赖
|
|||||||
│ │ │ ├─search-server -- 搜索中心服务端[7100]
|
│ │ │ ├─search-server -- 搜索中心服务端[7100]
|
||||||
│ │─zlt-commons -- 通用工具一级工程
|
│ │─zlt-commons -- 通用工具一级工程
|
||||||
│ │ ├─zlt-auth-client-spring-boot-starter -- 封装spring security client端的通用操作逻辑
|
│ │ ├─zlt-auth-client-spring-boot-starter -- 封装spring security client端的通用操作逻辑
|
||||||
|
│ │ ├─zlt-common-core -- 封装通用操作逻辑
|
||||||
│ │ ├─zlt-common-spring-boot-starter -- 封装通用操作逻辑
|
│ │ ├─zlt-common-spring-boot-starter -- 封装通用操作逻辑
|
||||||
│ │ ├─zlt-db-spring-boot-starter -- 封装数据库通用操作逻辑
|
│ │ ├─zlt-db-spring-boot-starter -- 封装数据库通用操作逻辑
|
||||||
│ │ ├─zlt-log-spring-boot-starter -- 封装log通用操作逻辑
|
│ │ ├─zlt-log-spring-boot-starter -- 封装log通用操作逻辑
|
||||||
@@ -119,6 +120,7 @@ central-platform -- 父项目,公共依赖
|
|||||||
│ ├─zlt-config -- 配置中心
|
│ ├─zlt-config -- 配置中心
|
||||||
│ ├─zlt-doc -- 项目文档
|
│ ├─zlt-doc -- 项目文档
|
||||||
│ ├─zlt-gateway -- api网关一级工程
|
│ ├─zlt-gateway -- api网关一级工程
|
||||||
|
│ │ ├─sc-gateway -- spring-cloud-gateway[9900]
|
||||||
│ │ ├─zuul-gateway -- netflix-zuul[9900]
|
│ │ ├─zuul-gateway -- netflix-zuul[9900]
|
||||||
│ ├─zlt-job -- 分布式任务调度一级工程
|
│ ├─zlt-job -- 分布式任务调度一级工程
|
||||||
│ │ ├─job-admin -- 任务管理器[8081]
|
│ │ ├─job-admin -- 任务管理器[8081]
|
||||||
|
|||||||
@@ -46,7 +46,6 @@
|
|||||||
<spring-social-security.version>1.1.6.RELEASE</spring-social-security.version>
|
<spring-social-security.version>1.1.6.RELEASE</spring-social-security.version>
|
||||||
<commons-io.version>2.6</commons-io.version>
|
<commons-io.version>2.6</commons-io.version>
|
||||||
<servlet-api.version>4.0.1</servlet-api.version>
|
<servlet-api.version>4.0.1</servlet-api.version>
|
||||||
<!--<platform-bom>Cairo-SR3</platform-bom>-->
|
|
||||||
<spring-cloud-alibaba-dependencies.version>2.1.0.RELEASE</spring-cloud-alibaba-dependencies.version>
|
<spring-cloud-alibaba-dependencies.version>2.1.0.RELEASE</spring-cloud-alibaba-dependencies.version>
|
||||||
<spring-boot-dependencies.version>2.1.8.RELEASE</spring-boot-dependencies.version>
|
<spring-boot-dependencies.version>2.1.8.RELEASE</spring-boot-dependencies.version>
|
||||||
<spring-cloud-dependencies.version>Greenwich.SR3</spring-cloud-dependencies.version>
|
<spring-cloud-dependencies.version>Greenwich.SR3</spring-cloud-dependencies.version>
|
||||||
@@ -140,7 +139,11 @@
|
|||||||
<artifactId>zlt-config</artifactId>
|
<artifactId>zlt-config</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-common-core</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.jsonwebtoken</groupId>
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
<artifactId>jjwt</artifactId>
|
<artifactId>jjwt</artifactId>
|
||||||
@@ -343,13 +346,6 @@
|
|||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--<dependency>
|
|
||||||
<groupId>io.spring.platform</groupId>
|
|
||||||
<artifactId>platform-bom</artifactId>
|
|
||||||
<version>${platform-bom}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
<scope>import</scope>
|
|
||||||
</dependency>-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-dependencies</artifactId>
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
@@ -441,6 +437,8 @@
|
|||||||
<developer>
|
<developer>
|
||||||
<name>LeTao Zhu</name>
|
<name>LeTao Zhu</name>
|
||||||
<email>zltdiablo@163.com</email>
|
<email>zltdiablo@163.com</email>
|
||||||
|
<organizationUrl>https://github.com/zlt2000</organizationUrl>
|
||||||
|
<url>https://blog.csdn.net/zlt2000</url>
|
||||||
</developer>
|
</developer>
|
||||||
</developers>
|
</developers>
|
||||||
</project>
|
</project>
|
||||||
@@ -35,6 +35,10 @@
|
|||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
|
|||||||
@@ -14,10 +14,18 @@
|
|||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-config</artifactId>
|
<artifactId>zlt-config</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>search-client</artifactId>
|
<artifactId>search-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
|||||||
@@ -44,6 +44,10 @@
|
|||||||
<artifactId>search-client</artifactId>
|
<artifactId>search-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
@@ -67,6 +71,11 @@
|
|||||||
<groupId>io.micrometer</groupId>
|
<groupId>io.micrometer</groupId>
|
||||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.social</groupId>
|
||||||
|
<artifactId>spring-social-security</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -19,5 +19,6 @@
|
|||||||
<module>zlt-ribbon-spring-boot-starter</module>
|
<module>zlt-ribbon-spring-boot-starter</module>
|
||||||
<module>zlt-auth-client-spring-boot-starter</module>
|
<module>zlt-auth-client-spring-boot-starter</module>
|
||||||
<module>zlt-sentinel-spring-boot-starter</module>
|
<module>zlt-sentinel-spring-boot-starter</module>
|
||||||
|
<module>zlt-common-core</module>
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
@@ -12,10 +12,6 @@
|
|||||||
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
||||||
<description>认证客户端通用组件</description>
|
<description>认证客户端通用组件</description>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.zlt</groupId>
|
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||||
@@ -23,6 +19,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-redis-spring-boot-starter</artifactId>
|
<artifactId>zlt-redis-spring-boot-starter</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
|||||||
+7
-4
@@ -6,6 +6,7 @@ import org.springframework.context.annotation.Import;
|
|||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
|
||||||
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
|
||||||
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
|
||||||
@@ -52,10 +53,12 @@ public class DefaultResourceServerConf extends ResourceServerConfigurerAdapter {
|
|||||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||||
.anyRequest();
|
.anyRequest();
|
||||||
setAuthenticate(authorizedUrl);
|
setAuthenticate(authorizedUrl);
|
||||||
//允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
|
|
||||||
http.headers()
|
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
|
||||||
.frameOptions()
|
.and()
|
||||||
.disable()
|
.httpBasic().disable()
|
||||||
|
.headers()
|
||||||
|
.frameOptions().disable()
|
||||||
.and()
|
.and()
|
||||||
.csrf().disable();
|
.csrf().disable();
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
package com.central.oauth2.common.config;
|
||||||
|
|
||||||
|
import com.central.oauth2.common.properties.SecurityProperties;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@EnableConfigurationProperties(SecurityProperties.class)
|
||||||
|
public class SecurityPropertiesConfig {
|
||||||
|
}
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
package com.central.oauth2.common.service;
|
|
||||||
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 请求权限判断service
|
|
||||||
*
|
|
||||||
* @author zlt
|
|
||||||
* @date 2018/10/28
|
|
||||||
*/
|
|
||||||
public interface IPermissionService {
|
|
||||||
/**
|
|
||||||
* 判断请求是否有权限
|
|
||||||
*
|
|
||||||
* @param authentication 认证信息
|
|
||||||
* @return 是否有权限
|
|
||||||
*/
|
|
||||||
boolean hasPermission(HttpServletRequest request, Authentication authentication);
|
|
||||||
}
|
|
||||||
+6
-9
@@ -6,7 +6,6 @@ import com.central.common.constant.CommonConstant;
|
|||||||
import com.central.common.context.TenantContextHolder;
|
import com.central.common.context.TenantContextHolder;
|
||||||
import com.central.common.model.SysMenu;
|
import com.central.common.model.SysMenu;
|
||||||
import com.central.oauth2.common.properties.SecurityProperties;
|
import com.central.oauth2.common.properties.SecurityProperties;
|
||||||
import com.central.oauth2.common.service.IPermissionService;
|
|
||||||
import com.central.oauth2.common.util.AuthUtils;
|
import com.central.oauth2.common.util.AuthUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@@ -18,7 +17,6 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|||||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -29,7 +27,7 @@ import java.util.stream.Collectors;
|
|||||||
* @date 2018/10/28
|
* @date 2018/10/28
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class DefaultPermissionServiceImpl implements IPermissionService {
|
public abstract class DefaultPermissionServiceImpl {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SecurityProperties securityProperties;
|
private SecurityProperties securityProperties;
|
||||||
@@ -43,10 +41,9 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
|||||||
*/
|
*/
|
||||||
public abstract List<SysMenu> findMenuByRoleCodes(String roleCodes);
|
public abstract List<SysMenu> findMenuByRoleCodes(String roleCodes);
|
||||||
|
|
||||||
@Override
|
public boolean hasPermission(Authentication authentication, String requestMethod, String requestURI) {
|
||||||
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
|
|
||||||
// 前端跨域OPTIONS请求预检放行 也可通过前端配置代理实现
|
// 前端跨域OPTIONS请求预检放行 也可通过前端配置代理实现
|
||||||
if (HttpMethod.OPTIONS.name().equalsIgnoreCase(request.getMethod())) {
|
if (HttpMethod.OPTIONS.name().equalsIgnoreCase(requestMethod)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
if (!(authentication instanceof AnonymousAuthenticationToken)) {
|
||||||
@@ -68,7 +65,7 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
|||||||
|
|
||||||
//判断不进行url权限认证的api,所有已登录用户都能访问的url
|
//判断不进行url权限认证的api,所有已登录用户都能访问的url
|
||||||
for (String path : securityProperties.getAuth().getUrlPermission().getIgnoreUrls()) {
|
for (String path : securityProperties.getAuth().getUrlPermission().getIgnoreUrls()) {
|
||||||
if (antPathMatcher.match(path, request.getRequestURI())) {
|
if (antPathMatcher.match(path, requestURI)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,9 +83,9 @@ public abstract class DefaultPermissionServiceImpl implements IPermissionService
|
|||||||
String roleCodes = grantedAuthorityList.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.joining(", "));
|
String roleCodes = grantedAuthorityList.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.joining(", "));
|
||||||
List<SysMenu> menuList = findMenuByRoleCodes(roleCodes);
|
List<SysMenu> menuList = findMenuByRoleCodes(roleCodes);
|
||||||
for (SysMenu menu : menuList) {
|
for (SysMenu menu : menuList) {
|
||||||
if (StringUtils.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), request.getRequestURI())) {
|
if (StringUtils.isNotEmpty(menu.getUrl()) && antPathMatcher.match(menu.getUrl(), requestURI)) {
|
||||||
if (StrUtil.isNotEmpty(menu.getPathMethod())) {
|
if (StrUtil.isNotEmpty(menu.getPathMethod())) {
|
||||||
return request.getMethod().equalsIgnoreCase(menu.getPathMethod());
|
return requestMethod.equalsIgnoreCase(menu.getPathMethod());
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ public class AuthUtils {
|
|||||||
Enumeration<String> headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
|
Enumeration<String> headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
|
||||||
while (headers.hasMoreElements()) {
|
while (headers.hasMoreElements()) {
|
||||||
String value = headers.nextElement();
|
String value = headers.nextElement();
|
||||||
if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
|
if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE))) {
|
||||||
String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
|
String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
|
||||||
int commaIndex = authHeaderValue.indexOf(',');
|
int commaIndex = authHeaderValue.indexOf(',');
|
||||||
if (commaIndex > 0) {
|
if (commaIndex > 0) {
|
||||||
|
|||||||
+2
@@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.central.oauth2.common.config.SecurityPropertiesConfig
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-commons</artifactId>
|
||||||
|
<version>2.7.2</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>zlt-common-core</artifactId>
|
||||||
|
<description>公共通用组件</description>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-log-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.social</groupId>
|
||||||
|
<artifactId>spring-social-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security.oauth</groupId>
|
||||||
|
<artifactId>spring-security-oauth2</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- easypoi -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.afterturn</groupId>
|
||||||
|
<artifactId>easypoi-base</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.nepxion</groupId>
|
||||||
|
<artifactId>banner</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-core</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
+24
@@ -1,12 +1,21 @@
|
|||||||
package com.central.common.utils;
|
package com.central.common.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
|
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||||
|
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zlt
|
* @author zlt
|
||||||
@@ -62,4 +71,19 @@ public class ResponseUtil {
|
|||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* webflux的response返回json对象
|
||||||
|
*/
|
||||||
|
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String msg) {
|
||||||
|
Result result = Result.succeedWith(null, httpStatus, msg);
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
response.setStatusCode(HttpStatus.valueOf(result.getResp_code()));
|
||||||
|
response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||||
|
DataBufferFactory dataBufferFactory = response.bufferFactory();
|
||||||
|
DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
|
||||||
|
return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
|
||||||
|
DataBufferUtils.release(buffer);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.context.ApplicationContextInitializer=\
|
||||||
|
com.central.common.config.BannerInitializer
|
||||||
@@ -12,81 +12,13 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-log-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-core</artifactId>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-aop</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.baomidou</groupId>
|
|
||||||
<artifactId>mybatis-plus</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.social</groupId>
|
|
||||||
<artifactId>spring-social-security</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security.oauth</groupId>
|
|
||||||
<artifactId>spring-security-oauth2</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-annotations</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.hutool</groupId>
|
|
||||||
<artifactId>hutool-all</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.poi</groupId>
|
|
||||||
<artifactId>poi</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.poi</groupId>
|
|
||||||
<artifactId>poi-ooxml</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- easypoi -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>cn.afterturn</groupId>
|
|
||||||
<artifactId>easypoi-base</artifactId>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
|
||||||
<artifactId>jackson-databind</artifactId>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.nepxion</groupId>
|
|
||||||
<artifactId>banner</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
+3
@@ -4,8 +4,10 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.central.common.constant.CommonConstant;
|
import com.central.common.constant.CommonConstant;
|
||||||
import com.central.common.constant.SecurityConstants;
|
import com.central.common.constant.SecurityConstants;
|
||||||
import com.central.common.context.TenantContextHolder;
|
import com.central.common.context.TenantContextHolder;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -18,6 +20,7 @@ import java.io.IOException;
|
|||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/9/15
|
* @date 2019/9/15
|
||||||
*/
|
*/
|
||||||
|
@ConditionalOnClass(Filter.class)
|
||||||
public class TenantFilter extends OncePerRequestFilter {
|
public class TenantFilter extends OncePerRequestFilter {
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
|||||||
+3
@@ -4,9 +4,11 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.central.common.constant.CommonConstant;
|
import com.central.common.constant.CommonConstant;
|
||||||
import com.central.log.properties.TraceProperties;
|
import com.central.log.properties.TraceProperties;
|
||||||
import org.slf4j.MDC;
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -19,6 +21,7 @@ import java.io.IOException;
|
|||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/9/15
|
* @date 2019/9/15
|
||||||
*/
|
*/
|
||||||
|
@ConditionalOnClass(Filter.class)
|
||||||
public class TraceFilter extends OncePerRequestFilter {
|
public class TraceFilter extends OncePerRequestFilter {
|
||||||
@Resource
|
@Resource
|
||||||
private TraceProperties traceProperties;
|
private TraceProperties traceProperties;
|
||||||
|
|||||||
-3
@@ -1,6 +1,3 @@
|
|||||||
org.springframework.context.ApplicationContextInitializer=\
|
|
||||||
com.central.common.config.BannerInitializer
|
|
||||||
|
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
com.central.common.filter.TenantFilter,\
|
com.central.common.filter.TenantFilter,\
|
||||||
com.central.common.filter.TraceFilter
|
com.central.common.filter.TraceFilter
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
@@ -36,6 +36,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
package com.central.common.ribbon;
|
||||||
|
|
||||||
|
import com.central.common.constant.ConfigConstants;
|
||||||
|
import com.central.common.ribbon.config.RuleConfigure;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ribbon扩展配置类
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2018/11/17 9:24
|
||||||
|
*/
|
||||||
|
@ConditionalOnProperty(value = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
|
||||||
|
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
|
||||||
|
public class LbIsolationAutoConfigure {
|
||||||
|
|
||||||
|
}
|
||||||
-11
@@ -1,14 +1,9 @@
|
|||||||
package com.central.common.ribbon;
|
package com.central.common.ribbon;
|
||||||
|
|
||||||
import com.central.common.constant.ConfigConstants;
|
|
||||||
import com.central.common.ribbon.config.RestTemplateProperties;
|
import com.central.common.ribbon.config.RestTemplateProperties;
|
||||||
import com.central.common.ribbon.config.RuleConfigure;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
|
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
|
||||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ribbon扩展配置类
|
* Ribbon扩展配置类
|
||||||
@@ -22,10 +17,4 @@ public class RibbonAutoConfigure {
|
|||||||
public DefaultPropertiesFactory defaultPropertiesFactory() {
|
public DefaultPropertiesFactory defaultPropertiesFactory() {
|
||||||
return new DefaultPropertiesFactory();
|
return new DefaultPropertiesFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnProperty(value = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
|
|
||||||
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
|
|
||||||
public class LbIsolationConfig {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -87,7 +87,7 @@ public class FeignInterceptorConfig {
|
|||||||
Enumeration<String> headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
|
Enumeration<String> headers = request.getHeaders(CommonConstant.TOKEN_HEADER);
|
||||||
while (headers.hasMoreElements()) {
|
while (headers.hasMoreElements()) {
|
||||||
String value = headers.nextElement();
|
String value = headers.nextElement();
|
||||||
if ((value.toLowerCase().startsWith(CommonConstant.BEARER_TYPE.toLowerCase()))) {
|
if ((value.toLowerCase().startsWith(CommonConstant.BEARER_TYPE))) {
|
||||||
String authHeaderValue = value.substring(CommonConstant.BEARER_TYPE.length()).trim();
|
String authHeaderValue = value.substring(CommonConstant.BEARER_TYPE.length()).trim();
|
||||||
int commaIndex = authHeaderValue.indexOf(',');
|
int commaIndex = authHeaderValue.indexOf(',');
|
||||||
if (commaIndex > 0) {
|
if (commaIndex > 0) {
|
||||||
|
|||||||
+5
@@ -5,8 +5,12 @@ import com.central.common.constant.CommonConstant;
|
|||||||
import com.central.common.constant.ConfigConstants;
|
import com.central.common.constant.ConfigConstants;
|
||||||
import com.central.common.context.LbIsolationContextHolder;
|
import com.central.common.context.LbIsolationContextHolder;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.FilterChain;
|
import javax.servlet.FilterChain;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -19,6 +23,7 @@ import java.io.IOException;
|
|||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2019/9/15
|
* @date 2019/9/15
|
||||||
*/
|
*/
|
||||||
|
@ConditionalOnClass(Filter.class)
|
||||||
public class LbIsolationFilter extends OncePerRequestFilter {
|
public class LbIsolationFilter extends OncePerRequestFilter {
|
||||||
@Value("${" + ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED + ":false}")
|
@Value("${" + ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED + ":false}")
|
||||||
private boolean enableIsolation;
|
private boolean enableIsolation;
|
||||||
|
|||||||
+8
-1
@@ -18,6 +18,7 @@ import java.util.stream.Collectors;
|
|||||||
* @date 2019/9/3
|
* @date 2019/9/3
|
||||||
*/
|
*/
|
||||||
public class CustomIsolationRule extends RoundRobinRule {
|
public class CustomIsolationRule extends RoundRobinRule {
|
||||||
|
private final static String KEY_DEFAULT = "default";
|
||||||
/**
|
/**
|
||||||
* 优先根据版本号取实例
|
* 优先根据版本号取实例
|
||||||
*/
|
*/
|
||||||
@@ -26,7 +27,13 @@ public class CustomIsolationRule extends RoundRobinRule {
|
|||||||
if (lb == null) {
|
if (lb == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String version = LbIsolationContextHolder.getVersion();
|
String version;
|
||||||
|
if (key != null && !KEY_DEFAULT.equals(key)) {
|
||||||
|
version = key.toString();
|
||||||
|
} else {
|
||||||
|
version = LbIsolationContextHolder.getVersion();
|
||||||
|
}
|
||||||
|
|
||||||
List<Server> targetList = null;
|
List<Server> targetList = null;
|
||||||
List<Server> upList = lb.getReachableServers();
|
List<Server> upList = lb.getReachableServers();
|
||||||
if (StrUtil.isNotEmpty(version)) {
|
if (StrUtil.isNotEmpty(version)) {
|
||||||
|
|||||||
+2
-1
@@ -2,4 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|||||||
com.central.common.ribbon.RibbonAutoConfigure,\
|
com.central.common.ribbon.RibbonAutoConfigure,\
|
||||||
com.central.common.ribbon.FeignAutoConfigure,\
|
com.central.common.ribbon.FeignAutoConfigure,\
|
||||||
com.central.common.ribbon.RestTemplateAutoConfigure,\
|
com.central.common.ribbon.RestTemplateAutoConfigure,\
|
||||||
com.central.common.ribbon.filter.LbIsolationFilter
|
com.central.common.ribbon.filter.LbIsolationFilter,\
|
||||||
|
com.central.common.ribbon.LbIsolationAutoConfigure
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zlt</groupId>
|
<groupId>com.zlt</groupId>
|
||||||
<artifactId>zlt-common-spring-boot-starter</artifactId>
|
<artifactId>zlt-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ spring.redis.timeout=5000
|
|||||||
|
|
||||||
##### elasticsearch配置
|
##### elasticsearch配置
|
||||||
zlt.elasticsearch.cluster-name=my-es
|
zlt.elasticsearch.cluster-name=my-es
|
||||||
zlt.elasticsearch.cluster-nodes=119.3.105.180
|
zlt.elasticsearch.cluster-nodes=192.168.28.130
|
||||||
|
|
||||||
##### sentinel配置
|
##### sentinel配置
|
||||||
zlt.sentinel.dashboard=192.168.28.130:6999
|
zlt.sentinel.dashboard=192.168.28.130:6999
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
<artifactId>zlt-gateway</artifactId>
|
<artifactId>zlt-gateway</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<modules>
|
<modules>
|
||||||
|
<!-- zuul -->
|
||||||
<module>zuul-gateway</module>
|
<module>zuul-gateway</module>
|
||||||
|
<!-- spring cloud gateway -->
|
||||||
|
<module>sc-gateway</module>
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-gateway</artifactId>
|
||||||
|
<version>2.7.2</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>sc-gateway</artifactId>
|
||||||
|
<description>spring cloud gateway网关</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-ribbon-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-sentinel-spring-boot-starter</artifactId>
|
||||||
|
</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>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<!-- swagger -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springfox</groupId>
|
||||||
|
<artifactId>springfox-swagger2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.springfox</groupId>
|
||||||
|
<artifactId>springfox-swagger-ui</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zlt</groupId>
|
||||||
|
<artifactId>zlt-redis-spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-oauth2-resource-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-extension</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.micrometer</groupId>
|
||||||
|
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>eu.bitwalker</groupId>
|
||||||
|
<artifactId>UserAgentUtils</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.spotify</groupId>
|
||||||
|
<artifactId>docker-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
|
||||||
|
<imageTags>
|
||||||
|
<imageTag>${project.version}</imageTag>
|
||||||
|
<imageTag>latest</imageTag>
|
||||||
|
</imageTags>
|
||||||
|
<forceTags>true</forceTags>
|
||||||
|
<baseImage>${docker.baseImage}</baseImage>
|
||||||
|
<volumes>${docker.volumes}</volumes>
|
||||||
|
<env>
|
||||||
|
<JAVA_OPTS>${docker.java.opts}</JAVA_OPTS>
|
||||||
|
</env>
|
||||||
|
<entryPoint>["sh","-c","java $JAVA_OPTS ${docker.java.security.egd} -jar /${project.build.finalName}.jar"]</entryPoint>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<targetPath>/</targetPath>
|
||||||
|
<directory>${project.build.directory}</directory>
|
||||||
|
<include>${project.build.finalName}.jar</include>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.central;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/5
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@EnableFeignClients
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SCGatewayApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SCGatewayApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
+49
@@ -0,0 +1,49 @@
|
|||||||
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
|
import org.springframework.security.authentication.ReactiveAuthenticationManager;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
|
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
|
||||||
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||||
|
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/6
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public class CustomAuthenticationManager implements ReactiveAuthenticationManager {
|
||||||
|
private TokenStore tokenStore;
|
||||||
|
|
||||||
|
public CustomAuthenticationManager(TokenStore tokenStore) {
|
||||||
|
this.tokenStore = tokenStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Authentication> authenticate(Authentication authentication) {
|
||||||
|
return Mono.justOrEmpty(authentication)
|
||||||
|
.filter(a -> a instanceof BearerTokenAuthenticationToken)
|
||||||
|
.cast(BearerTokenAuthenticationToken.class)
|
||||||
|
.map(BearerTokenAuthenticationToken::getToken)
|
||||||
|
.flatMap((accessTokenValue -> {
|
||||||
|
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
|
||||||
|
if (accessToken == null) {
|
||||||
|
return Mono.error(new InvalidTokenException("Invalid access token: " + accessTokenValue));
|
||||||
|
} else if (accessToken.isExpired()) {
|
||||||
|
tokenStore.removeAccessToken(accessToken);
|
||||||
|
return Mono.error(new InvalidTokenException("Access token expired: " + accessTokenValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
OAuth2Authentication result = tokenStore.readAuthentication(accessToken);
|
||||||
|
if (result == null) {
|
||||||
|
return Mono.error(new InvalidTokenException("Invalid access token: " + accessTokenValue));
|
||||||
|
}
|
||||||
|
return Mono.just(result);
|
||||||
|
}))
|
||||||
|
.cast(Authentication.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
|
import com.central.common.utils.ResponseUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 403拒绝访问异常处理,转换为JSON
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
|
||||||
|
return ResponseUtil.responseWriter(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
|
import com.central.common.utils.ResponseUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.web.server.ServerAuthenticationEntryPoint;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 401未授权异常处理,转换为JSON
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
||||||
|
return ResponseUtil.responseWriter(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.central.common.constant.SecurityConstants;
|
||||||
|
import com.central.common.model.SysUser;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
|
import org.springframework.security.web.server.WebFilterExchange;
|
||||||
|
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 认证成功处理类
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public class Oauth2AuthSuccessHandler implements ServerAuthenticationSuccessHandler {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
||||||
|
SysUser user = (SysUser)authentication.getPrincipal();
|
||||||
|
Long userId = user.getId();
|
||||||
|
String username = user.getUsername();
|
||||||
|
OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
|
||||||
|
String clientId = oauth2Authentication.getOAuth2Request().getClientId();
|
||||||
|
|
||||||
|
ServerWebExchange exchange = webFilterExchange.getExchange();
|
||||||
|
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
||||||
|
.headers(h -> {
|
||||||
|
h.add(SecurityConstants.USER_ID_HEADER, String.valueOf(userId));
|
||||||
|
h.add(SecurityConstants.USER_HEADER, username);
|
||||||
|
h.add(SecurityConstants.TENANT_HEADER, clientId);
|
||||||
|
h.add(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
|
||||||
|
return webFilterExchange.getChain().filter(build);
|
||||||
|
}
|
||||||
|
}
|
||||||
+48
@@ -0,0 +1,48 @@
|
|||||||
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
|
import com.central.common.model.SysMenu;
|
||||||
|
import com.central.gateway.feign.MenuService;
|
||||||
|
import com.central.oauth2.common.service.impl.DefaultPermissionServiceImpl;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.security.authorization.AuthorizationDecision;
|
||||||
|
import org.springframework.security.authorization.ReactiveAuthorizationManager;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.web.server.authorization.AuthorizationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* url权限认证
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/6
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class PermissionAuthManager extends DefaultPermissionServiceImpl implements ReactiveAuthorizationManager<AuthorizationContext> {
|
||||||
|
@Resource
|
||||||
|
private MenuService menuService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext authorizationContext) {
|
||||||
|
return authentication.map(auth -> {
|
||||||
|
ServerWebExchange exchange = authorizationContext.getExchange();
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
boolean isPermission = super.hasPermission(auth, request.getMethodValue(), request.getURI().getPath());
|
||||||
|
return new AuthorizationDecision(isPermission);
|
||||||
|
}).defaultIfEmpty(new AuthorizationDecision(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SysMenu> findMenuByRoleCodes(String roleCodes) {
|
||||||
|
return menuService.findByRoleCodes(roleCodes);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.central.gateway.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.reactive.CorsWebFilter;
|
||||||
|
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
||||||
|
import org.springframework.web.util.pattern.PathPatternParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跨域配置
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/5
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class CorsConfig {
|
||||||
|
private static final String ALL = "*";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public CorsWebFilter corsFilter() {
|
||||||
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
|
// cookie跨域
|
||||||
|
config.setAllowCredentials(Boolean.TRUE);
|
||||||
|
config.addAllowedMethod(ALL);
|
||||||
|
config.addAllowedOrigin(ALL);
|
||||||
|
config.addAllowedHeader(ALL);
|
||||||
|
// 配置前端js允许访问的自定义响应头
|
||||||
|
config.addExposedHeader("setToken");
|
||||||
|
|
||||||
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
|
||||||
|
source.registerCorsConfiguration("/**", config);
|
||||||
|
|
||||||
|
return new CorsWebFilter(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.central.gateway.config;
|
||||||
|
|
||||||
|
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||||
|
import com.central.gateway.route.NacosRouteDefinitionRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态路由配置
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(prefix = "zlt.gateway.dynamicRoute", name = "enabled", havingValue = "true")
|
||||||
|
public class DynamicRouteConfig {
|
||||||
|
@Autowired
|
||||||
|
private ApplicationEventPublisher publisher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nacos实现方式
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnProperty(prefix = "zlt.gateway.dynamicRoute", name = "dataType", havingValue = "nacos", matchIfMissing = true)
|
||||||
|
public class NacosDynRoute {
|
||||||
|
@Autowired
|
||||||
|
private NacosConfigProperties nacosConfigProperties;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public NacosRouteDefinitionRepository nacosRouteDefinitionRepository() {
|
||||||
|
return new NacosRouteDefinitionRepository(publisher, nacosConfigProperties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
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.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.web.server.SecurityWebFilterChain;
|
||||||
|
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
|
||||||
|
import org.springframework.security.web.server.authentication.ServerAuthenticationEntryPointFailureHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资源服务器配置
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/5
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ResourceServerConfiguration {
|
||||||
|
@Autowired
|
||||||
|
private SecurityProperties securityProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TokenStore tokenStore;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PermissionAuthManager permissionAuthManager;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||||
|
//认证处理器
|
||||||
|
ReactiveAuthenticationManager customAuthenticationManager = new CustomAuthenticationManager(tokenStore);
|
||||||
|
JsonAuthenticationEntryPoint entryPoint = new JsonAuthenticationEntryPoint();
|
||||||
|
//token转换器
|
||||||
|
ServerBearerTokenAuthenticationConverter tokenAuthenticationConverter = new ServerBearerTokenAuthenticationConverter();
|
||||||
|
tokenAuthenticationConverter.setAllowUriQueryParameter(true);
|
||||||
|
//oauth2认证过滤器
|
||||||
|
AuthenticationWebFilter oauth2Filter = new AuthenticationWebFilter(customAuthenticationManager);
|
||||||
|
oauth2Filter.setServerAuthenticationConverter(tokenAuthenticationConverter);
|
||||||
|
oauth2Filter.setAuthenticationFailureHandler(new ServerAuthenticationEntryPointFailureHandler(entryPoint));
|
||||||
|
oauth2Filter.setAuthenticationSuccessHandler(new Oauth2AuthSuccessHandler());
|
||||||
|
http.addFilterAt(oauth2Filter, SecurityWebFiltersOrder.AUTHENTICATION);
|
||||||
|
|
||||||
|
ServerHttpSecurity.AuthorizeExchangeSpec authorizeExchange = http.authorizeExchange();
|
||||||
|
if (securityProperties.getAuth().getHttpUrls().length > 0) {
|
||||||
|
authorizeExchange.pathMatchers(securityProperties.getAuth().getHttpUrls()).authenticated();
|
||||||
|
}
|
||||||
|
if (securityProperties.getIgnore().getUrls().length > 0) {
|
||||||
|
authorizeExchange.pathMatchers(securityProperties.getIgnore().getUrls()).permitAll();
|
||||||
|
}
|
||||||
|
authorizeExchange
|
||||||
|
.pathMatchers(HttpMethod.OPTIONS).permitAll()
|
||||||
|
.anyExchange()
|
||||||
|
.access(permissionAuthManager)
|
||||||
|
.and()
|
||||||
|
.exceptionHandling()
|
||||||
|
.accessDeniedHandler(new JsonAccessDeniedHandler())
|
||||||
|
.authenticationEntryPoint(entryPoint)
|
||||||
|
.and()
|
||||||
|
.headers()
|
||||||
|
.frameOptions()
|
||||||
|
.disable()
|
||||||
|
.and()
|
||||||
|
.httpBasic().disable()
|
||||||
|
.csrf().disable();
|
||||||
|
return http.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.central.gateway.feign;
|
||||||
|
|
||||||
|
import com.central.common.constant.ServiceNameConstants;
|
||||||
|
import com.central.common.model.SysMenu;
|
||||||
|
import com.central.gateway.feign.fallback.MenuServiceFallbackFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
*/
|
||||||
|
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = MenuServiceFallbackFactory.class, decode404 = true)
|
||||||
|
public interface MenuService {
|
||||||
|
/**
|
||||||
|
* 角色菜单列表
|
||||||
|
* @param roleCodes
|
||||||
|
*/
|
||||||
|
@GetMapping(value = "/menus/{roleCodes}")
|
||||||
|
List<SysMenu> findByRoleCodes(@PathVariable("roleCodes") String roleCodes);
|
||||||
|
}
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
package com.central.gateway.feign.fallback;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.central.gateway.feign.MenuService;
|
||||||
|
import feign.hystrix.FallbackFactory;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* menuService降级工场
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/1/18
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class MenuServiceFallbackFactory implements FallbackFactory<MenuService> {
|
||||||
|
@Override
|
||||||
|
public MenuService create(Throwable throwable) {
|
||||||
|
return roleIds -> {
|
||||||
|
log.error("调用findByRoleCodes异常:{}", roleIds, throwable);
|
||||||
|
return CollectionUtil.newArrayList();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
+47
@@ -0,0 +1,47 @@
|
|||||||
|
package com.central.gateway.filter;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.central.common.constant.CommonConstant;
|
||||||
|
import com.central.common.constant.ConfigConstants;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
|
||||||
|
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
|
||||||
|
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 传递负载均衡隔离值
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConditionalOnProperty(name = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
|
||||||
|
public class LbIsolationFilter extends LoadBalancerClientFilter {
|
||||||
|
public LbIsolationFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties) {
|
||||||
|
super(loadBalancer, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ServiceInstance choose(ServerWebExchange exchange) {
|
||||||
|
String vresion = exchange.getRequest().getHeaders().getFirst(CommonConstant.Z_L_T_VERSION);
|
||||||
|
if (StrUtil.isNotEmpty(vresion)) {
|
||||||
|
if (this.loadBalancer instanceof RibbonLoadBalancerClient) {
|
||||||
|
RibbonLoadBalancerClient client = (RibbonLoadBalancerClient) this.loadBalancer;
|
||||||
|
String serviceId = ((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost();
|
||||||
|
return client.choose(serviceId, vresion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.choose(exchange);
|
||||||
|
}
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
package com.central.gateway.filter;
|
||||||
|
|
||||||
|
import eu.bitwalker.useragentutils.UserAgent;
|
||||||
|
import com.central.gateway.utils.ReactiveAddrUtil;
|
||||||
|
import com.central.log.monitor.PointUtil;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求统计分析埋点过滤器
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RequestStatisticsFilter implements GlobalFilter, Ordered {
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
Map<String, String> headers = request.getHeaders().toSingleValueMap();
|
||||||
|
UserAgent userAgent = UserAgent.parseUserAgentString(headers.get("User-Agent"));
|
||||||
|
//埋点
|
||||||
|
PointUtil.debug("1", "request-statistics",
|
||||||
|
"ip=" + ReactiveAddrUtil.getRemoteAddr(request)
|
||||||
|
+ "&browser=" + userAgent.getBrowser()
|
||||||
|
+ "&operatingSystem=" + userAgent.getOperatingSystem());
|
||||||
|
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package com.central.gateway.filter;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import com.central.common.constant.CommonConstant;
|
||||||
|
import com.central.common.constant.SecurityConstants;
|
||||||
|
import com.central.log.properties.TraceProperties;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成日志链路追踪id,并传入header中
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class TraceFilter implements GlobalFilter, Ordered {
|
||||||
|
@Autowired
|
||||||
|
private TraceProperties traceProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
if (traceProperties.getEnable()) {
|
||||||
|
//链路追踪id
|
||||||
|
String traceId = IdUtil.fastSimpleUUID();
|
||||||
|
MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
|
||||||
|
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate()
|
||||||
|
.headers(h -> h.add(CommonConstant.TRACE_ID_HEADER, traceId))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
|
||||||
|
return chain.filter(build);
|
||||||
|
}
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Ordered.HIGHEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
}
|
||||||
+94
@@ -0,0 +1,94 @@
|
|||||||
|
package com.central.gateway.route;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.alibaba.nacos.api.config.listener.Listener;
|
||||||
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* nacos路由数据源
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class NacosRouteDefinitionRepository implements RouteDefinitionRepository {
|
||||||
|
private static final String SCG_DATA_ID = "scg-routes";
|
||||||
|
private static final String SCG_GROUP_ID = "SCG_GATEWAY";
|
||||||
|
|
||||||
|
private ApplicationEventPublisher publisher;
|
||||||
|
|
||||||
|
private NacosConfigProperties nacosConfigProperties;
|
||||||
|
|
||||||
|
public NacosRouteDefinitionRepository(ApplicationEventPublisher publisher, NacosConfigProperties nacosConfigProperties) {
|
||||||
|
this.publisher = publisher;
|
||||||
|
this.nacosConfigProperties = nacosConfigProperties;
|
||||||
|
addListener();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||||
|
try {
|
||||||
|
String content = nacosConfigProperties.configServiceInstance().getConfig(SCG_DATA_ID, SCG_GROUP_ID,5000);
|
||||||
|
List<RouteDefinition> routeDefinitions = getListByStr(content);
|
||||||
|
return Flux.fromIterable(routeDefinitions);
|
||||||
|
} catch (NacosException e) {
|
||||||
|
log.error("getRouteDefinitions by nacos error", e);
|
||||||
|
}
|
||||||
|
return Flux.fromIterable(CollUtil.newArrayList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加Nacos监听
|
||||||
|
*/
|
||||||
|
private void addListener() {
|
||||||
|
try {
|
||||||
|
nacosConfigProperties.configServiceInstance().addListener(SCG_DATA_ID, SCG_GROUP_ID, new Listener() {
|
||||||
|
@Override
|
||||||
|
public Executor getExecutor() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receiveConfigInfo(String configInfo) {
|
||||||
|
publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (NacosException e) {
|
||||||
|
log.error("nacos-addListener-error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> save(Mono<RouteDefinition> route) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> delete(Mono<String> routeId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<RouteDefinition> getListByStr(String content) {
|
||||||
|
if (StrUtil.isNotEmpty(content)) {
|
||||||
|
return JSONObject.parseArray(content, RouteDefinition.class);
|
||||||
|
}
|
||||||
|
return new ArrayList<>(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
+62
@@ -0,0 +1,62 @@
|
|||||||
|
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://blog.csdn.net/zlt2000
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
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://blog.csdn.net/zlt2000
|
||||||
|
* 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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
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://blog.csdn.net/zlt2000
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.central.gateway.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @date 2019/10/7
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ReactiveAddrUtil {
|
||||||
|
private final static String UNKNOWN_STR = "unknown";
|
||||||
|
/**
|
||||||
|
* 获取客户端IP地址
|
||||||
|
*/
|
||||||
|
public static String getRemoteAddr(ServerHttpRequest request) {
|
||||||
|
Map<String, String> headers = request.getHeaders().toSingleValueMap();
|
||||||
|
String ip = headers.get("X-Forwarded-For");
|
||||||
|
if (isEmptyIP(ip)) {
|
||||||
|
ip = headers.get("Proxy-Client-IP");
|
||||||
|
if (isEmptyIP(ip)) {
|
||||||
|
ip = headers.get("WL-Proxy-Client-IP");
|
||||||
|
if (isEmptyIP(ip)) {
|
||||||
|
ip = headers.get("HTTP_CLIENT_IP");
|
||||||
|
if (isEmptyIP(ip)) {
|
||||||
|
ip = headers.get("HTTP_X_FORWARDED_FOR");
|
||||||
|
if (isEmptyIP(ip)) {
|
||||||
|
ip = request.getRemoteAddress().getAddress().getHostAddress();
|
||||||
|
if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
|
||||||
|
// 根据网卡取本机配置的IP
|
||||||
|
ip = getLocalAddr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (ip.length() > 15) {
|
||||||
|
String[] ips = ip.split(",");
|
||||||
|
for (int index = 0; index < ips.length; index++) {
|
||||||
|
String strIp = ips[index];
|
||||||
|
if (!isEmptyIP(ip)) {
|
||||||
|
ip = strIp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isEmptyIP(String ip) {
|
||||||
|
if (StrUtil.isEmpty(ip) || UNKNOWN_STR.equalsIgnoreCase(ip)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取本机的IP地址
|
||||||
|
*/
|
||||||
|
public static String getLocalAddr() {
|
||||||
|
try {
|
||||||
|
return InetAddress.getLocalHost().getHostAddress();
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
log.error("InetAddress.getLocalHost()-error", e);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
discovery:
|
||||||
|
locator:
|
||||||
|
lowerCaseServiceId: true
|
||||||
|
enabled: true
|
||||||
|
routes:
|
||||||
|
- id: auth
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/api-uaa/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: auth-login-page
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/login.html
|
||||||
|
filters:
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: auth-login-process
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/user/login
|
||||||
|
filters:
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: auth-login-token
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/oauth/token
|
||||||
|
filters:
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: auth-login-authorize
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/oauth/authorize
|
||||||
|
filters:
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: auth-check-process
|
||||||
|
uri: lb://uaa-server
|
||||||
|
predicates:
|
||||||
|
- Path=/oauth/check_token
|
||||||
|
filters:
|
||||||
|
- PreserveHostHeader
|
||||||
|
- id: user
|
||||||
|
uri: lb://user-center
|
||||||
|
predicates:
|
||||||
|
- Path=/api-user/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- id: file
|
||||||
|
uri: lb://file-center
|
||||||
|
predicates:
|
||||||
|
- Path=/api-file/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- id: log
|
||||||
|
uri: lb://log-center
|
||||||
|
predicates:
|
||||||
|
- Path=/api-log/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- id: generator
|
||||||
|
uri: lb://code-generator
|
||||||
|
predicates:
|
||||||
|
- Path=/api-generator/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
- id: search
|
||||||
|
uri: lb://search-center
|
||||||
|
predicates:
|
||||||
|
- Path=/api-search/**
|
||||||
|
filters:
|
||||||
|
- StripPrefix=1
|
||||||
|
# sentinel:
|
||||||
|
# datasource.ds1.nacos:
|
||||||
|
# server-addr: ${zlt.nacos.server-addr}
|
||||||
|
# data-id: ${spring.application.name}-sentinel-gw-flow
|
||||||
|
# group-id: DEFAULT_GROUP
|
||||||
|
# ruleType: gw-flow
|
||||||
|
# filter:
|
||||||
|
# enabled: true
|
||||||
|
|
||||||
|
zlt:
|
||||||
|
swagger-agg:
|
||||||
|
generate-routes: user,file
|
||||||
|
oauth2:
|
||||||
|
token:
|
||||||
|
store:
|
||||||
|
type: redis
|
||||||
|
security:
|
||||||
|
ignore:
|
||||||
|
# 忽略认证的地址
|
||||||
|
httpUrls: >
|
||||||
|
/api-uaa/oauth/**,
|
||||||
|
/api-uaa/validata/**,
|
||||||
|
/api-uaa/css/**,
|
||||||
|
/api-uaa/images/**,
|
||||||
|
/api-uaa/js/**,
|
||||||
|
/api-user/users-anon/**,
|
||||||
|
/login.html,
|
||||||
|
/user/login
|
||||||
|
auth:
|
||||||
|
urlPermission:
|
||||||
|
#是否开启url级别权限
|
||||||
|
enable: false
|
||||||
|
#配置只进行登录认证,不进行url权限认证的api
|
||||||
|
ignoreUrls: >
|
||||||
|
/api-user/menus/current,
|
||||||
|
/api-user/users/current,
|
||||||
|
/api-log/requestStat
|
||||||
|
#白名单
|
||||||
|
includeClientIds:
|
||||||
|
- webApp
|
||||||
|
renew:
|
||||||
|
#是否开启token自动续签(目前只有redis实现)
|
||||||
|
enable: true
|
||||||
|
#白名单
|
||||||
|
includeClientIds:
|
||||||
|
- webApp
|
||||||
|
gateway:
|
||||||
|
#网关动态路由
|
||||||
|
dynamicRoute:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
hystrix:
|
||||||
|
command:
|
||||||
|
default:
|
||||||
|
execution:
|
||||||
|
isolation:
|
||||||
|
thread:
|
||||||
|
timeoutInMilliseconds: 1000
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
server:
|
||||||
|
port: 9900
|
||||||
|
|
||||||
|
zlt:
|
||||||
|
nacos:
|
||||||
|
server-addr: 192.168.28.130:8848
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: sc-gateway
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
config:
|
||||||
|
server-addr: ${zlt.nacos.server-addr}
|
||||||
|
file-extension: yml
|
||||||
|
shared-dataids: common.yml
|
||||||
|
refreshable-dataids: common.yml
|
||||||
|
discovery:
|
||||||
|
server-addr: ${zlt.nacos.server-addr}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjmgfejBXsPYynAIPczHA
|
||||||
|
eEvTDfAVaNKQudyI7VszdezbHDM1CStCIgwiMmLd7QYf1SrrmQoqxhcSRbhjE3ej
|
||||||
|
RF5qzhtx3kmepdpMrQptcsLjRkixaxCc4E2k6Us5707gGwbhoaTrRit5F2MnAdLY
|
||||||
|
C1TS3WwnO/hQfqUcAglbK8yrJ4AwAv0DAoIUSWnWqzuniV1SYbdV57uswxUssoWy
|
||||||
|
sEfPz+nv1ZLRs6Wz4eQ5Myqx2+CjWc9F8iXa2PV8Rmjms3dVbWcLUpCP18Dfzp8l
|
||||||
|
n8vF9LfYB7UaLSpfJe6FFF6+vCg4JHfo12djTUgwGjauMF3e9mmjU83KIoQS66lp
|
||||||
|
AQIDAQAB
|
||||||
|
-----END PUBLIC KEY-----
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user