Skip to content

Commit

Permalink
Merge pull request #34 from yinjihuan/encrypt1.1
Browse files Browse the repository at this point in the history
支持忽略接口加解密功能
  • Loading branch information
yinjihuan authored May 17, 2019
2 parents 64956a0 + fce2e2e commit feba691
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public class EncryptionConfig {
* 不支持@PathVariable格式的URI
*/
private List<String> requestDecyptUriList = new ArrayList<String>();

/**
* 忽略加密的接口URI<br>
* 比如:/user/list<br>
* 不支持@PathVariable格式的URI
*/
private List<String> responseEncryptUriIgnoreList = new ArrayList<String>();

/**
* 忽略对请求内容进行解密的接口URI<br>
* 比如:/user/list<br>
* 不支持@PathVariable格式的URI
*/
private List<String> requestDecyptUriIgnoreList = new ArrayList<String>();

/**
* 响应数据编码
Expand Down Expand Up @@ -136,4 +150,29 @@ public void setOrder(int order) {
public int getOrder() {
return order;
}

public List<String> getResponseEncryptUriIgnoreList() {
// 配置了注解则用注解获取的URI
if (ApiEncryptDataInit.responseEncryptUriIgnoreList.size() > 0) {
return ApiEncryptDataInit.responseEncryptUriIgnoreList;
}
return responseEncryptUriIgnoreList;
}

public void setResponseEncryptUriIgnoreList(List<String> responseEncryptUriIgnoreList) {
this.responseEncryptUriIgnoreList = responseEncryptUriIgnoreList;
}

public List<String> getRequestDecyptUriIgnoreList() {
// 配置了注解则用注解获取的URI
if (ApiEncryptDataInit.requestDecyptUriIgnoreList.size() > 0) {
return ApiEncryptDataInit.requestDecyptUriIgnoreList;
}
return requestDecyptUriIgnoreList;
}

public void setRequestDecyptUriIgnoreList(List<String> requestDecyptUriIgnoreList) {
this.requestDecyptUriIgnoreList = requestDecyptUriIgnoreList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

boolean decryptionStatus = this.contains(encryptionConfig.getRequestDecyptUriList(), uri, req.getMethod());
boolean encryptionStatus = this.contains(encryptionConfig.getResponseEncryptUriList(), uri, req.getMethod());
boolean decryptionIgnoreStatus = this.contains(encryptionConfig.getRequestDecyptUriIgnoreList(), uri, req.getMethod());
boolean encryptionIgnoreStatus = this.contains(encryptionConfig.getResponseEncryptUriIgnoreList(), uri, req.getMethod());

// 没有配置具体加解密的URI默认全部都开启加解密
if (encryptionConfig.getRequestDecyptUriList().size() == 0
Expand All @@ -92,6 +94,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
encryptionStatus = true;
}

// 接口在忽略加密列表中
if (encryptionIgnoreStatus) {
encryptionStatus = false;
}

// 接口在忽略解密列表中
if (decryptionIgnoreStatus) {
decryptionStatus = false;
}

// 没有加解密操作
if (decryptionStatus == false && encryptionStatus == false) {
chain.doFilter(req, resp);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.cxytiandi.encrypt.springboot.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 忽略解密注解
*
* <p>加了此注解的接口将不进行数据解密操作<p>
* <p>适用于全局开启加解密操作,但是想忽略某些接口场景<p>
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DecryptIgnore {

String value() default "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cxytiandi.encrypt.springboot.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 忽略加密注解
*
* <p>加了此注解的接口将不进行数据加密操作<p>
* <p>适用于全局开启加解密操作,但是想忽略某些接口场景<p>
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EncryptIgnore {

String value() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

import com.cxytiandi.encrypt.springboot.HttpMethodTypePrefixConstant;
import com.cxytiandi.encrypt.springboot.annotation.Decrypt;
import com.cxytiandi.encrypt.springboot.annotation.DecryptIgnore;
import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
import com.cxytiandi.encrypt.springboot.annotation.EncryptIgnore;

public class ApiEncryptDataInit implements ApplicationContextAware {

Expand All @@ -43,6 +45,20 @@ public class ApiEncryptDataInit implements ApplicationContextAware {
*/
public static List<String> requestDecyptUriList = new ArrayList<String>();

/**
* 忽略加密的接口URI<br>
* 比如:/user/list<br>
* 不支持@PathVariable格式的URI
*/
public static List<String> responseEncryptUriIgnoreList = new ArrayList<String>();

/**
* 忽略对请求内容进行解密的接口URI<br>
* 比如:/user/list<br>
* 不支持@PathVariable格式的URI
*/
public static List<String> requestDecyptUriIgnoreList = new ArrayList<String>();

private String contextPath;

@Override
Expand Down Expand Up @@ -79,6 +95,25 @@ private void initData(Map<String, Object> beanMap) {
logger.debug("Decrypt URI: {}", uri);
requestDecyptUriList.add(uri);
}
EncryptIgnore encryptIgnore = AnnotationUtils.findAnnotation(method, EncryptIgnore.class);
if (encryptIgnore != null) {
// 注解中的URI优先级高
String uri = encryptIgnore.value();
if (!StringUtils.hasText(uri)) {
uri = getApiUri(clz, method);
}
logger.debug("EncryptIgnore URI: {}", uri);
responseEncryptUriIgnoreList.add(uri);
}
DecryptIgnore decryptIgnore = AnnotationUtils.findAnnotation(method, DecryptIgnore.class);
if (decryptIgnore != null) {
String uri = decryptIgnore.value();
if (!StringUtils.hasText(uri)) {
uri = getApiUri(clz, method);
}
logger.debug("DecryptIgnore URI: {}", uri);
requestDecyptUriIgnoreList.add(uri);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
import org.springframework.web.bind.annotation.RestController;

import com.cxytiandi.encrypt.springboot.annotation.Decrypt;
import com.cxytiandi.encrypt.springboot.annotation.DecryptIgnore;
import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
import com.cxytiandi.encrypt.springboot.annotation.EncryptIgnore;
import com.cxytiandi.encrypt_springboot_example.dto.UserDto;
import com.cxytiandi.encrypt_springboot_example.dto.UserXmlDto;

@RestController
public class UserController {

@Encrypt
//@Encrypt
@GetMapping("/encryptStr")
public String encryptStr() {
return "加密字符串";
}

@Encrypt
//@Encrypt
@GetMapping("/encryptEntity")
public UserDto encryptEntity() {
UserDto dto = new UserDto();
Expand All @@ -32,6 +34,8 @@ public UserDto encryptEntity() {

//@Encrypt
//@Decrypt
@DecryptIgnore
@EncryptIgnore
@PostMapping("/save")
public UserDto save(@RequestBody UserDto dto) {
System.err.println(dto.getId() + "\t" + dto.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.html

spring.encrypt.key=abcdef0123456789
spring.encrypt.requestDecyptUriList[0]=/save
spring.encrypt.requestDecyptUriList[1]=/decryptEntityXml
#spring.encrypt.requestDecyptUriList[0]=/save
#spring.encrypt.requestDecyptUriList[1]=/decryptEntityXml

spring.encrypt.responseEncryptUriList[0]=/encryptStr
spring.encrypt.responseEncryptUriList[1]=/encryptEntity
spring.encrypt.responseEncryptUriList[2]=/save
spring.encrypt.responseEncryptUriList[3]=/encryptEntityXml
spring.encrypt.responseEncryptUriList[4]=/decryptEntityXml
#spring.encrypt.responseEncryptUriList[0]=/encryptStr
#spring.encrypt.responseEncryptUriList[1]=/encryptEntity
#spring.encrypt.responseEncryptUriList[2]=/save
#spring.encrypt.responseEncryptUriList[3]=/encryptEntityXml
#spring.encrypt.responseEncryptUriList[4]=/decryptEntityXml
#spring.encrypt.urlPatterns[0]=/user/*
#spring.encrypt.urlPatterns[1]=/save/*
#spring.encrypt.urlPatterns[1]=/save/*

#spring.encrypt.requestDecyptUriIgnoreList[0]=/save
#spring.encrypt.responseEncryptUriIgnoreList[0]=/encryptEntity
#spring.encrypt.responseEncryptUriIgnoreList[1]=/save

0 comments on commit feba691

Please sign in to comment.