init
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.sl;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GatewayApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.sl.gateway.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "sl")
|
||||
public class MyConfig {
|
||||
|
||||
private String[] noAuthPaths;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.sl.transport.common.constant.Constants;
|
||||
|
||||
/**
|
||||
* 鉴权业务的回调,具体逻辑由 GatewayFilterFactory 具体完成
|
||||
*
|
||||
* @author zzj
|
||||
* @version 1.0
|
||||
*/
|
||||
public interface AuthFilter {
|
||||
|
||||
/**
|
||||
* 校验token
|
||||
*
|
||||
* @param token 请求中的token
|
||||
* @return token中携带的数据
|
||||
*/
|
||||
AuthUserInfoDTO check(String token);
|
||||
|
||||
/**
|
||||
* 鉴权
|
||||
*
|
||||
* @param token 请求中的token
|
||||
* @param authUserInfo token中携带的数据
|
||||
* @param path 当前请求的路径
|
||||
* @return 是否通过
|
||||
*/
|
||||
Boolean auth(String token, AuthUserInfoDTO authUserInfo, String path);
|
||||
|
||||
/**
|
||||
* 请求中携带token的名称
|
||||
*
|
||||
* @return 头名称
|
||||
*/
|
||||
default String tokenHeaderName() {
|
||||
return Constants.GATEWAY.AUTHORIZATION;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.itheima.auth.sdk.service.TokenCheckService;
|
||||
import com.sl.gateway.config.MyConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 快递员token拦截处理
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CourierTokenGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> implements AuthFilter {
|
||||
|
||||
@Resource
|
||||
private MyConfig myConfig;
|
||||
@Resource
|
||||
private TokenCheckService tokenCheckService;
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Object config) {
|
||||
return new TokenGatewayFilter(this.myConfig, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthUserInfoDTO check(String token) {
|
||||
//校验token
|
||||
return tokenCheckService.parserToken(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean auth(String token, AuthUserInfoDTO authUserInfoDTO, String path) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.sl.gateway.config.MyConfig;
|
||||
import com.sl.gateway.properties.JwtProperties;
|
||||
import com.sl.transport.common.constant.Constants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 用户端token拦截处理
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CustomerTokenGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> implements AuthFilter {
|
||||
|
||||
@Resource
|
||||
private MyConfig myConfig;
|
||||
|
||||
@Resource
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Object config) {
|
||||
return new TokenGatewayFilter(this.myConfig, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthUserInfoDTO check(String token) {
|
||||
// 普通用户的token没有对接权限系统,需要自定实现
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean auth(String token, AuthUserInfoDTO authUserInfoDTO, String path) {
|
||||
//普通用户不需要校验角色
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tokenHeaderName() {
|
||||
return Constants.GATEWAY.ACCESS_TOKEN;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.itheima.auth.sdk.service.TokenCheckService;
|
||||
import com.sl.gateway.config.MyConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 司机端token拦截处理
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DriverTokenGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> implements AuthFilter {
|
||||
|
||||
@Resource
|
||||
private MyConfig myConfig;
|
||||
|
||||
@Resource
|
||||
private TokenCheckService tokenCheckService;
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Object config) {
|
||||
return new TokenGatewayFilter(this.myConfig, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthUserInfoDTO check(String token) {
|
||||
//校验token
|
||||
return this.tokenCheckService.parserToken(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean auth(String token, AuthUserInfoDTO authUserInfoDTO, String path) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.itheima.auth.sdk.service.TokenCheckService;
|
||||
import com.sl.gateway.config.MyConfig;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 后台管理员token拦截处理
|
||||
*/
|
||||
@Component
|
||||
public class ManagerTokenGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> implements AuthFilter {
|
||||
|
||||
@Resource
|
||||
private MyConfig myConfig;
|
||||
@Resource
|
||||
private TokenCheckService tokenCheckService;
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Object config) {
|
||||
//由于实现了AuthFilter接口,所以可以传递this对象到TokenGatewayFilter中
|
||||
return new TokenGatewayFilter(this.myConfig, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthUserInfoDTO check(String token) {
|
||||
//校验token
|
||||
return tokenCheckService.parserToken(token);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean auth(String token, AuthUserInfoDTO authUserInfoDTO, String path) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.sl.gateway.filter;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.sl.gateway.config.MyConfig;
|
||||
import com.sl.transport.common.constant.Constants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Slf4j
|
||||
public class TokenGatewayFilter implements GatewayFilter, Ordered {
|
||||
|
||||
private MyConfig myConfig;
|
||||
private AuthFilter authFilter;
|
||||
|
||||
public TokenGatewayFilter(MyConfig myConfig, AuthFilter authFilter) {
|
||||
this.myConfig = myConfig;
|
||||
this.authFilter = authFilter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
//获取请求路径
|
||||
String path = exchange.getRequest().getPath().toString();
|
||||
//查看请求路径是否在白名单中
|
||||
if (StrUtil.startWithAny(path, myConfig.getNoAuthPaths())) {
|
||||
//无需校验,直接放行
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
//获取header的参数
|
||||
String token = exchange.getRequest().getHeaders().getFirst(this.authFilter.tokenHeaderName());
|
||||
if (StrUtil.isEmpty(token)) {
|
||||
//没有权限
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
|
||||
//校验token
|
||||
AuthUserInfoDTO authUserInfoDTO = null;
|
||||
try { //捕获token校验异常
|
||||
authUserInfoDTO = this.authFilter.check(token);
|
||||
} catch (Exception e) {
|
||||
log.error("令牌校验失败,token = {}, path= {}", token, path, e);
|
||||
}
|
||||
if (ObjectUtil.isEmpty(authUserInfoDTO)) {
|
||||
//token失效 或 伪造,响应401
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
|
||||
//鉴权
|
||||
Boolean result = false;
|
||||
try { //捕获鉴权异常
|
||||
result = this.authFilter.auth(token, authUserInfoDTO, path);
|
||||
} catch (Exception e) {
|
||||
log.error("权限校验失败,token = {}, path= {}", token, path, e);
|
||||
}
|
||||
if (!result) {
|
||||
//没有权限,响应400
|
||||
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
|
||||
//增加参数,向下游微服务传递参数
|
||||
exchange.getRequest().mutate().header(Constants.GATEWAY.USERINFO, JSONUtil.toJsonStr(authUserInfoDTO));
|
||||
exchange.getRequest().mutate().header(Constants.GATEWAY.TOKEN, token);
|
||||
|
||||
//校验通过放行
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
//指定了拦截器的顺序,设置最小值确定第一个被执行
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sl.gateway.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "sl.jwt")
|
||||
@Data
|
||||
public class JwtProperties {
|
||||
|
||||
private String publicKey;
|
||||
}
|
||||
BIN
sl-express-gateway/src/main/resources/auth/pub.key
Normal file
BIN
sl-express-gateway/src/main/resources/auth/pub.key
Normal file
Binary file not shown.
7
sl-express-gateway/src/main/resources/banner.txt
Normal file
7
sl-express-gateway/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
_ ${spring.application.name} ${application.version}
|
||||
___ | | ___ __ __ _ __ _ __ ___ ___ ___ Port: ${server.port}
|
||||
/ __|| | _____ / _ \\ \/ /| '_ \ | '__|/ _ \/ __|/ __| Pid: ${pid} Profile(s): ${AnsiColor.GREEN}${spring.profiles.active}${AnsiColor.DEFAULT}
|
||||
\__ \| ||_____|| __/ > < | |_) || | | __/\__ \\__ \
|
||||
|___/|_| \___|/_/\_\| .__/ |_| \___||___/|___/ https://sl-express.itheima.net/
|
||||
|_|
|
||||
117
sl-express-gateway/src/main/resources/bootstrap-local.yml
Normal file
117
sl-express-gateway/src/main/resources/bootstrap-local.yml
Normal file
@@ -0,0 +1,117 @@
|
||||
server:
|
||||
port: 9527
|
||||
tomcat:
|
||||
uri-encoding: UTF-8
|
||||
threads:
|
||||
max: 1000
|
||||
min-spare: 30
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
server-addr: 192.168.150.101:8848
|
||||
discovery:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
ip: 192.168.150.1
|
||||
config:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
gateway:
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowed-origin-patterns: "*"
|
||||
allowed-headers: "*"
|
||||
allow-credentials: true
|
||||
allowed-methods:
|
||||
- GET
|
||||
- POST
|
||||
- DELETE
|
||||
- PUT
|
||||
- OPTION
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
|
||||
routes:
|
||||
- id: sl-express-ms-web-courier
|
||||
uri: lb://sl-express-ms-web-courier
|
||||
predicates:
|
||||
- Path=/courier/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CourierToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-customer
|
||||
uri: lb://sl-express-ms-web-customer
|
||||
predicates:
|
||||
- Path=/customer/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CustomerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-driver
|
||||
uri: lb://sl-express-ms-web-driver
|
||||
predicates:
|
||||
- Path=/driver/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- DriverToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-manager
|
||||
uri: lb://sl-express-ms-web-manager
|
||||
predicates:
|
||||
- Path=/manager/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- ManagerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-trade
|
||||
uri: lb://sl-express-ms-trade
|
||||
predicates:
|
||||
- Path=/trade/notify/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
itcast:
|
||||
authority:
|
||||
host: ${authority.host} #authority服务地址,根据实际情况更改
|
||||
port: ${authority.port} #authority服务端口
|
||||
timeout: ${authority.timeout} #http请求的超时时间
|
||||
public-key-file: auth/pub.key
|
||||
applicationId: ${authority.applicationId}
|
||||
|
||||
#角色id
|
||||
role:
|
||||
manager: ${role.manager}
|
||||
courier: ${role.courier}
|
||||
driver: ${role.driver}
|
||||
|
||||
sl:
|
||||
noAuthPaths:
|
||||
- /courier/login/account
|
||||
- /courier/swagger-ui.html
|
||||
- /courier/webjars/
|
||||
- /courier/swagger-resources
|
||||
- /courier/v2/api-docs
|
||||
- /courier/doc.html
|
||||
- /customer/user/login
|
||||
- /customer/user/refresh
|
||||
- /customer/swagger-ui.html
|
||||
- /customer/webjars/
|
||||
- /customer/swagger-resources
|
||||
- /customer/v2/api-docs
|
||||
- /customer/doc.html
|
||||
- /driver/login/account
|
||||
- /driver/swagger-ui.html
|
||||
- /driver/webjars/
|
||||
- /driver/swagger-resources
|
||||
- /driver/v2/api-docs
|
||||
- /driver/doc.html
|
||||
- /manager/login
|
||||
- /manager/webjars/
|
||||
- /manager/swagger-resources
|
||||
- /manager/v2/api-docs
|
||||
- /manager/doc.html
|
||||
- /manager/captcha
|
||||
jwt:
|
||||
public-key: ${sl.jwt.user-secret-key}
|
||||
116
sl-express-gateway/src/main/resources/bootstrap-stu.yml
Normal file
116
sl-express-gateway/src/main/resources/bootstrap-stu.yml
Normal file
@@ -0,0 +1,116 @@
|
||||
server:
|
||||
port: 9527
|
||||
tomcat:
|
||||
uri-encoding: UTF-8
|
||||
threads:
|
||||
max: 1000
|
||||
min-spare: 30
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
server-addr: 192.168.150.101:8848
|
||||
discovery:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
config:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
gateway:
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowed-origin-patterns: "*"
|
||||
allowed-headers: "*"
|
||||
allow-credentials: true
|
||||
allowed-methods:
|
||||
- GET
|
||||
- POST
|
||||
- DELETE
|
||||
- PUT
|
||||
- OPTION
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
|
||||
routes:
|
||||
- id: sl-express-ms-web-courier
|
||||
uri: lb://sl-express-ms-web-courier
|
||||
predicates:
|
||||
- Path=/courier/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CourierToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-customer
|
||||
uri: lb://sl-express-ms-web-customer
|
||||
predicates:
|
||||
- Path=/customer/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CustomerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-driver
|
||||
uri: lb://sl-express-ms-web-driver
|
||||
predicates:
|
||||
- Path=/driver/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- DriverToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-manager
|
||||
uri: lb://sl-express-ms-web-manager
|
||||
predicates:
|
||||
- Path=/manager/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- ManagerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-trade
|
||||
uri: lb://sl-express-ms-trade
|
||||
predicates:
|
||||
- Path=/trade/notify/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
itcast:
|
||||
authority:
|
||||
host: ${authority.host} #authority服务地址,根据实际情况更改
|
||||
port: ${authority.port} #authority服务端口
|
||||
timeout: ${authority.timeout} #http请求的超时时间
|
||||
public-key-file: auth/pub.key
|
||||
applicationId: ${authority.applicationId}
|
||||
|
||||
#角色id
|
||||
role:
|
||||
manager: ${role.manager}
|
||||
courier: ${role.courier}
|
||||
driver: ${role.driver}
|
||||
|
||||
sl:
|
||||
noAuthPaths:
|
||||
- /courier/login/account
|
||||
- /courier/swagger-ui.html
|
||||
- /courier/webjars/
|
||||
- /courier/swagger-resources
|
||||
- /courier/v2/api-docs
|
||||
- /courier/doc.html
|
||||
- /customer/user/login
|
||||
- /customer/user/refresh
|
||||
- /customer/swagger-ui.html
|
||||
- /customer/webjars/
|
||||
- /customer/swagger-resources
|
||||
- /customer/v2/api-docs
|
||||
- /customer/doc.html
|
||||
- /driver/login/account
|
||||
- /driver/swagger-ui.html
|
||||
- /driver/webjars/
|
||||
- /driver/swagger-resources
|
||||
- /driver/v2/api-docs
|
||||
- /driver/doc.html
|
||||
- /manager/login
|
||||
- /manager/webjars/
|
||||
- /manager/swagger-resources
|
||||
- /manager/v2/api-docs
|
||||
- /manager/doc.html
|
||||
- /manager/captcha
|
||||
jwt:
|
||||
public-key: ${sl.jwt.user-secret-key}
|
||||
116
sl-express-gateway/src/main/resources/bootstrap-test.yml
Normal file
116
sl-express-gateway/src/main/resources/bootstrap-test.yml
Normal file
@@ -0,0 +1,116 @@
|
||||
server:
|
||||
port: 9527
|
||||
tomcat:
|
||||
uri-encoding: UTF-8
|
||||
threads:
|
||||
max: 1000
|
||||
min-spare: 30
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
username: nacos
|
||||
password: nacos
|
||||
server-addr: 192.168.150.101:8848
|
||||
discovery:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
config:
|
||||
namespace: ecae68ba-7b43-4473-a980-4ddeb6157bdc
|
||||
gateway:
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowed-origin-patterns: "*"
|
||||
allowed-headers: "*"
|
||||
allow-credentials: true
|
||||
allowed-methods:
|
||||
- GET
|
||||
- POST
|
||||
- DELETE
|
||||
- PUT
|
||||
- OPTION
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
|
||||
routes:
|
||||
- id: sl-express-ms-web-courier
|
||||
uri: lb://sl-express-ms-web-courier
|
||||
predicates:
|
||||
- Path=/courier/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CourierToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-customer
|
||||
uri: lb://sl-express-ms-web-customer
|
||||
predicates:
|
||||
- Path=/customer/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- CustomerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-driver
|
||||
uri: lb://sl-express-ms-web-driver
|
||||
predicates:
|
||||
- Path=/driver/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- DriverToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-web-manager
|
||||
uri: lb://sl-express-ms-web-manager
|
||||
predicates:
|
||||
- Path=/manager/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- ManagerToken
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
- id: sl-express-ms-trade
|
||||
uri: lb://sl-express-ms-trade
|
||||
predicates:
|
||||
- Path=/trade/notify/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- AddRequestHeader=X-Request-From, sl-express-gateway
|
||||
itcast:
|
||||
authority:
|
||||
host: ${authority.host} #authority服务地址,根据实际情况更改
|
||||
port: ${authority.port} #authority服务端口
|
||||
timeout: ${authority.timeout} #http请求的超时时间
|
||||
public-key-file: auth/pub.key
|
||||
applicationId: ${authority.applicationId}
|
||||
|
||||
#角色id
|
||||
role:
|
||||
manager: ${role.manager}
|
||||
courier: ${role.courier}
|
||||
driver: ${role.driver}
|
||||
|
||||
sl:
|
||||
noAuthPaths:
|
||||
- /courier/login/account
|
||||
- /courier/swagger-ui.html
|
||||
- /courier/webjars/
|
||||
- /courier/swagger-resources
|
||||
- /courier/v2/api-docs
|
||||
- /courier/doc.html
|
||||
- /customer/user/login
|
||||
- /customer/user/refresh
|
||||
- /customer/swagger-ui.html
|
||||
- /customer/webjars/
|
||||
- /customer/swagger-resources
|
||||
- /customer/v2/api-docs
|
||||
- /customer/doc.html
|
||||
- /driver/login/account
|
||||
- /driver/swagger-ui.html
|
||||
- /driver/webjars/
|
||||
- /driver/swagger-resources
|
||||
- /driver/v2/api-docs
|
||||
- /driver/doc.html
|
||||
- /manager/login
|
||||
- /manager/webjars/
|
||||
- /manager/swagger-resources
|
||||
- /manager/v2/api-docs
|
||||
- /manager/doc.html
|
||||
- /manager/captcha
|
||||
jwt:
|
||||
public-key: ${sl.jwt.user-secret-key}
|
||||
15
sl-express-gateway/src/main/resources/bootstrap.yml
Normal file
15
sl-express-gateway/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
application:
|
||||
version: v1.0
|
||||
spring:
|
||||
main:
|
||||
#common中引入了springboot-web,由于Gateway采用webflux实现,需要需要设置应用类型为:reactive
|
||||
web-application-type: reactive
|
||||
application:
|
||||
name: sl-express-gateway
|
||||
profiles:
|
||||
active: local
|
||||
mvc:
|
||||
pathmatch:
|
||||
#解决异常:swagger Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
|
||||
#因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher
|
||||
matching-strategy: ant_path_matcher
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.sl.gateway.auth;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.itheima.auth.boot.autoconfigure.AuthorityProperties;
|
||||
import com.itheima.auth.factory.AuthTemplateFactory;
|
||||
import com.itheima.auth.sdk.AuthTemplate;
|
||||
import com.itheima.auth.sdk.common.Result;
|
||||
import com.itheima.auth.sdk.dto.AuthUserInfoDTO;
|
||||
import com.itheima.auth.sdk.dto.LoginDTO;
|
||||
import com.itheima.auth.sdk.dto.UserDTO;
|
||||
import com.itheima.auth.sdk.service.TokenCheckService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author zzj
|
||||
* @version 1.0
|
||||
*/
|
||||
@SpringBootTest(properties = "spring.main.web-application-type = reactive")
|
||||
public class AuthTemplateTest {
|
||||
|
||||
@Resource
|
||||
private AuthTemplate authTemplate;
|
||||
@Resource
|
||||
private TokenCheckService tokenCheckService;
|
||||
@Autowired
|
||||
private AuthorityProperties authorityProperties;
|
||||
|
||||
@Test
|
||||
public void testLogin() {
|
||||
//登录
|
||||
Result<LoginDTO> result = this.authTemplate.opsForLogin()
|
||||
.token("zhangsan", "123456");
|
||||
|
||||
String token = result.getData().getToken().getToken();
|
||||
System.out.println("token为:" + token);
|
||||
|
||||
UserDTO user = result.getData().getUser();
|
||||
System.out.println("user信息:" + user);
|
||||
|
||||
//查询角色
|
||||
Result<List<Long>> resultRole = AuthTemplateFactory.get(token).opsForRole()
|
||||
.findRoleByUserId(user.getId());
|
||||
System.out.println(resultRole);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkToken() {
|
||||
//上面方法中生成的token
|
||||
String token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMDAyNjIxMzAwOTkwMDc2NzA1IiwiYWNjb3VudCI6InpoYW5nc2FuIiwibmFtZSI6IuW8oOS4iSIsIm9yZ2lkIjoxMDAyNjE5NTU4MzU3NDI1OTUzLCJzdGF0aW9uaWQiOjk4MTIyMzcwMzMzNTQxMDYyNSwiYWRtaW5pc3RyYXRvciI6ZmFsc2UsImV4cCI6MTY1OTEzNDA0MH0.WBZaeBvmuw202raw7JvvHnIMpST28d0gv6ufVDenL_iGQwdClucUfd3YPLg9BLoiosaP16SEuB1nM_-HWl8rUA";
|
||||
AuthUserInfoDTO authUserInfo = this.tokenCheckService.parserToken(token);
|
||||
System.out.println(authUserInfo);
|
||||
|
||||
System.out.println(JSONUtil.toJsonStr(authUserInfo));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user