init project v1.0.0
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
/target/
|
||||
*.iml
|
||||
@@ -0,0 +1,107 @@
|
||||
<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>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>zuul-gateway</artifactId>
|
||||
<description>zuul网关</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- 公共实体类模块 -->
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-auth-client-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-ribbon-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-oauth2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<!-- 移除tomcat容器-->
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- 替换undertow容器 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<!-- swagger -->
|
||||
<dependency>
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>swagger-butler-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>swagger-bootstrap-ui</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.zyplayer</groupId>
|
||||
<artifactId>swagger-mg-ui</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-extension</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</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>
|
||||
</plugins>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
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.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@EnableZuulProxy
|
||||
@EnableFeignClients
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class GatewayApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApp.class, args);
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import com.central.common.config.DefaultAsycTaskConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* 线程池配置、启用异步
|
||||
* @Async quartz 需要使用
|
||||
*/
|
||||
@Configuration
|
||||
public class AsycTaskExecutorConfig extends DefaultAsycTaskConfig {
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
/**
|
||||
* 跨域配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/8/9 15:59
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfigration {
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
// #允许向该服务器提交请求的URI,*表示全部允许
|
||||
config.addAllowedOrigin(CorsConfiguration.ALL);
|
||||
// 允许cookies跨域
|
||||
config.setAllowCredentials(true);
|
||||
// #允许访问的头信息,*表示全部
|
||||
config.addAllowedHeader(CorsConfiguration.ALL);
|
||||
// 允许提交请求的方法,*表示全部允许
|
||||
config.addAllowedMethod(CorsConfiguration.ALL);
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置过滤器
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean someFilterRegistration() {
|
||||
FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(corsFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
registration.setName("corsFilter");
|
||||
registration.setOrder(Integer.MIN_VALUE);
|
||||
return registration;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import com.central.common.config.DefaultPasswordConfig;
|
||||
import com.central.oauth2.common.config.DefaultResourceServerConf;
|
||||
import com.central.oauth2.common.config.TokenStoreConfig;
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
*/
|
||||
@Configuration
|
||||
@EnableResourceServer
|
||||
@EnableConfigurationProperties(SecurityProperties.class)
|
||||
@Import({DefaultPasswordConfig.class, TokenStoreConfig.class})
|
||||
public class ResourceServerConfiguration extends DefaultResourceServerConf {
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
@Override
|
||||
public void configure(HttpSecurity http) throws Exception {
|
||||
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl = setHttp(http)
|
||||
.authorizeRequests()
|
||||
.antMatchers(securityProperties.getAuth().getHttpUrls()).authenticated()
|
||||
.antMatchers(securityProperties.getIgnore().getUrls()).permitAll()
|
||||
.antMatchers(HttpMethod.OPTIONS).permitAll()
|
||||
.anyRequest();
|
||||
setAuthenticate(authorizedUrl);
|
||||
//允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
|
||||
http.headers()
|
||||
.frameOptions()
|
||||
.disable()
|
||||
.and()
|
||||
.csrf().disable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpSecurity setAuthenticate(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.AuthorizedUrl authorizedUrl) {
|
||||
return authorizedUrl.access("@permissionService.hasPermission(request, authentication)").and();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.central.gateway.config;
|
||||
|
||||
import com.didispace.swagger.butler.EnableSwaggerButler;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* swagger 聚合文档配置
|
||||
* zuul routers 映射具体服务的/v2/api-docs swagger
|
||||
*/
|
||||
@Configuration
|
||||
@EnableSwaggerButler
|
||||
public class SwaggerConfig {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.central.gateway.feign.fallback;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysMenu;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.gateway.feign.MenuService;
|
||||
import feign.hystrix.FallbackFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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();
|
||||
};
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.central.gateway.filter.pre;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORM_BODY_WRAPPER_FILTER_ORDER;
|
||||
|
||||
/**
|
||||
* 将认证用户的相关信息放入header中, 后端服务可以直接读取使用
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/11/20
|
||||
*/
|
||||
@Component
|
||||
public class UserInfoHeaderFilter extends ZuulFilter {
|
||||
@Override
|
||||
public String filterType() {
|
||||
return FilterConstants.PRE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return FORM_BODY_WRAPPER_FILTER_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
|
||||
Object principal = authentication.getPrincipal();
|
||||
String userInfo;
|
||||
if (principal instanceof SysUser) {
|
||||
SysUser user = (SysUser) principal;
|
||||
userInfo = user.getUsername() + SecurityConstants.USER_SPLIT + user.getId();
|
||||
} else {
|
||||
//jwt的token只有name
|
||||
userInfo = authentication.getName();
|
||||
}
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, userInfo);
|
||||
ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ","));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.central.gateway.service;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 请求权限判断service
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/10/28
|
||||
*/
|
||||
public interface IPermissionService {
|
||||
/**
|
||||
* 判断请求是否有权限
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @param authentication 认证信息
|
||||
* @return 是否有权限
|
||||
*/
|
||||
boolean hasPermission(HttpServletRequest request, Authentication authentication);
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.central.gateway.service.impl;
|
||||
|
||||
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.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 请求权限判断service
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/10/28
|
||||
*/
|
||||
@Slf4j
|
||||
@Service("permissionService")
|
||||
public class PermissionServiceImpl extends DefaultPermissionServiceImpl {
|
||||
@Resource
|
||||
private MenuService menuService;
|
||||
|
||||
@Override
|
||||
public List<SysMenu> findMenuByRoleCodes(String roleCodes) {
|
||||
return menuService.findByRoleCodes(roleCodes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
spring:
|
||||
redis:
|
||||
host: ${zlt.redis.host}
|
||||
port: ${zlt.redis.port}
|
||||
timeout: ${zlt.redis.timeout}
|
||||
|
||||
zlt:
|
||||
oauth2:
|
||||
token:
|
||||
store:
|
||||
type: resJwt
|
||||
security:
|
||||
ignore:
|
||||
# 忽略认证的地址
|
||||
httpUrls: >
|
||||
/api-uaa/**,
|
||||
/api-user/users-anon/**,
|
||||
/login.html,
|
||||
/user/login
|
||||
# 配置只认证登录,登录后所有角色都能访问的url
|
||||
menusPaths: /api-user/menus/current
|
||||
auth:
|
||||
# 配置必需认证的url
|
||||
httpUrls: /api-uaa/clients/**
|
||||
#是否开启url级别权限
|
||||
urlEnabled: false
|
||||
|
||||
zuul:
|
||||
ribbon-isolation-strategy: thread
|
||||
ribbon:
|
||||
eager-load:
|
||||
enabled: true
|
||||
threadPool:
|
||||
useSeparateThreadPools: true
|
||||
threadPoolKeyPrefix: api-gateway
|
||||
#关闭重试
|
||||
retryable: false
|
||||
#/oauth/token需要请求头处理
|
||||
sensitive-headers:
|
||||
ignored-headers: Access-Control-Allow-Credentials,Access-Control-Allow-Origin,Access-Control-Allow-Methods
|
||||
add-host-header: true
|
||||
routes:
|
||||
auth:
|
||||
path: /api-uaa/**
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
auth-login-page:
|
||||
path: /login.html
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
auth-login-process:
|
||||
path: /user/login
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
auth-login-token:
|
||||
path: /oauth/token
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
auth-login-authorize:
|
||||
path: /oauth/authorize
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
auth-check-process:
|
||||
path: /oauth/check_token
|
||||
service-id: uaa-server
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
user:
|
||||
path: /api-user/**
|
||||
service-id: user-center
|
||||
strip-prefix: true
|
||||
custom-sensitive-headers: true
|
||||
file:
|
||||
path: /api-file/**
|
||||
service-id: file-center
|
||||
strip-prefix: true
|
||||
log:
|
||||
path: /api-log/**
|
||||
service-id: log-center
|
||||
strip-prefix: true
|
||||
generator:
|
||||
path: /api-generator/**
|
||||
service-id: code-generator
|
||||
strip-prefix: true
|
||||
ssl-hostname-validation-enabled: false
|
||||
|
||||
swagger:
|
||||
butler:
|
||||
api-docs-path: v2/api-docs
|
||||
swagger-version: 2.0
|
||||
auto-generate-from-zuul-routes: true
|
||||
generate-routes: auth,user,file
|
||||
@@ -0,0 +1,19 @@
|
||||
server:
|
||||
port: 9200
|
||||
undertow:
|
||||
io-threads: 16
|
||||
worker-threads: 256
|
||||
buffer-size: 1024
|
||||
direct-buffers: true
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: api-gateway
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: ${zlt.nacos.server-addr}
|
||||
file-extension: yml
|
||||
shared-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-----
|
||||
Reference in New Issue
Block a user