Introduction to Soul Chapter 17 ContextPath Plug-in

introduce

The soul gateway uses the context_PATH plug-in to override the contextPath of the request path when calling the target service

use

Set the soul-admin – > Plug-in Management – > context_path to on.

Add context_PATH support to the gateway’s POM.xml file.

  <! -- soul context_path plugin start-->
  <dependency>
      <groupId>org.dromara</groupId>
      <artifactId>soul-spring-boot-starter-plugin-context-path</artifactId>
     <version>${last.version}</version>
  </dependency>
  <! -- soul context_path plugin end-->
Copy the code

Add a selector and a rule to the context_path of the soul-admin plug-in list to set the contextPath value.

scenario

Context_path is a plug-in that redefines the contextPath of the URI. After matching the request, it sets a custom contextPath, and then intercepts the custom contextPath according to the Url of the request to obtain the real Url. For example, if the request path is /soul/ HTTP /order and the contextPath is’ /soul/ HTTP ‘, the actual requested URL is’ /order ‘.

Code implementation
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
    final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
    assertsoulContext ! =null;
    final String handle = rule.getHandle();
    final ContextMappingHandle contextMappingHandle = GsonUtils.getInstance().fromJson(handle, ContextMappingHandle.class);
    if (Objects.isNull(contextMappingHandle) || StringUtils.isBlank(contextMappingHandle.getContextPath())) {
        log.error("context path mapping rule configuration is null :{}", rule);
        return chain.execute(exchange);
    }
    //check the context path illegal
    if(! soulContext.getPath().startsWith(contextMappingHandle.getContextPath())) { Object error = SoulResultWrap.error(SoulResultEnum.CONTEXT_PATH_ERROR.getCode(), SoulResultEnum.CONTEXT_PATH_ERROR.getMsg(),null);
        return WebFluxResultUtils.result(exchange, error);
    }
  // Redefine contextPath key logic
    this.buildContextPath(soulContext, contextMappingHandle);
    return chain.execute(exchange);
}
Copy the code
    private void buildContextPath(final SoulContext context, final ContextMappingHandle handle) {
        context.setContextPath(handle.getContextPath());
        if(! StringUtils.isBlank(handle.getRealUrl())) { log.info("context path mappingPlugin replaced old :{} , real:{}", context.getRealUrl(), handle.getRealUrl());
          // If the actual URL has been calculated, returns without redefining the contextPath
            context.setRealUrl(handle.getRealUrl());
            return;
        }
      // Redefine contextPath to rewrite realUrl
        Optional<String> optional = Arrays.stream(context.getPath()
                .split(handle.getContextPath()))
                .reduce((first, last) -> last);
        optional.ifPresent(context::setRealUrl);
    }

Copy the code