若依开源管理系统采用Nacos作为配置中心,GateWay网关作为一项基本服务,也是注册在Nacos中。
yml配置文件
网关:ruoyi-gateway-pro.yml
spring:
  redis:
    host: localhost
    port: 6379
    password: 
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true  #true:只能识别小写 false:只能识别大写
          enabled: true  #开启服务注册发现
      routes:
        # 认证中心
        - id: ruoyi-auth
          uri: lb://ruoyi-auth
          predicates:
            - Path=/auth/**
          filters:
            # 验证码处理
            - CacheRequestFilter
            - ValidateCodeFilter
            - StripPrefix=1
# 安全配置
security:
  # 验证码
  captcha:
    enabled: false
    type: math
  # 防止XSS攻击
  xss:
    enabled: true
    excludeUrls:
      - /system/notice
  # 不校验白名单
  ignore:
    whites:
      - /auth/logout
      - /auth/login
      - /auth/register
      - /*/v2/api-docs
      - /csrf
 
可以看到,在权限认证之前,经过了多道filter进行过滤:
 CacheRequestFilter,ValidateCodeFilter
Property类

 @RefreshScope注解:该配置自动刷新(更改配置文件,即时生效)
@ConfigurationProperties注解:可以读取配置文件中的信息,并把它映射为实体类。
上图中的信息是把
 
 映射为CaptchaProperties实体类。
config类
CaptchaConfig(验证码Bean)

 @Bean(name = "captchaProducer")生成了一个验证码bean,名字是captchaProducer
如果只用@bean注解,未指定name属性,那么生成的bean名称是getKaptchaBean
- 补充(生成bean规则)

 
GatewayConfig(网关限流配置)

 @Order是org.springframework.core.annotation核心包中的注解,其作用定义了Spring容器加载bean的顺序。
 Ordered.HIGHEST_PRECEDENCE:初始化最高优先级,即spring容器启动时,优先初始化添加该注解的bean
RouterFunctionConfiguration(路由配置)

- 补充(路由规则) 
参考文档:https://blog.51cto.com/u_15185289/2783813 

 
 ValidateCodeHandler如何处理/code请求?
代码如下:
@Component
public class ValidateCodeHandler implements HandlerFunction<ServerResponse>{
    @Autowired
    private ValidateCodeService validateCodeService;
    @Override
    public Mono<ServerResponse> handle(ServerRequest serverRequest){
        AjaxResult ajax;
        try{
            ajax = validateCodeService.createCapcha();
        }catch (CaptchaException | IOException e){
            return Mono.error(e);
        }
        return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(ajax));
    }
}










