Skip to content

Commit

Permalink
Merge pull request #20 from yinjihuan/encrypt1.1
Browse files Browse the repository at this point in the history
支持spring boot配置
  • Loading branch information
yinjihuan authored Jan 25, 2019
2 parents 1162cf6 + 72dffe3 commit d84b199
Show file tree
Hide file tree
Showing 48 changed files with 798 additions and 132 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# maven ignore
target/
bin/
*.jar
*.war
*.zip
Expand Down
32 changes: 32 additions & 0 deletions encrypt-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# maven ignore
target/
*.jar
*.war
*.zip
*.tar
*.tar.gz

# eclipse ignore
.settings/
.project
.classpath

# idea ignore
.idea/
*.ipr
*.iml
*.iws

# temp ignore
*.log
*.cache
*.diff
*.patch
*.tmp
*.java~
*.properties~
*.xml~

# system ignore
.DS_Store
Thumbs.db
5 changes: 5 additions & 0 deletions encrypt-core/.project
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
Expand Down
45 changes: 45 additions & 0 deletions encrypt-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,50 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.10.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.10.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.cxytiandi.encrypt.springboot.init.ApiEncryptDataInit;

/**
* 加解密配置类
*
Expand All @@ -13,6 +17,7 @@
* @about http://cxytiandi.com/about
*
*/
@ConfigurationProperties(prefix = "spring.encrypt")
public class EncryptionConfig {

/**
Expand Down Expand Up @@ -44,6 +49,16 @@ public class EncryptionConfig {
*/
private boolean debug = false;

/**
* 过滤器拦截模式
*/
private String[] urlPatterns = new String[] { "/*" };

/**
* 过滤器执行顺序
*/
private int order = 1;

public EncryptionConfig() {
super();
}
Expand All @@ -67,6 +82,10 @@ public void setKey(String key) {
}

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

Expand All @@ -75,6 +94,10 @@ public void setResponseEncryptUriList(List<String> responseEncryptUriList) {
}

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

Expand All @@ -98,4 +121,19 @@ public void setDebug(boolean debug) {
this.debug = debug;
}

public void setUrlPatterns(String[] urlPatterns) {
this.urlPatterns = urlPatterns;
}

public String[] getUrlPatterns() {
return urlPatterns;
}

public void setOrder(int order) {
this.order = order;
}

public int getOrder() {
return order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public EncryptionFilter(EncryptionConfig config, EncryptAlgorithm encryptAlgorit
this.encryptAlgorithm = encryptAlgorithm;
}

public EncryptionFilter(String key) {
EncryptionConfig config = new EncryptionConfig();
config.setKey(key);
this.encryptionConfig = config;
}

public EncryptionFilter(String key, List<String> responseEncryptUriList, List<String> requestDecyptUriList,
String responseCharset, boolean debug) {
this.encryptionConfig = new EncryptionConfig(key, responseEncryptUriList, requestDecyptUriList, responseCharset, debug);
Expand All @@ -75,8 +81,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}

boolean decryptionStatus = encryptionConfig.getRequestDecyptUriList().contains(uri);
boolean encryptionStatus = encryptionConfig.getResponseEncryptUriList().contains(uri);

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

// 没有配置具体加解密的URI默认全部都开启加解密
if (encryptionConfig.getRequestDecyptUriList().size() == 0
Expand Down Expand Up @@ -147,6 +154,18 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha

}

private boolean contains(List<String> list, String uri, String methodType) {
if (list.contains(uri)) {
return true;
}
String prefixUri = methodType.toLowerCase() + ":" + uri;
logger.debug("contains uri: {}", prefixUri);
if (list.contains(prefixUri)) {
return true;
}
return false;
}

@Override
public void destroy() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cxytiandi.encrypt.springboot;

public class HttpMethodTypePrefixConstant {

public static final String GET = "get:";

public static final String POST = "post:";

public static final String DELETE = "delete:";

public static final String PUT = "put:";

}
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>
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Decrypt {

String value() default "";

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

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

import com.cxytiandi.encrypt.springboot.autoconfigure.EncryptAutoConfiguration;

/**
* 启用加密Starter
*
* <p>在Spring Boot启动类上加上此注解<p>
*
* <pre class="code">
* &#064;SpringBootApplication
* &#064;EnableEncrypt
* public class App {
* public static void main(String[] args) {
* SpringApplication.run(App.class, args);
* }
* }
* <pre>
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({EncryptAutoConfiguration.class})
public @interface EnableEncrypt {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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>
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Encrypt {

String value() default "";

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cxytiandi.encrypt.core.EncryptionConfig;
import com.cxytiandi.encrypt.core.EncryptionFilter;
import com.cxytiandi.encrypt.springboot.init.ApiEncryptDataInit;


/**
* 加解密自动配置
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*
*/
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties(EncryptionConfig.class)
public class EncryptAutoConfiguration {

@Autowired
private EncryptionConfig encryptionConfig;

/**
* 不要用泛型注册Filter,泛型在Spring Boot 2.x版本中才有
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FilterRegistrationBean filterRegistration() {
EncryptionConfig config = new EncryptionConfig();
config.setKey(encryptionConfig.getKey());
config.setRequestDecyptUriList(encryptionConfig.getRequestDecyptUriList());
config.setResponseEncryptUriList(encryptionConfig.getResponseEncryptUriList());
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter(config));
registration.addUrlPatterns(encryptionConfig.getUrlPatterns());
registration.setName("EncryptionFilter");
registration.setOrder(encryptionConfig.getOrder());
return registration;
}

@Bean
public ApiEncryptDataInit apiEncryptDataInit() {
return new ApiEncryptDataInit();
}
}
Loading

0 comments on commit d84b199

Please sign in to comment.