基本架构
nc父工程
pom文件
<parent>
<artifactId>nc01</artifactId>
<groupId>com.nc.parent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nc.user</groupId>
<artifactId>nc-user</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>nc-user-interface</module>
<module>nc-user-service</module>
</modules>
<packaging>pom</packaging>
注册中心
- 启动项
- 配置文件
server:
port: 10086
spring:
application:
name: nc-registry #当前应用的名称 如果是eureka集群 那么spring应用的名称是一样的
eureka:
server:
eviction-interval-timer-in-ms: 5000 #每个10秒进行服务列表剔除操作
enable-self-preservation: false #关闭自我保护模式(缺省为打开)
client:
service-url: #EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其他Server的地址
defaultZone: http://127.0.0.1:${server.port}/eureka/
register-with-eureka: false #不向注册中心注册自己
fetch-registry: false #不向注册中心拉取服务列表
pom文件的相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
网关gateway
配置文件
server:
port: 10010
spring:
application:
name: nc-gateway
zuul:
ignored-services:
- upload-service # 忽略upload-service服务
prefix: /api # 添加路由前缀
routes:
item-service: /item/** #商品微服务的映射路径
search-service: /search/** #搜索微服务的映射路径
user-service: /user/** #个人中心微服务的映射路径
auth-service: /auth/** # 授权中心微服务
cart-service: /cart/** # 授权中心微服务
order-service: /order/**
add-host-header: true #添加原始头信息
sensitive-headers: #过滤敏感头信息为空
eureka:
client:
registry-fetch-interval-seconds: 5
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
nc:
jwt:
pubKeyPath: C:\\temp\\rsa\\rsa.pub # 公钥地址
cookieName: NC_TOKEN # cookie的名称
filter: #白名单
allowPaths:
- /api/auth
- /api/search
- /api/user/register
- /api/user/check
- /api/user/code
- /api/item
白名单配置文件
package com.nc.gateway.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@ConfigurationProperties(prefix = "nc.filter")
public class FilterProperties {
private List<String> allowPaths;
public List<String> getAllowPaths() {
return allowPaths;
}
public void setAllowPaths(List<String> allowPaths) {
this.allowPaths = allowPaths;
}
}
jwt配置文件
package com.nc.gateway.config;
import com.nc.auth.utils.RsaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.annotation.PostConstruct;
import java.security.PublicKey;
@ConfigurationProperties(prefix = "nc.jwt")
public class JwtProperties {
private String pubKeyPath;// 公钥
private PublicKey publicKey; // 公钥
private String cookieName;
private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
@PostConstruct
public void init(){
try {
// 获取公钥和私钥
this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
} catch (Exception e) {
logger.error("初始化公钥失败!", e);
throw new RuntimeException();
}
}
public String getPubKeyPath() {
return pubKeyPath;
}
public void setPubKeyPath(String pubKeyPath) {
this.pubKeyPath = pubKeyPath;
}
public PublicKey getPublicKey() {
return publicKey;
}
public void setPublicKey(PublicKey publicKey) {
this.publicKey = publicKey;
}
public String getCookieName() {
return cookieName;
}
public void setCookieName(String cookieName) {
this.cookieName = cookieName;
}
}
跨域配置文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;//web.cors下的包
import org.springframework.web.filter.CorsFilter;
@Configuration
public class NcCorsConfig {
@Bean
public CorsFilter corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允许的域,不要写*,否则cookie就无法使用了
config.addAllowedOrigin("http://manage.nc.com");
config.addAllowedOrigin("http://www.nc.com");
//2) 是否发送Cookie信息
config.setAllowCredentials(true);
//3) 允许的请求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
// 4)允许的头信息
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
//3.返回新的CorsFilter.
return new CorsFilter(configSource);
}
}
公共类 common
分页工具类
import java.util.List;
public class PageResult<T> {
private Long total;// 总条数
private Integer totalPage;// 总页数
private List<T> items;// 当前页数据
public PageResult() {
}
public PageResult(Long total, List<T> items) {
this.total = total;
this.items = items;
}
public PageResult(Long total, Integer totalPage, List<T> items) {
this.total = total;
this.totalPage = totalPage;
this.items = items;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
}