关于拦截器是用注解方便,还是用配置文件写死方便的总结。

总结:

1.用注解的话,第一次写接口时 需要增加很多注解,但是也是很方便,复制粘贴就可。后期维护灵活度非常大。

2.用写死的方式的话,拦截器越多,后期维护难度越大,针对/abc接口,可能 a拦截器排除,b拦截器也排除,c拦截器来拦截,那么需要写多次,后期及难维护。举例如下:

/**
 * 拼多多接口的拦截器配置
 * 1.token
 * 2.shopid
 */
@Configuration
public class PinduoduoWebMvcConfigurer_back extends CommonWebMvcConfigurer {

    @Autowired
    VerifyRsaTokenInterceptor verifyRsaTokenInterceptor;

    @Autowired
    VerifyShopIdInterceptor verifyShopIdInterceptor;

    @Autowired
    VerifyAesTokenInterceptor verifyAesTokenInterceptor;


    /**
     * 将拦截器注册到此项目中.
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        /*
         1.验证token
         */
        registry.addInterceptor(verifyRsaTokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/buildToken") //这里需要写多次,拦截器越多,重复写的次数越多,后期越难维护
                .excludePathPatterns("/importShopId")
                .excludePathPatterns("/PinduoduoShopStatusGet")
                .excludePathPatterns("/slb.html")

                /*
                 1.TaobaoJushitaJdpUsersGet 需要这个来统计活跃度。这个接口需要的安全度不高,delphi和guanli模块都有调用,完全开放。
                 2.TaobaoJushitaJdpUserDelete 需要用这个来定时关闭 数据推送,不可拦截。用下面的Aes128拦截器拦截。
                 */
                .excludePathPatterns("/PddDdyPdpUsersGet")
                .excludePathPatterns("/PddDdyPdpUserDelete")

                .order(1);


        /*
         2.验证shopId
         */
        registry.addInterceptor(verifyShopIdInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/buildToken")
                .excludePathPatterns("/importShopId")
                .excludePathPatterns("/PinduoduoShopStatusGet")
                .excludePathPatterns("/slb.html")

                /*
                 1.TaobaoJushitaJdpUsersGet 需要这个来统计活跃度。这个接口需要的安全度不高,delphi和guanli模块都有调用,完全开放。
                 2.TaobaoJushitaJdpUserDelete  需要用这个来定时关闭 数据推送,不可拦截。用下面的Aes128拦截器拦截。
                 */
                .excludePathPatterns("/PddDdyPdpUsersGet")
                .excludePathPatterns("/PddDdyPdpUserDelete")

                .order(2);


        /*
         3.有一些接口,无法验证颁发的token,为了安全,避免接口完全开放,
           这类接口利用对称秘钥,验证固定字符串,只增加需要拦截的。多数不需要这个拦截器拦截。
           TaobaoJushitaJdpUserDelete -- 场景为,需要 guanli 模块定时调用,其它模块基本用不到。
         */
        registry.addInterceptor(verifyAesTokenInterceptor).addPathPatterns("/PddDdyPdpUserDelete").order(3);
    }

猜你喜欢

转载自www.cnblogs.com/del88/p/13169010.html