server:
port: #Zuul的端口号
spring:
application:
name: #Zuul的name
zuul:
routes:
user_name: #user微服务的名称
path: /user/** #配置请求URL的请求规则
serviceId: user_name #指定Eureka注册中心中的服务id
strip-prefix: true
sentiviteHeaders:
customSensitiveHeaders: true #让zuul处理cookie和重定向
#Eurkea注册发现中心
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka/
instance:
prefer-ip-address: true
Zuul.pom文件配置
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies>
ZuuL启动类
@SpringBootApplication @EnableEurekaClient //注册发现中心 @EnableZuulProxy //网关过滤器 public class EncryptApplication { public static void main(String[] args) { SpringApplication.run(EncryptApplication.class); } }
//秘钥
public class RsaKeys { //服务器公钥 private static final String serverPubKey = "使用OpenSSL生成的公钥"; //服务器私钥(经过pkcs8格式处理) private static final String serverPrvKeyPkcs8 = "使用OpenSSL生成的私钥经过pkcs8格式处理"; public static String getServerPubKey() { return serverPubKey; } public static String getServerPrvKeyPkcs8() { return serverPrvKeyPkcs8; }
}
//RSA解码加密,网上搜一下,很多
@Service("RsaService") public class RsaServiceImpl implements RsaService { /*** * RSA解密 * * @param encryptData * @return * @throws Exception */ public String RSADecryptDataPEM(String encryptData, String prvKey) throws Exception { byte[] encryptBytes = encryptData.getBytes(); byte[] prvdata = RSA.decryptByPrivateKey(Base64Utils.decode(encryptData), prvKey); String outString = new String(prvdata, "UTF-8"); return outString; } @Override public String RSADecryptDataBytes(byte[] encryptBytes, String prvKey) throws Exception { // TODO Auto-generated method stub byte[] prvdata = RSA.decryptByPrivateKey(encryptBytes, prvKey); String outString = new String(prvdata, "utf-8"); return outString; } /*** * RSA加密 * * @param data * @return * @throws Exception */ public String RSAEncryptDataPEM(String data, String pubKey) throws Exception { byte[] pubdata = RSA.encryptByPublicKey(data.getBytes("UTF-8"), pubKey); String outString = new String(Base64Utils.encode(pubdata)); return outString; } @Override public String getRsaAlgorithm() { // TODO Auto-generated method stub KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance("RSA"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return keyFactory.getAlgorithm(); } }
创建filter过滤器继承ZuulFilter
@Component public class RSARequestFilter extends ZuulFilter { //RSA加密解密接口
@Autowired private RsaService rsaService; @Override public String filterType() { //过滤器在什么时候执行,在解密之前执行 return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { //执行顺序 return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1; } @Override public boolean shouldFilter() { //是否使用过滤器 return true; } @Override public Object run() throws ZuulException { //获取Conext对象应用上下文, 从中获取req,res对象 RequestContext cxt = RequestContext.getCurrentContext(); HttpServletRequest request = cxt.getRequest(); HttpServletResponse response = cxt.getResponse(); //存放加密数据的变量 String requestData = null; String decodeData = null; try { //从req中获取请求的内容,获取通过公钥加密后的数据, ServletInputStream inputStream = request.getInputStream(); //通过流的工具类,吧流的信息转换成string类型的,指定字符集utf-8,得到公钥加密后的数据 requestData = StreamUtils.copyToString(inputStream, Charsets.UTF_8); System.out.println("加密后的:" + requestData); if (!Strings.isNullOrEmpty(requestData)) { //如果得到的数据不为空,进行解密操作 decodeData = rsaService.RSADecryptDataPEM(requestData, RsaKeys.getServerPrvKeyPkcs8()); System.out.println("解密后的:" + decodeData); } //吧解密后的数据转发给对应服务request if (!Strings.isNullOrEmpty(decodeData)) { byte[] bytes = decodeData.getBytes(); cxt.setRequest(new HttpServletRequestWrapper(request) { @Override public ServletInputStream getInputStream() { return new ServletInputStreamWrapper(bytes); } @Override public int getContentLength() { return bytes.length; } @Override public long getContentLengthLong() { return bytes.length; } }); } //进行转码操作 cxt.addZuulRequestHeader("Content-Type" , MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"); } catch (Exception e) { e.printStackTrace(); } return null; } }
在POSTMan测试前,可以先成加密后的接口密文, 比如http://127.0.0.1:9002/user/user/2 本机的9002是你的Zuul网关端口,访问配置文件中要拦截的接口,解密后,转发到user微服务查询id 等于2的用户
测试类
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = EncryptApplication.class) public class EncryptTest { @Autowired private RsaService rsaService; @Test public void genEncryptDataByPubKey() { String data = "{\"需要加密的明文\":\"需要加密的明文\"}"; try { String encData = rsaService.RSAEncryptDataPEM(data, RsaKeys.getServerPubKey()); } catch (Exception e) { e.printStackTrace(); } } @Test public void pojie() throws Exception { String mima = "明文加密后的秘钥"; //调用解密 String s = rsaService.RSADecryptDataPEM(mima, RsaKeys.getServerPrvKeyPkcs8()); System.out.println(s); } }
写的不是特别好,主要是自己学习用
原文:https://www.cnblogs.com/zxfirst/p/13427203.html