导图社区 Spring、SpringBoot、SpringCloud知识点整理
关于Spring、SpringBoot、SpringCloud知识点整理的思维导图,介绍了依赖注入、简单的示例流程、AOP切面、spring常用的配置等
编辑于2022-05-01 22:25:24Spring
依赖注入
容器负责创建对象和维护对象之间的依赖关系 Spring IOC容器负责创建Bean
声明Bean的注解
@Service
service层使用
@Repository
dao层使用
@Controller
MVC展示层使用
@Component
组件,没有明确角色
本身作用都是等效的,不同层使用不同注解,逻辑清晰而已
注入Bean的注解
@Autowired
Spring注解
@Inject
@Resource
JDK自带
简单的示例流程
pom增加依赖
spring-context
创建配置类
@Configuration
@ComponentScan
创建service
运行
实例化AnnotationConfigApplicationContext,构造方法中传入配置类
getBean获取Bean
执行Bean方法
AOP切面
依赖
spring-aop
aspectjweaver
基本流程
使用@Aspect声明切面
使用@After、@Before、@Around定义断言,可直接将拦截规则(切点-PointCut)作为参数
配置类增加@EnableAspectJAutoProxy开启Spring对AecpectJ的支持
为了使切点复用,可使用@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用
符合条件的每一个被拦截处为连接点(JoinPoint)
切点操作
切点
以注解作为切点
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
以包下面的某些类作为切点
以包下面的某些类的某些方法作为切点
注解式拦截
@PointCut("@annotation(注解包路径.注解)")
方法规则式拦截
@Before("execution(* com.gf.service..*.*(..))")
Spring常用配置
Bean的Scope
含义:描述Spring容器是如何创建Bean实例的
注解:@Scope
Singleton
默认,一个Spring容器只有一个Bean实例
ProtoType
每次调用新建一个Bean实例
Request
在Web项目中,给每个请求创建一个Bean实例
Session
在Web项目中,给每个Session创建一个Bean实例
GlobalSession
只在portal应用中有用,给每个global http session创建Bean实例
EL与资源调用
@Value
#{...},用于执行SPEL表达式,并将结果赋给属性
${...},用于加载外部属性文件的值
二者可以混用,但必须#{}在外,${}在内
使用方式
在配置类增加@PropertySource("classpath:配置文件")
示例:@PropertySource("classpath:test.properties")
通过@Value注入值
@Value("${book.name}") private String bookeName;
Profile
为在不同环境下使用不同配置提供支持
实现方式
设定Environment的ActiveProfiles来设定当前context需要使用的配置环境
@Profile("")
通过设置jvm的spring.profiles.active参数设置配置环境
Web项目设置在Servlet的context parameter中
具体实现
创建配置类,配置类中创建相关场景的Bean
@Configuration @PropertySource("classpath:test.properties") public class ProfileConfig { @Value("${database.url.dev}") private String dataBaseUrlDev; @Value("${database.url.prod}") private String dataBaseUrlProd; @Bean @Profile("dev") public ProfileDemoBean devProfileDemoBean() { return new ProfileDemoBean("from dev profile," + dataBaseUrlDev); } @Bean @Profile("prod") public ProfileDemoBean prodProfileDemoBean() { return new ProfileDemoBean("from prod profile," + dataBaseUrlProd); } }
设定Environment的ActiveProfiles来设定当前context需要使用的配置环境
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.getEnvironment().setActiveProfiles("dev"); context.register(ProfileConfig.class); context.refresh();
事件
子主题 1
子主题 1
多线程
子主题 1
子主题 1
计划任务
配置类增加@EnableScheduling注解
Service方法增加@Scheduled注解
条件注解
拦截器
作用
实现对每个请求前后进行相关的业务处理,类似Servlet的Filter
基本实现
实现HandlerInterceptor或AnsyncHandlerInceptor
preHandle
在Controller处理之前进行调用
Interceptor拦截器是链式的,可以同时存在多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行
返回true或者false
true:请求将会继续执行后续操作
false:拦截,不进行后续操作
postHandle
在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行
afterCompletion
在整个请求完成之后,也就是DispatcherServlet渲染了视图执行
主要作用是用于清理资源的
Spring Boot实现
创建普通Bean,实现HandlerInterceptor接口
package com.gf.myspringboot.interceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 标题: * 描述: * 创建日期: 2022/3/31. * 修改人: * 修改日期: * 修改描述: * * @author GuoFu */ @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //return HandlerInterceptor.super.preHandle(request, response, handler); long startTime = System.currentTimeMillis(); request.setAttribute("startTime", startTime); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { //HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); long startTime = (long) request.getAttribute("startTime"); request.removeAttribute("startTime"); long endTime = System.currentTimeMillis(); System.out.println("请求耗时:" + (endTime - startTime) + "ms"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
创建配置类,实现WebMvcConfigurer接口
重写addInterceptors方法,增加相关拦截器
InterceptorRegistration registration = registry.addInterceptor(创建的拦截器实现类); registration.addPathPatterns("/**"); //所有路径都被拦截 registration.excludePathPatterns( //添加不拦截路径 "你的登陆路径", //登录 "/**/*.html", //html静态资源 "/**/*.js", //js静态资源 "/**/*.css", //css静态资源 "/**/*.woff", "/**/*.ttf" );
Spring Boot
特性
独立运行的Spring项目
子主题 1
内嵌Servlet容器
Tomcat
Jetty
Undertow
提供Starter简化Maven配置
spring-boot-starter-web
自动配置spring
准生产的应用监控
提供SSH、Telnet、http等方式对运行时进行监控
快速搭建
说明
JDK1.8,最低要求1.6
Spring框架4.x
官网脚手架
http://start.spring.io
IDEA
新建Spring Initializr项目
Spring Boot CLI
Maven手动创建
相关重点
核心注解@SpringBootApplication
常用操作
Banner相关
定制Banner
在src/main/resource新建banner.txt
关闭Banner
SpringApplication springApplication = new SpringApplication(MyspringbootApplication.class); springApplication.setBannerMode(Banner.Mode.OFF); springApplication.run(args);
修改启动端口
修改application.properties
server.port=端口
外部配置
可以使用properties、yml或者命令参数作为外部配置
命令行参数配置
java -jar xx.jar --server.port=9090
常规属性配置
Spring环境下需要通过@PropertySource指明properties文件的位置,然后通过@value注入,SpingBoot中直接使用@Value注入即可
分支主题 5
子主题 1
Spring Cloud
组件
服务发现
Nacos
Eureka
Consul
配置中心
Nacos
Spring Cloud Config
携程阿波罗
网关
Spring Cloud Gateway
Zuul
负载均衡
Ribbon
服务间调用
Feign
OpenFeign
集成了Ribbon,自动负载均衡
说明
Feign是Netflix开发的,现在处于维护状态,Springcloud团队基于Feign开发了Openfeign
熔断器
Hytrix
防止出现雪崩现象
全链路监控
Skywalking
zipin
Pinpoint
actuator
org.springframework.boot spring-boot-starter-actuator 通过在appliction.properties中暴露actuator的监控接口 management.endpoints.web.exposure.include=*,hystrix.stream 启动项目后,看到类似的打印,actuator/** 代表暴露的接口
Sentinel
Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性
Spring Cloud组件
Nacos与Spring Cloud整合
Nacos Config
使用 Nacos Config Starter 完成 Spring Cloud 应用的配置管理
基本流程
修改 pom.xml 文件,引入 Nacos Config Starter
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
配置文件中配置 Nacos Config 元数据
spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848
完成之后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中
使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
读取配置
注入方式
@Value("${}")
类型安全方式
创建实体类Bean
增加@ConfigurationProperties(prefix = "xxxx")注解
适时注入实体类Bean即可
关键点
启动类增加:@EnableDiscoveryClient 开启服务注册与发现
监控注册中心变化
@Component class SampleRunner implements ApplicationRunner { @Autowired private NacosConfigManager nacosConfigManager; @Override public void run(ApplicationArguments args) throws Exception { nacosConfigManager.getConfigService().addListener( "nacos-config-example.properties", "DEFAULT_GROUP", new Listener() { /** * Callback with latest config data. * @param configInfo latest config data for specific dataId in Nacos * server */ @Override public void receiveConfigInfo(String configInfo) { System.out.println("配置发生变更,自动刷新..."); Properties properties = new Properties(); try { properties.load(new StringReader(configInfo)); } catch (IOException e) { e.printStackTrace(); } //System.out.println("config changed: " + properties); } @Override public Executor getExecutor() { return null; } }); } }
提供者(服务注册)
基本流程
修改 pom.xml 文件,引入 Nacos Discovery Starter
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
配置文件中配置 Nacos Server地址等信息
server.port=18082 spring.application.name=service-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=true #spring.cloud.nacos.discovery.instance-enabled=true spring.cloud.nacos.username=nacos spring.cloud.nacos.password=nacos management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
@EnableDiscoveryClient 注解开启服务注册与发现功能
关键点
消费者(服务发现)
基本流程
修改 pom.xml 文件,引入 Nacos Discovery Starter
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
配置文件中配置 Nacos Server地址等信息
server.port=18083 spring.application.name=service-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.enabled=true #spring.cloud.nacos.discovery.instance-enabled=true
使用 @EnableDiscoveryClient 注解开启服务注册与发现功能
启动类增加@EnableFeignClients
org.springframework.cloud spring-cloud-starter-openfeign 2.2.1.RELEASE
增加提供者对应的接口
@FeignClient(name = "提供者")
注入接口,调用即可
关键点
@FeignClient(name = "提供者")
数据持久化
目前仅支持Mysql数据库,且版本要求:5.6.5+
流程
进入nacos服务端安装目录\conf目录,初始化文件:nacos-mysql.sql
创建库,导入SQL脚本
修改Nacos配置文件,conf/application.properties,配置数据库连接信息
总结
通过集中式存储来保证数据的持久化
为Nacos集群部署奠定了基础
采用单一数据源,直接解决了分布式和集群部署中的一致性问题
OpenFeign
细节点
参数传递
零散值
queryString方式
@RequestParam
必须指定Value值
路径参数方式
@PathVariable
对象
@RequestBody
数组
集合
需要将集合封装在对象中
超时设置
默认情况下,被调用者必须在一秒内响应,否则超时
修改超时
针对某个项目
feign.client.config.服务名.connectTimeout
feign.client.config.服务名.readTimeout
针对所有项目
feign.client.config.default.connectTimeout
feign.client.config.default.readTimeout
通讯日志
便于了解接口调用通讯过程中的详细请求、响应细节
只会对DEBUG级别的日志做出响应
logging.level.Feign客户端包=debug
日志级别
NONE
不记录任何日志
BASIC
仅记录请求方法、URL、响应状态码、执行时间
HEADERS
在BASIC基础上额外增加请求、响应headers信息
FULL
记录请求响应的headers、元数据、body
开启方式
针对某个项目
feign.client.config.服务名.loggerLevel
针对所有项目
feign.client.config.default.loggerLevel
HTTP客户端
HttpURLConnection
OpenFeign默认
每次请求都会建立、关闭连接,性能方面不如HttpClient、okHttp
HttpClient
替换方式
添加依赖
io.github.openfeign feign-httpclient
全局配置
feign: httpclient: # 开启httpclient enabled: true
OkHttp
Hystrix熔断器
Feign默认集成Hystrix
@EnableHystrix
开启操作
@FeignClient定义接口
name
fallback指定回退类
创建fallback指定的回退类
实现@FeignClient注解的接口
实现类增加@Component注解
修改配置文件,开启Feign的Hystrix熔断
feign.hystrix.enabled=true
可被捕获的异常情况
FAILURE
执行失败,抛出异常
TIMEOUT
执行超时
SHORT_CIRCUITED
断路器打开
THREAD_POOL_REJECTED
线程池拒绝
SEMAPHORE_REJECTED
信号量拒绝
会触发fallback
BAD_REQUEST
错误请求
不会触发fallback,抛出HystrixBadRequestException 一般是由于非法参数或者非系统异常引起的。对于这类异常可以根据响应创建对应的异常进行异常封装或者直接处理
不会触发fallback
Spring Cloud Gateway
概述
API网关
组成
Spring 5.x
Spring Boot 2.x
Project Reactor
http://doc.ruoyi.vip/ruoyi-cloud/cloud/gateway.html#基本介绍
核心概念
路由(Route)
是网关最基础部分
路由信息ID-id
目标URI-uri
一组断言-predicates
如果断言为真,说明请求的URI和配置匹配
一组过滤器-filters
组成
断言(Predicate)
Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自于Http Request中的任何信息,比如请求头和参数等
过滤器(Filter)
一个标准的Spring Web Filter。Spring Cloud Gateway中的Filter分为两种类型,分别是Gateway Filter和Global Filter。过滤器将会对请求和相应进行处理
基本流程
修改 pom.xml 文件,引入 Nacos Discovery Starter、Spring Cloud Gateway Starter
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.cloud spring-cloud-starter-gateway
配置文件中配置 Nacos Server 地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848
配置文件中配置 Spring Cloud Gateway 路由
spring.cloud.gateway.routes[0].id=nacos-route spring.cloud.gateway.routes[0].uri=lb://service-gateway-provider spring.cloud.gateway.routes[0].predicates[0].name=Path spring.cloud.gateway.routes[0].predicates[0].args[pattern]=/nacos/** spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1 spring: application: name: ruoyi-gateway cloud: gateway: routes: # 系统模块 - id: ruoyi-system uri: http://localhost:9201/ predicates: - Path=/system/** filters: - StripPrefix=1
@EnableDiscoveryClient 注解开启服务注册与发现功能
关键点
常用网关路由
Datetime
匹配日期时间之后发生的请求
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - After=2021-02-23T14:20:00.000+08:00[Asia/Shanghai]
Cookie
匹配指定名称且其值与正则表达式匹配的cookie
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Cookie=loginname, ruoyi
Header
匹配具有指定名称的请求头,\d+值匹配正则表达式
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Header=X-Request-Id, \d+
Host
匹配主机名的列表
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Host=**.somehost.org,**.anotherhost.org
Method
匹配请求methods的参数,它是一个或多个参数
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Method=GET,POST
Path
匹配请求路径
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Path=/system/**
Query
匹配查询参数
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - Query=username, abc.
RemoteAddr
匹配IP地址和子网掩码
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system uri: http://localhost:9201/ predicates: - RemoteAddr=192.168.10.1/0
Weight
匹配权重
spring: application: name: ruoyi-gateway cloud: gateway: routes: - id: ruoyi-system-a uri: http://localhost:9201/ predicates: - Weight=group1, 8 - id: ruoyi-system-b uri: http://localhost:9201/ predicates: - Weight=group1, 2
路由配置
websocket配置方式
spring: cloud: gateway: routes: - id: ruoyi-api uri: ws://localhost:9090/ predicates: - Path=/api/**
http地址配置方式
spring: cloud: gateway: routes: - id: ruoyi-api uri: http://localhost:9090/ predicates: - Path=/api/**
注册中心配置方式
spring: cloud: gateway: routes: - id: ruoyi-api uri: lb://ruoyi-api predicates: - Path=/api/**
限流配置
限流算法
计数器
漏桶(Leaky Bucket)
令牌桶(Token Bucket)
配置流程
Spring Cloud Gateway官方提供了RequestRateLimiterGatewayFilterFactory过滤器工厂,使用Redis 和Lua脚本实现了令牌桶的方式。
添加依赖
org.springframework.boot spring-boot-starter-data-redis-reactive
限流规则,根据URI限流
spring: redis: host: localhost port: 6379 password: cloud: gateway: routes: # 系统模块 - id: ruoyi-system uri: lb://ruoyi-system predicates: - Path=/system/** filters: - StripPrefix=1 - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 1 # 令牌桶每秒填充速率 redis-rate-limiter.burstCapacity: 2 # 令牌桶总容量 key-resolver: "#{@pathKeyResolver}" # 使用 SpEL 表达式按名称引用 bean StripPrefix=1配置,表示网关转发到业务模块时候会自动截取前缀。
编写URI限流规则配置类
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.core.publisher.Mono; /** * 限流规则配置类 */ @Configuration public class KeyResolverConfiguration { @Bean public KeyResolver pathKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getURI().getPath()); } }
测试服务验证限流
启动网关服务RuoYiGatewayApplication.java和系统服务RuoYiSystemApplication.java。 因为网关服务有认证鉴权,可以设置一下白名单/system/**在进行测试,多次请求会发现返回HTTP ERROR 429,同时在redis中会操作两个key,表示限流成功。 request_rate_limiter.{xxx}.timestamp request_rate_limiter.{xxx}.tokens
熔断降级
其它
spring-boot-devtools
热部署
增加POM依赖
org.springframework.boot spring-boot-devtools 2.2.1.RELEASE true org.springframework.boot spring-boot-maven-plugin true
配置文件开启配置
spring: devtools: restart: enabled: true #设置开启热部署
Swagger
单应用集成
增加POM
io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2
Swagger3.0.0可能有问题,无法访问页面
增加配置类
package com.gf.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * 标题: * 描述: * 创建日期: 2022/4/2. * 修改人: * 修改日期: * 修改描述: * * @author GuoFu */ @Configuration public class SwaggerConfig { @Bean public Docket userApi() { Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.gf.controller"))//过滤的接口 .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger接口文档") .version("1.0") .description("练习、测试接口") .termsOfServiceUrl("http://www.baidu.com") .contact(new Contact("GuoFu", "", "guofu_gh@163.com")) .license("Licence Version 1.0") .licenseUrl("#") .build(); } }
@EnableSwagger2
访问:http://ip:端口/swagger-ui.html
网关集成
SpringBoot+redis实现数据缓存
关键注解
@Cacheable
查询方法
@CacheEvict
删除方法
@CachePut
新增、修改方法
@CacheConfig
参考链接
https://dandelioncloud.cn/article/details/1503391003691282433
https://blog.csdn.net/pan_junbiao/article/details/108813376
常用注解
@Configuration
作用于类上。声明当前类是一个配置类,相当于一个Spring配置的xml文件
@Bean
作用于方法上。声明当前方法返回值是一个Bean
@RequestParam
接收http://host:port/path?参数名=参数值数据
参数
name 绑定本次参数的名称,要跟URL上面的一样
defaultValue 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值
required 这个参数是否必须的
value 跟name一样的作用,是name属性的一个别名
@PathVariable
接收http://host:port/path/{参数值}数据
参数
name 绑定本次参数的名称,要跟URL上面的一样
required 这个参数是否必须的
value 跟name一样的作用,是name属性的一个别名
接收请求参数
主题
主题
主题
主题
主题
主题
主题
主题
主题
主题
技术点整理
oAuth2.0
子主题 1
JWT
概述
Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场景。JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源,也可以增加一些额外的其它业务逻辑所必须的声明信息,该token也可直接被用于认证,也可被加密。
JWT:JSON Web Token
基于JSON
适用于分布式单点登录SSO场景
在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源
鉴权机制
验证流程图
流程
用户使用用户名密码来请求服务器
HTTPS/POST
服务器进行验证用户的信息
服务器通过验证发送给用户一个token
客户端存储token,并在每次请求时附送上这个token值
服务端验证token值,并返回数据
验证签名是否正确
是否过期
token的接收方是否是自己
...
token必须要在每次请求时传递给服务端,它应该保存在请求头里
服务端配置Access-Control-Allow-Origin: *以支持支持CORS(跨来源资源共享)策略
组成
JWTString=Base64(Header).Base64(Payload).HMACSHA256(base64UrlEncode(header)+"."+base64UrlEncode(payload),secret)
头部Header
{ "typ": "JWT", "alg": "HS256" }
声明类型(比如:jwt)
声明加密的算法
HMAC SHA256
base64加密
载荷Payload
组成
标准中注册的声明
公共的声明
可以添加任何的信息,一般添加用户的相关信息或其他业务需要的必要信息.但不建议添加敏感信息,因为该部分在客户端可解密.
标准中注册的声明
建议,但不强制使用
iss: jwt签发者
sub: jwt所面向的用户
aud: 接收jwt的一方
exp: jwt的过期时间,这个过期时间必须要大于签发时间
nbf: 定义在什么时间之前,该jwt都是不可用的.
iat: jwt的签发时间
jti: jwt的唯一身份标识,主要用来作为一次性token,从而回避重放攻击。
私有的声明
提供者和消费者所共同定义的声明,一般不建议存放敏感信息,因为base64是对称解密的,意味着该部分信息可以归类为明文信息
base64加密
签证signature
组成
header (base64后的)
payload (base64后的)
secret
通过Header中声明的算法进行加盐secret加密
注意点
secret是保存在服务器端的
jwt的签发生成也是在服务器端
用来进行jwt的签发和jwt的验证
应用
请求头里加入Authorization,并加上Bearer标注
fetch('api/user/1', { headers: { 'Authorization': 'Bearer ' + token } })
Java
java-jwt
SpringBoot
Interceptor拦截器应用
OSS
阿里云对象存储OSS(Object Storage Service)
相关概念
存储类型(Storage Class)
标准
低频
归档
冷归
存储空间(Bucket)
存储对象(Object)的容器
说明
同一个存储空间的内部是扁平的,没有文件系统的目录等概念,所有的对象都直接隶属于其对应的存储空间
每个用户可以拥有多个存储空间
存储空间的名称在OSS范围内必须是全局唯一的,一旦创建之后无法修改名称
存储空间内部的对象数目没有限制
命名规范
只能包括小写字母、数字和短划线(-)
必须以小写字母或者数字开头和结尾
长度必须在3~63字符之间
对象(Object)
基本单元,也称为OSS的文件
对象没有文件目录层级结构的关系
组成
元信息(Object Meta)
表示了对象的一些属性,比如最后修改时间、大小等信息,同时用户也可以在元信息中存储一些自定义的信息
键值对
用户数据(Data)
文件名(Key)
对象由存储空间内部唯一的Key来标识
除通过追加方式上传的Object可以通过继续追加上传写入数据外,其他方式上传的Object内容无法编辑,您可以通过重复上传同名的对象来覆盖之前的对象
命名规范
使用UTF-8编码
区分大小写
长度必须在1~1023字符之间
不能以正斜线(/)或者反斜线(\)开头
地域(Region)
访问域名(Endpoint)
访问密钥(AccessKey)
常见操作
创建Bucket
上传文件
下载文件
列举文件
删除文件
使用方式
通过控制台管理OSS
通过API或SDK管理OSS
通过工具管理OSS
ossbrowser
ossutil
ossftp
通过云存储网关管理OSS
ElasticSearch(ES)
子主题 1
K8s
子主题 1
浮动主题
若依微服务版
项目结构
ruoyi-gateway
网关
Ruoyigatewayapplication.Java
ruoyi-auth
认证授权中心
Ruoyiauthapplication.Java
ruoyi-api
ruoyi-api-system
ruoyi-common
ruoyi-common-core
ruoyi-common-datascope
ruoyi-common-datasource
ruoyi-common-log
ruoyi-common-redis
ruoyi-common-security
ruoyi-common-swagger
支持库
ruoyi-modules
ruoyi-file
文件服务模块
Ruoyifileapplication.Java
ruoyi-gen
代码生成模块
Ruoyigenapplication.Java
ruoyi-job
定时任务模块
Ruoyijobapplication.Java
ruoyi-system
系统模块
Ruoyisystemapplication.Java
ruoyi-ui
ruoyi-visual
ruoyi-monitor
监控中心
Ruoyimonitorapplication