19. 网关服务开发
pd-gateway作为通用权限系统的网关服务,前端的http请求首先需要经过网关服务处理,再通过网关服务的路由功能转发到权限服务或者其他微服务进行业务处理。我们可以在网关服务进行统一的jwt令牌解析、鉴权相关操作。
19.1 配置文件
19.1.1 bootstrap.yml
由于我们当前使用的是Nacos作为整个项目的配置中心,所以Spring Boot的大部分配置文件都在Nacos中进行统一配置,我们的项目中只需要按照Spring Boot的要求在resources目录下提供bootstrap.yml配置文件即可,文件内容如下:
pinda:
# docker部署时,需要指定, 表示运行该服务的宿主机IP
local-ip: ${LOCAL_IP:${spring.cloud.client.ip-address}}
nacos:
ip: ${NACOS_IP:@pom.nacos.ip@}
port: ${NACOS_PORT:@pom.nacos.port@}
namespace: ${NACOS_ID:@pom.nacos.namespace@}
spring:
main:
allow-bean-definition-overriding: true
application:
name: @project.artifactId@ #pd-gateway
profiles:
active: @pom.profile.name@ #dev
cloud:
nacos:
config:
server-addr: ${pinda.nacos.ip}:${pinda.nacos.port}
file-extension: yml
namespace: ${pinda.nacos.namespace}
shared-dataids: common.yml,redis.yml,mysql.yml
refreshable-dataids: common.yml
enabled: true
discovery:
server-addr: ${pinda.nacos.ip}:${pinda.nacos.port}
namespace: ${pinda.nacos.namespace}
metadata:
management.context-path: ${server.servlet.context-path:}${spring.mvc.servlet.path:}${management.endpoints.web.base-path:}
#http://localhost:8760/api/gate/actuator
19.1.2 logback-spring.xml
由于pd-gateway已经添加了pd-tools-log模块的依赖,所以可以在项目中使用logback记录日志信息。在resources目录下提供logback-spring.xml配置文件,Spring Boot默认就可以加载到,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="com/itheima/pinda/log/logback/pinda-defaults.xml"/>
<springProfile name="test,docker,prod">
<logger name="com.itheima.pinda.zuul" additivity="true" level="INFO">
<appender-ref ref="ASYNC_CONTROLLER_APPENDER"/>
</logger>
</springProfile>
<springProfile name="dev">
<logger name="com.itheima.pinda.zuul" additivity="true" level="INFO">
<appender-ref ref="CONTROLLER_APPENDER"/>
</logger>
</springProfile>
</configuration>
19.1.3 j2cache配置文件
在当前pd-gateway项目中会使用到j2cache来操作缓存,在Nacos配置中心的redis.yml中已经配置了j2cache的相关配置:
j2cache:
# config-location: /j2cache.properties
open-spring-cache: true
cache-clean-mode: passive
allow-null-values: true
redis-client: lettuce
l2-cache-open: true
# l2-cache-open: false # 关闭二级缓存
broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
# broadcast: jgroups # 关闭二级缓存
L1:
provider_class: caffeine
L2:
provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
config_section: lettuce
sync_ttl_to_redis: true
default_cache_null_object: false
serialization: fst
caffeine:
properties: /j2cache/caffeine.properties # 这个配置文件需要放在项目中
lettuce:
mode: single
namespace:
storage: generic
channel: j2cache
scheme: redis
hosts: ${pinda.redis.ip}:${pinda.redis.port}
password: ${pinda.redis.password}
database: ${pinda.redis.database}
sentinelMasterId:
maxTotal: 100
maxIdle: 10
minIdle: 10
timeout: 10000
通过上面的配置可以看到,还需要在项目中提供/j2cache/caffeine.properties,文件内容如下:
#########################################
# Caffeine configuration
# \u6682\u65F6\u6CA1\u7528
# [name] = size, xxxx[s|m|h|d]
#########################################
default=2000, 2h
resource=2000, 1h
19.1.4 密钥文件
JWT签名算法中,一般有两个选择:HS256和RS256。
HS256 (带有 SHA-256 的 HMAC )是一种对称加密算法, 双方之间仅共享一个密钥。由于使用相同的密钥生成签名和验证签名, 因此必须注意确保密钥不被泄密。
RS256 (采用SHA-256 的 RSA 签名) 是一种非对称加密算法, 它使用公共/私钥对: JWT的提供方采用私钥生成签名, JWT 的使用方获取公钥以验证签名。
本项目中使用RS256非对称加密算法进行签名,这就需要使用RSA生成一对公钥和私钥。在授课资料中已经提供了一对公钥和私钥,其中pub.key为公钥,pri.key为私钥。
前面我们已经提到,在当前网关服务中我们需要对客户端请求中携带的jwt token进行解析,只需要公钥就可以。将授课资料中的pub.key文件复制到pd-gateway项目的resources/client下。
19.2 启动类
package com.itheima.pinda;
import com.itheima.pinda.auth.client.EnableAuthClient;
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;
/**
*网关启动类
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients({"com.itheima.pinda"})
@EnableZuulProxy//开启网关代理
@EnableAuthClient//开启授权客户端,开启后就可以使用pd-tools-jwt提供的工具类进行jwt token解析了
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}
19.3 配置类
package com.itheima.pinda.zuul.config;
import com.itheima.pinda.common.config.BaseConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* 解决跨域问题
*/
@Configuration
public class ZuulConfiguration extends BaseConfig {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source =
new UrlBasedCorsConfigurationSource();
final org.springframework.web.cors.CorsConfiguration config =
new org.springframework.web.cors.CorsConfiguration();
// 允许cookies跨域
config.setAllowCredentials(true);
// #允许向该服务器提交请求的URI,*表示全部允许
config.addAllowedOrigin("*");
// #允许访问的头信息,*表示全部
config.addAllowedHeader("*");
// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
config.setMaxAge(18000L);
// 允许提交请求的方法,*表示全部允许
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
// 允许Get的请求类型
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
19.4 API接口和熔断器
在网关服务中会通过Feign来调用权限服务获取相关信息,所以需要定义API接口和对应的熔断器类
package com.itheima.pinda.zuul.api;
import com.itheima.pinda.authority.dto.auth.ResourceQueryDTO;
import com.itheima.pinda.authority.entity.auth.Resource;
import com.itheima.pinda.base.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(name = "${pinda.feign.authority-server:pd-auth-server}",
fallback = ResourceApiFallback.class)
public interface ResourceApi {
//获取所有需要鉴权的资源
@GetMapping("/resource/list")
public R<List> list();
//查询当前登录用户拥有的资源权限
@GetMapping("/resource")
public R<List<Resource>> visible(ResourceQueryDTO resource);
}
package com.itheima.pinda.zuul.api;
import com.itheima.pinda.authority.dto.auth.ResourceQueryDTO;
import com.itheima.pinda.authority.entity.auth.Resource;
import com.itheima.pinda.base.R;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 资源API熔断
*/
@Component
public class ResourceApiFallback implements ResourceApi {
public R<List> list() {
return null;
}
public R<List<Resource>> visible(ResourceQueryDTO resource) {
return null;
}
}
19.5 过滤器
在网关服务中我们需要通过过滤器来实现jwt token解析
和鉴权
相关处理。
19.5.1 BaseFilter
BaseFilter作为基础过滤器,统一抽取一些公共属性和方法。
package com.itheima.pinda.zuul.filter;
import javax.servlet.http.HttpServletRequest;
import com.itheima.pinda.base.R;
import com.itheima.pinda.common.adapter.IgnoreTokenConfig;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
/**
* 基础 网关过滤器
*/
@Slf4j
public abstract class BaseFilter extends ZuulFilter {
@Value("${server.servlet.context-path}")
protected String zuulPrefix;
/**
* 判断当前请求uri是否需要忽略
*/
protected boolean isIgnoreToken() {
HttpServletRequest request =
RequestContext.getCurrentContext().getRequest();
String uri = request.getRequestURI();
uri = StrUtil.subSuf(uri, zuulPrefix.length());
uri = StrUtil.subSuf(uri, uri.indexOf("/", 1));
boolean ignoreToken = IgnoreTokenConfig.isIgnoreToken(uri);
return ignoreToken;
}
/**
* 网关抛异常
* @param errMsg
* @param errCode
* @param httpStatusCode
*/
protected void errorResponse(String errMsg, int errCode, int httpStatusCode) {
R tokenError = R.fail(errCode, errMsg);
RequestContext ctx = RequestContext.getCurrentContext();
// 返回错误码
ctx.setResponseStatusCode(httpStatusCode);
ctx.addZuulResponseHeader(
"Content-Type", "application/json;charset=UTF-8");
if (ctx.getResponseBody() == null) {
// 返回错误内容
ctx.setResponseBody(tokenError.toString());
// 过滤该请求,不对其进行路由
ctx.setSendZuulResponse(false);
}
}
}
19.5.2 TokenContextFilter
TokenContextFilter过滤器主要作用就是解析请求头中的jwt token并将解析出的用户信息放入zuul的header中供后面的程序使用。
package com.itheima.pinda.zuul.filter;
import javax.servlet.http.HttpServletRequest;
import com.itheima.pinda.auth.client.properties.AuthClientProperties;
import com.itheima.pinda.auth.client.utils.JwtTokenClientUtils;
import com.itheima.pinda.auth.utils.JwtUserInfo;
import com.itheima.pinda.base.R;
import com.itheima.pinda.context.BaseContextConstants;
import com.itheima.pinda.exception.BizException;
import com.itheima.pinda.utils.StrHelper;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
/**
* 解析token中的用户信息 过滤器
*/
@Component
public class TokenContextFilter extends BaseFilter {
@Autowired
private AuthClientProperties authClientProperties;
@Autowired
private JwtTokenClientUtils jwtTokenClientUtils;
@Override
public String filterType() {
// 前置过滤器
return PRE_TYPE;
}
/**
* filterOrder:通过int值来定义过滤器的执行顺序,数字越大,优先级越低
*/
@Override
public int filterOrder() {
/*
一定要在
org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter
过滤器之后执行,因为这个过滤器做了路由,而我们需要这个路由信息来鉴权
这个过滤器会将我们鉴权需要的信息放置在请求上下文中
*/
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
}
/**
* 返回一个boolean类型来判断该过滤器是否要执行
*/
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
// 不进行拦截的地址
if (isIgnoreToken()) {
return null;
}
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
//获取token, 解析,然后将信息放入 header
//1, 获取token
String userToken =
request.getHeader(authClientProperties.getUser().getHeaderName());
//2, 解析token
JwtUserInfo userInfo = null;
try {
userInfo = jwtTokenClientUtils.getUserInfo(userToken);
} catch (BizException e) {
errorResponse(e.getMessage(), e.getCode(), 200);
return null;
} catch (Exception e) {
errorResponse("解析token出错", R.FAIL_CODE, 200);
return null;
}
//3, 将信息放入header
if (userInfo != null) {
addHeader(ctx, BaseContextConstants.JWT_KEY_ACCOUNT,
userInfo.getAccount());
addHeader(ctx, BaseContextConstants.JWT_KEY_USER_ID,
userInfo.getUserId());
addHeader(ctx, BaseContextConstants.JWT_KEY_NAME,
userInfo.getName());
addHeader(ctx, BaseContextConstants.JWT_KEY_ORG_ID,
userInfo.getOrgId());
addHeader(ctx, BaseContextConstants.JWT_KEY_STATION_ID,
userInfo.getStationId());
}
return null;
}
private void addHeader(RequestContext ctx, String name, Object value) {
if (StringUtils.isEmpty(value)) {
return;
}
ctx.addZuulRequestHeader(name, StrHelper.encode(value.toString()));
}
}
19.5.3 AccessFilter
AccessFilter过滤器主要进行的是鉴权相关处理。具体的处理逻辑如下:
第1步:判断当前请求uri是否需要忽略
第2步:获取当前请求的请求方式和uri,拼接成GET/user/page这种形式,称为权限标识符
第3步:从缓存中获取所有需要进行鉴权的资源(同样是由资源表的method字段值+url字段值拼接成),如果没有获取到则通过Feign调用权限服务获取并放入缓存中
第4步:判断这些资源是否包含当前请求的权限标识符,如果不包含当前请求的权限标识符,则返回未经授权错误提示
第5步:如果包含当前的权限标识符,则从zuul header中取出用户id,根据用户id取出缓存中的用户拥有的权限,如果没有取到则通过Feign调用权限服务获取并放入缓存,判断用户拥有的权限是否包含当前请求的权限标识符
第6步:如果用户拥有的权限包含当前请求的权限标识符则说明当前用户拥有权限,直接放行
第7步:如果用户拥有的权限不包含当前请求的权限标识符则说明当前用户没有权限,返回未经授权错误提示
package com.itheima.pinda.zuul.filter;
import cn.hutool.core.util.StrUtil;
import com.itheima.pinda.authority.dto.auth.ResourceQueryDTO;
import com.itheima.pinda.authority.entity.auth.Resource;
import com.itheima.pinda.base.R;
import com.itheima.pinda.common.constant.CacheKey;
import com.itheima.pinda.context.BaseContextConstants;
import com.itheima.pinda.exception.code.ExceptionCode;
import com.itheima.pinda.zuul.api.ResourceApi;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
/**
* 权限验证过滤器
*/
@Component
@Slf4j
public class AccessFilter extends BaseFilter {
@Autowired
private CacheChannel cacheChannel;
@Autowired
private ResourceApi resourceApi;
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 10;
}
@Override
public boolean shouldFilter() {
return true;
}
/**
* 验证当前用户是否拥有某个URI的访问权限
*/
@Override
public Object run() {
// 不进行拦截的地址
if (isIgnoreToken()) {
return null;
}
RequestContext requestContext = RequestContext.getCurrentContext();
String requestURI = requestContext.getRequest().getRequestURI();
requestURI = StrUtil.subSuf(requestURI, zuulPrefix.length());
requestURI = StrUtil.subSuf(requestURI, requestURI.indexOf("/", 1));
String method = requestContext.getRequest().getMethod();
String permission = method + requestURI;
//从缓存中获取所有需要进行鉴权的资源
CacheObject resourceNeed2AuthObject =
cacheChannel.get(CacheKey.RESOURCE,
CacheKey.RESOURCE_NEED_TO_CHECK);
List<String> resourceNeed2Auth =
(List<String>) resourceNeed2AuthObject.getValue();
if(resourceNeed2Auth == null){
resourceNeed2Auth = resourceApi.list().getData();
if(resourceNeed2Auth != null){
cacheChannel.set(CacheKey.RESOURCE,
CacheKey.RESOURCE_NEED_TO_CHECK,
resourceNeed2Auth);
}
}
if(resourceNeed2Auth != null){
long count = resourceNeed2Auth.stream().filter((String r) -> {
return permission.startsWith(r);
}).count();
if(count == 0){
//未知请求
errorResponse(ExceptionCode.UNAUTHORIZED.getMsg(),
ExceptionCode.UNAUTHORIZED.getCode(), 200);
return null;
}
}
String userId = requestContext.getZuulRequestHeaders().
get(BaseContextConstants.JWT_KEY_USER_ID);
CacheObject cacheObject = cacheChannel.get(CacheKey.USER_RESOURCE, userId);
List<String> userResource = (List<String>) cacheObject.getValue();
// 如果从缓存获取不到当前用户的资源权限,需要查询数据库获取,然后再放入缓存
if(userResource == null){
ResourceQueryDTO resourceQueryDTO = new ResourceQueryDTO();
resourceQueryDTO.setUserId(new Long(userId));
//通过Feign调用服务,查询当前用户拥有的权限
R<List<Resource>> result = resourceApi.visible(resourceQueryDTO);
if(result.getData() != null){
List<Resource> userResourceList = result.getData();
userResource = userResourceList.stream().map((Resource r) -> {
return r.getMethod() + r.getUrl();
}).collect(Collectors.toList());
cacheChannel.set(CacheKey.USER_RESOURCE,userId,userResource);
}
}
long count = userResource.stream().filter((String r) -> {
return permission.startsWith(r);
}).count();
if(count > 0){
//有访问权限
return null;
}else{
log.warn("用户{}没有访问{}资源的权限",userId,method + requestURI);
errorResponse(ExceptionCode.UNAUTHORIZED.getMsg(),
ExceptionCode.UNAUTHORIZED.getCode(), 200);
}
return null;
}
}
20. 通用权限系统企业应用指南
20.1 新项目开发
如果是新项目开发,可以在品达通用权限系统的基础上进行相关的业务开发,其实就是将通用权限系统当做开发脚手架在此基础之上快速开始业务开发。
本小节通过一个商品服务的案例来讲解如何基于品达通用权限系统进行新业务的开发。
20.1.1 数据库环境搭建
创建数据库pd_goods并创建表pd_goods_info,可以使用资料中提供的建表脚本pd_goods_info.sql进行创建。
20.1.2 后端业务功能开发
20.1.2.1 创建工程
在品达通用权限系统基础上创建商品服务相关模块,如下图:
pd-goods #商品服务父工程
├── pd-goods-entity #实体
├── pd-goods-server #服务
20.1.2.2 pd-goods-entity开发
第一步:配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>pd-goods</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pd-goods-entity</artifactId>
<description>接口服务实体模块</description>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-common</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
</dependency>
</dependencies>
</project>
第二步:创建商品实体类
package com.itheima.pinda.goods.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.itheima.pinda.base.entity.Entity;
import lombok.*;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("pd_goods_info")
public class GoodsInfo extends Entity<Long> {
private static final long serialVersionUID = 1L;
/**
* 商品编码
*/
private String code;
/**
* 商品名称
*/
private String name;
/**
* 国条码
*/
private String barCode;
/**
* 品牌表id
*/
private Integer brandId;
/**
* 一级分类id
*/
private Integer oneCategoryId;
/**
* 二级分类id
*/
private Integer twoCategoryId;
/**
* 三级分类id
*/
private Integer threeCategoryId;
/**
* 商品的供应商id
*/
private Integer supplierId;
/**
* 商品售价价格
*/
private BigDecimal price;
/**
* 商品加权平均成本
*/
private BigDecimal averageCost;
/**
* 上下架状态:0下架,1上架
*/
private boolean publishStatus;
/**
* 审核状态: 0未审核,1已审核
*/
private boolean auditStatus;
/**
* 商品重量
*/
private Float weight;
/**
* 商品长度
*/
private Float length;
/**
* 商品重量
*/
private Float height;
/**
* 商品宽度
*/
private Float width;
/**
* 颜色
*/
private String color;
/**
* 生产日期
*/
private LocalDateTime productionDate;
/**
* 商品有效期
*/
private Integer shelfLife;
/**
* 商品描述
*/
private String descript;
}
第三步:创建商品操作对应的多个DTO类
package com.itheima.pinda.goods.dto;
import com.itheima.pinda.goods.entity.GoodsInfo;
import lombok.*;
import lombok.experimental.Accessors;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class GoodsInfoPageDTO extends GoodsInfo {
private LocalDateTime startCreateTime;
private LocalDateTime endCreateTime;
}
package com.itheima.pinda.goods.dto;
import com.itheima.pinda.goods.entity.GoodsInfo;
public class GoodsInfoSaveDTO extends GoodsInfo {
}
package com.itheima.pinda.goods.dto;
import com.itheima.pinda.goods.entity.GoodsInfo;
public class GoodsInfoUpdateDTO extends GoodsInfo {
}
20.1.2.3 pd-goods-server开发
第一步:配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>pd-goods</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pd-goods-server</artifactId>
<description>接口服务启动模块</description>
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-log</artifactId>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-swagger2</artifactId>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-validator</artifactId>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-xss</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-databases</artifactId>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-tools-dozer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>pd-goods-entity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
第二步:导入资料中提供的配置文件
第三步:在配置中心Nacos中创建pd-goods-server.yml
配置文件内容如下:
# 在这里配置 权限服务 所有环境都能使用的配置
pinda:
mysql:
database: pd_goods
swagger:
enabled: true
docket:
core:
title: 核心模块
base-package: com.itheima.pinda.goods.controller
server:
port: 8767
第四步:编写启动类
package com.itheima.pinda;
import com.itheima.pinda.validator.config.EnableFormValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.net.InetAddress;
import java.net.UnknownHostException;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients(value = {
"com.itheima.pinda",
})
@EnableTransactionManagement
@Slf4j
@EnableFormValidator
public class GoodsServerApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(GoodsServerApplication.class, args);
Environment env = application.getEnvironment();
log.info("\n----------------------------------------------------------\n\t" +
"应用 '{}' 运行成功! 访问连接:\n\t" +
"Swagger文档: \t\thttp://{}:{}/doc.html\n\t" +
"----------------------------------------------------------",
env.getProperty("spring.application.name"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port"));
}
}
第五步:导入资料中提供的配置类
第六步:创建Mapper接口
package com.itheima.pinda.goods.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pinda.goods.entity.GoodsInfo;
import org.springframework.stereotype.Repository;
/**
* Mapper 接口
*/
@Repository
public interface GoodsInfoMapper extends BaseMapper<GoodsInfo> {
}
第七步:创建Service接口和实现类
package com.itheima.pinda.goods.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.pinda.goods.entity.GoodsInfo;
public interface GoodsInfoService extends IService<GoodsInfo> {
}
package com.itheima.pinda.goods.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.goods.dao.GoodsInfoMapper;
import com.itheima.pinda.goods.entity.GoodsInfo;
import com.itheima.pinda.goods.service.GoodsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {
}
第八步:创建Controller
package com.itheima.pinda.goods.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.pinda.database.mybatis.conditions.Wraps;
import com.itheima.pinda.database.mybatis.conditions.query.LbqWrapper;
import com.itheima.pinda.dozer.DozerUtils;
import com.itheima.pinda.goods.dto.GoodsInfoPageDTO;
import com.itheima.pinda.goods.dto.GoodsInfoSaveDTO;
import com.itheima.pinda.goods.dto.GoodsInfoUpdateDTO;
import com.itheima.pinda.goods.entity.GoodsInfo;
import com.itheima.pinda.goods.service.GoodsInfoService;
import com.itheima.pinda.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@Validated
@RestController
@RequestMapping("/goodsInfo")
@Api(value = "GoodsInfo", tags = "商品信息")
public class GoodsInfoController extends BaseController {
@Autowired
private DozerUtils dozer;
@Autowired
private GoodsInfoService goodsInfoService;
/**
* 分页查询商品信息
*
* @param data 分页查询对象
* @return 查询结果
*/
@ApiOperation(value = "分页查询商品信息", notes = "分页查询商品信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
@ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
})
@GetMapping("/page")
@SysLog("分页查询商品信息")
public R<IPage<GoodsInfo>> page(GoodsInfoPageDTO data) {
Page<GoodsInfo> page = getPage();
LbqWrapper<GoodsInfo> wrapper = Wraps.lbQ();
wrapper.like(GoodsInfo::getName, data.getName())
.like(GoodsInfo::getCode, data.getCode())
.eq(GoodsInfo::getBarCode, data.getBarCode())
.geHeader(GoodsInfo::getCreateTime, data.getStartCreateTime())
.leFooter(GoodsInfo::getCreateTime, data.getEndCreateTime())
.orderByDesc(GoodsInfo::getCreateTime);
goodsInfoService.page(page, wrapper);
return success(page);
}
@ApiOperation(value = "查询商品信息", notes = "查询商品信息")
@GetMapping("/list")
@SysLog("查询商品信息")
public R<List<GoodsInfo>> list(GoodsInfoPageDTO data) {
LbqWrapper<GoodsInfo> wrapper = Wraps.lbQ();
wrapper.like(GoodsInfo::getName, data.getName())
.like(GoodsInfo::getCode, data.getCode())
.eq(GoodsInfo::getBarCode, data.getBarCode())
.geHeader(GoodsInfo::getCreateTime, data.getStartCreateTime())
.leFooter(GoodsInfo::getCreateTime, data.getEndCreateTime())
.orderByDesc(GoodsInfo::getCreateTime);
return success(goodsInfoService.list(wrapper));
}
/**
* 查询商品信息
*
* @param id 主键id
* @return 查询结果
*/
@ApiOperation(value = "查询商品信息", notes = "查询商品信息")
@GetMapping("/{id}")
@SysLog("查询商品信息")
public R<GoodsInfo> get(@PathVariable Long id) {
return success(goodsInfoService.getById(id));
}
/**
* 新增商品信息
*
* @param data 新增对象
* @return 新增结果
*/
@ApiOperation(value = "新增商品信息", notes = "新增商品信息不为空的字段")
@PostMapping
@SysLog("新增商品信息")
public R<GoodsInfo> save(@RequestBody @Validated GoodsInfoSaveDTO data) {
GoodsInfo GoodsInfo = dozer.map(data, GoodsInfo.class);
goodsInfoService.save(GoodsInfo);
return success(GoodsInfo);
}
/**
* 修改商品信息
*
* @param data 修改对象
* @return 修改结果
*/
@ApiOperation(value = "修改商品信息", notes = "修改商品信息不为空的字段")
@PutMapping
@SysLog("修改商品信息")
public R<GoodsInfo> update(@RequestBody @Validated(SuperEntity.Update.class) GoodsInfoUpdateDTO data) {
GoodsInfo GoodsInfo = dozer.map(data, GoodsInfo.class);
goodsInfoService.updateById(GoodsInfo);
return success(GoodsInfo);
}
/**
* 删除商品信息
*
* @param ids 主键id
* @return 删除结果
*/
@ApiOperation(value = "删除商品信息", notes = "根据id物理删除商品信息")
@SysLog("删除商品信息")
@DeleteMapping
public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
goodsInfoService.removeByIds(ids);
return success();
}
}
20.1.3 配置网关路由规则
在Nacos中的pd-gateway.yml中新增商品服务相关的路由配置,内容如下:
zuul:
# debug:
# request: true
# include-debug-header: true
retryable: false
servlet-path: / # 默认是/zuul , 上传文件需要加/zuul前缀才不会出现乱码,这个改成/ 即可不加前缀
ignored-services: "*" # 忽略eureka上的所有服务
sensitive-headers: # 一些比较敏感的请求头,不想通过zuul传递过去, 可以通过该属性进行设置
# prefix: /api #为zuul设置一个公共的前缀
# strip-prefix: false #对于代理前缀默认会被移除 故加入false 表示不要移除
routes: # 路由配置方式
authority: # authority是路由名称,可以随便定义,但是path和service-id需要一一对应
path: /authority/**
serviceId: pd-auth-server
goods:
path: /goods/**
serviceId: pd-goods-server
20.1.4 前端开发
可以将pinda-authority-ui作为前端开发脚手架,基于此工程开发商品服务相关页面。资料中已经提供了开发完成的前端工程,直接运行即可。
下载安装node js: https://blog.csdn.net/qq_51929833/article/details/123694846
解压前端工程并删除文件夹node-sass
重新安装并运行
npm install node-sass
npm run dev
出现以下页面就是运行成功了
输入管理员账号和密码,验证码登录查看
user:pinda
password:pinda123
20.1.5 配置菜单和资源权限
启动网关服务、权限服务、商品服务、前端工程,使用管理员账号登录,配置商品服务相关的菜单和对应的资源权限。
20.1.6 配置角色
启动网关服务和权限服务,使用管理员账号登录。创建新角色并进行配置(菜单权限和资源权限)和授权(为用户分配角色)。
20.2 已有项目集成
本小节通过一个已经完成开发的TMS(品达物流)项目来展示如何进行已有项目集成的过程。
20.2.1 TMS调整
20.2.1.1 页面菜单
对于已经完成相关业务开发的项目,可以将其前端系统的页面通过iframe的形式内嵌到通用权限系统的前端页面中,这就需要对其前端系统的页面进行相应的修改。因为原来的TMS系统前端页面的左侧菜单和导航菜单都在自己页面中展示,现在需要将这些菜单配置到通用权限系统中,通过权限系统的前端系统来展示。
20.2.1.2 请求地址
为了能够进行鉴权相关处理,需要将TMS前端发送的请求首先经过通用权限系统的网关进行处理:
20.2.2 网关路由配置
配置通用权限系统的网关路由规则,将针对TMS的请求转发到TMS相关服务:
zuul:
retryable: false
servlet-path: /
ignored-services: "*" # 忽略eureka上的所有服务
sensitive-headers: # 一些比较敏感的请求头,不想通过zuul传递过去, 可以通过该属性进行设置
routes: # 路由配置方式
authority:
path: /authority/**
serviceId: pd-auth-server
pay:
path: /pay/**
serviceId: pd-ofpay-server
web-manager:
path: /web-manager/**
serviceId: pd-web-manager
web-xczx:
path: /xczx/api/**
url: http://xc-main-java.itheima.net:7291/api/
20.2.3 通用权限系统配置
20.2.3.1 菜单配置
登录通用权限系统,配置TMS项目相应的菜单:
20.2.3.2 资源权限配置
资源权限都是关联到某个菜单下的,所以要配置资源权限需要先选中某个菜单,然后就可以配置相关资源权限了:
20.2.3.3 角色配置
登录通用权限系统,在角色管理菜单中配置TMS项目中使用到的角色:
角色创建完成后可以为角色配置菜单权限和资源权限:
完成角色的菜单权限和资源权限配置后可以将角色授权给用户: