网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringCloud使用Feign拦截器实现URL过滤和RequestParam加密

一、FeignInterceptor.class拦截器
package com.xiaohang.socialcard.pre.intercepter;

import com.xiaohang.socialcard.pre.utils.SM4Util;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.*;

@Configuration
@Slf4j
public class FeignInterceptor implements RequestInterceptor {

    @Value("${encrypt.urls}")
    private String[] urlList;

    @Override
    public void apply(RequestTemplate template) {
        if (Arrays.asList(urlList).contains(template.url())) {//根据URL地址过滤请求
            log.info("请求参数为:{}", template.queryLine());//?param=123456
            Collection paramList = template.queries().get("param");
            String param = paramList.iterator().next();
            try {
                // 加密
                param = SM4Util.encryptEcb("1234567890", param);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Map> newQueries = new HashMap<>();
            Collection value = new ArrayList<>();
            value.add(param);
            newQueries.put("param", value);
            template.queries(newQueries);// 替换原有对象
            log.info("加密后参数为:{}", template.queryLine());//?param=xxxxxx
        }
    }
}
二、配置文件

application.yml

长寿网站建设公司创新互联建站,长寿网站设计制作,有大型网站制作公司丰富经验。已为长寿超过千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的长寿做网站的公司定做!

changsha-payment.url: http://xx.xx.xx.xx:8080
encrypt.urls: /openInter/getToken,/openInter/dispatchPayEx,/bcs/balance
三、APIFeign接口
package com.xiaohang.socialcard.pre.feign;

import com.xiaohang.socialcard.pre.model.ChangShaResp;
import com.xiaohang.socialcard.pre.pojo.Loffee;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "changsha-payment", url = "${changsha-payment.url}")
public interface ChangShaApi {

    @RequestMapping(method = RequestMethod.GET, path = "openInter/getToken", consumes = "*/*", produces = "application/json;charset=UTF-8")
    ChangShaResp getToken(@RequestParam(name = "param", required = true) String param);
    @RequestMapping(method = RequestMethod.POST, value = "openInter/dispatchPayEx", produces = "text/html", consumes = "application/x-www-form-urlencoded")
    String payPage(@RequestParam(name = "param", required = true) String param);

    @RequestMapping(method = RequestMethod.POST, path = "/bcs/balance", produces = "text/html", consumes = "application/x-www-form-urlencoded")
    String downLoadPay(@RequestParam(name = "param", required = true) String param);
四、启动类Application.class
package com.xiaohang.socialcard.pre;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
五、pom.xml


    4.0.0

    com.xiaohang
    pre-socialcard-changsha
    1.0-SNAPSHOT
    jar

    
        org.springframework.cloud
        spring-cloud-starter-parent
        Camden.SR5
    
    
        UTF-8
        1.8
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
            1.2.3.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
            1.2.5.RELEASE
        
        
            org.projectlombok
            lombok
            1.16.18
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.9
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        org.springframework
                        springloaded
                        1.2.5.RELEASE
                    
                
            
        
    

文章标题:SpringCloud使用Feign拦截器实现URL过滤和RequestParam加密
转载注明:http://bjjierui.cn/article/gsshie.html

其他资讯