标签:Feign 拦截器 String RequestParam boot springframework param import org
一、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<String> paramList = template.queries().get("param");
String param = paramList.iterator().next();
try {
// 加密
param = SM4Util.encryptEcb("1234567890", param);
} catch (Exception e) {
e.printStackTrace();
}
Map<String, Collection<String>> newQueries = new HashMap<>();
Collection<String> 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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xiaohang</groupId>
<artifactId>pre-socialcard-changsha</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.SR5</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
标签:Feign,拦截器,String,RequestParam,boot,springframework,param,import,org 来源: https://blog.51cto.com/12012821/2417863
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。