Skip to main content
Version: 当前版本

自定义JWT插件转化算法

说明#

  • 用户可以自定义JWT插件中转化算法

扩展#

转化算法的默认实现为 org.apache.shenyu.plugin.jwt.strategy.DefaultJwtConvertStrategy,采用的是SPI机制进行扩展,步骤如下:

  1. 实现接口org.apache.shenyu.plugin.jwt.strategy.JwtConvertStrategy

    /** * Represents a conversion strategy that convert jwt to some attributes of * serverWebExchange, especially attributes of the request header. */@SPIpublic interface JwtConvertStrategy {
        /**     * HandleJson needs to be parsed into jwtRuleHandle in order to     * specify how to convert jwt.     *     * @param handleJson handleJson from rule     * @return jwtRuleHandle     */    JwtRuleHandle parseHandleJson(String handleJson);
        /**     * Converts jwt to some attributes of serverWebExchange based on jwtRuleHandle.     *     * @param jwtRuleHandle jwtRuleHandle     * @param exchange      exchange     * @param jwtBody       jwtBody     * @return serverWebExchange     */    ServerWebExchange convert(JwtRuleHandle jwtRuleHandle, ServerWebExchange exchange, Map<String, Object> jwtBody);
    }
    @Joinpublic class CustomJwtConvertStrategy implements JwtConvertStrategy {
        @Override    public CustomJwtRuleHandle parseHandleJson(final String handleJson) {
            return GsonUtils.getInstance().fromJson(handleJson, CustomJwtRuleHandle.class);    }
        @Override    public ServerWebExchange convert(final JwtRuleHandle jwtRuleHandle, final ServerWebExchange exchange, final Map<String, Object> jwtBody) {        final CustomJwtRuleHandle customJwtRuleHandle = (CustomJwtRuleHandle) jwtRuleHandle;        String customConvert = customJwtRuleHandle.getCustomConvert();        ServerHttpRequest modifiedRequest =                exchange.getRequest().mutate().header("custom", customConvert).build();
            return exchange.mutate().request(modifiedRequest).build();    }}
    
  1. 配置SPI

    custom=org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy

说明:系统会根据JwtRuleHandlehandleType参数来使用不同转化策略,比如下面的JwtRuleHandle系统会使用我们上面自定义的CustomJwtConvertStrategy。(注意:handleTypedefault或者不存在handleType属性,系统默认使用DefaultJwtConvertStrategy

{    "handleType":"custom",    "customConvert":"customConvert"}

案例代码可查看org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy