zoukankan      html  css  js  c++  java
  • monkey-api-encrypt 1.1.2版本发布啦

    时隔10多天,monkey-api-encrypt发布了第二个版本,还是要感谢一些正在使用的朋友们,提出了一些问题。

    GitHub主页:https://github.com/yinjihuan/monkey-api-encrypt

    本次更新内容如下:

    • 支持Spring Boot配置
    • 支持注解开启加解密(Spring Boot中)
    • 增加Spring MVC示例

    手动注册过滤器使用

    @Configuration
    public class FilterConfig {
    
        @Bean
        public FilterRegistrationBean<EncryptionFilter> filterRegistration() {
        	EncryptionConfig config = new EncryptionConfig();
        	config.setKey("abcdef0123456789");
        	config.setRequestDecyptUriList(Arrays.asList("/save", "/decryptEntityXml"));
        	config.setResponseEncryptUriList(Arrays.asList("/encryptStr", "/encryptEntity", "/save", "/encryptEntityXml", "/decryptEntityXml"));
            FilterRegistrationBean<EncryptionFilter> registration = new FilterRegistrationBean<EncryptionFilter>();
            registration.setFilter(new EncryptionFilter(config));
            registration.addUrlPatterns("/*");
            registration.setName("EncryptionFilter");
            registration.setOrder(1);
            return registration;
        }
    
    }
    

    Spring Boot Starter方式使用

    启动类加@EnableEncrypt注解,开启加解密自动配置,省略了手动注册Filter的步骤

    @EnableEncrypt
    @SpringBootApplication
    public class App {
    
    	public static void main(String[] args) {
    		SpringApplication.run(App.class, args);
    	}
    	
    }
    
    

    配置文件中配置加密的信息,也就是EncryptionConfig

    spring.encrypt.key=abcdef0123456789
    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
    

    如果感觉配置比较繁琐,你的加解密接口很多,需要大量的配置,还可以采用另一种方式来标识加解密,就是注解的方式。

    响应的数据需要加密,就在接口的方法上加@Encrypt注解

    @Encrypt
    @GetMapping("/encryptEntity")
    public UserDto encryptEntity() {
    	UserDto dto = new UserDto();
    	dto.setId(1);
    	dto.setName("加密实体对象");
    	return dto;
    }
    

    接收的数据需要解密,就在接口的方法上加@Decrypt注解

    @Decrypt
    @PostMapping("/save")
    public UserDto save(@RequestBody UserDto dto) {
    	System.err.println(dto.getId() + "	" + dto.getName());
    	return dto;
    }
    

    同时需要加解密那么两个注解都加上即可

    @Encrypt
    @Decrypt
    @PostMapping("/save")
    public UserDto save(@RequestBody UserDto dto) {
    	System.err.println(dto.getId() + "	" + dto.getName());
    	return dto;
    }
    

    Spring MVC中使用

    Spring MVC中可以直接在web.xml中注册Filter,不方便传递的是配置的参数,我们可以配置一个自定的过滤器,然后在这个过滤器中配置EncryptionFilter

    public class ApiEncryptionFilter implements Filter {
    
    	EncryptionFilter filter = null;
    	
    	@Override
    	public void init(FilterConfig filterConfig) throws ServletException {
    		EncryptionConfig config = new EncryptionConfig();
    		config.setKey("abcdef0123456789");
    		config.setRequestDecyptUriList(Arrays.asList("/save"));
    		config.setResponseEncryptUriList(Arrays.asList("/encryptEntity"));
    		filter = new EncryptionFilter(config);
    	}
    
    	@Override
    	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    			throws IOException, ServletException {
    		filter.doFilter(request, response, chain);
    	}
    
    	@Override
    	public void destroy() {
    		
    	}
    
    }
    

    web.xml

    <filter>
            <description>自定义加解密过滤器</description>  
            <filter-name>ApiEncryptionFilter</filter-name>
            <filter-class>com.cxytiandi.mvc.filter.ApiEncryptionFilter</filter-class>
    </filter>
    
    <filter-mapping>
            <filter-name>ApiEncryptionFilter</filter-name>
            <url-pattern>/*</url-pattern>
     </filter-mapping>
    

    如果需要使用注解的话需要在spring的xml中配置ApiEncryptDataInit

    <bean id="apiEncryptDataInit" class="com.cxytiandi.encrypt.springboot.init.ApiEncryptDataInit"></bean>
    

    注意事项

    要么使用手动注册Filter的方式开启加解密功能,手动构造EncryptionConfig传入EncryptionFilter中,要么使用@EnableEncrypt开启加解密功能。

    @EnableEncrypt+配置文件可以在Spring Boot,Spring Cloud Zuul中使用

    @EnableEncrypt+@Encrypt+@Decrypt可以在Spring Boot,Spring MVC中使用

    相同URI问题

    当存在两个相同的URI时,比如GET请求的/user和POST的请求/user。如果只想对其中某一个进行处理,我们的逻辑的是按照URI进行匹配,这样就会影响到另一个,原因是URI是一样的。

    如果是使用@Encrypt+@Decrypt的方式,框架会自动处理,会为每一个URI加上前缀来区分不同的请求方式。同时提供了扩展的属性值,在@Encrypt+@Decrypt中都有value属性,可以手动配置uri。因为某些框架不是用的Spring MVC的注解,比如CXF,框架无法做到适配所有的注解,这个时候可以用uri属性来配置。

    配置格式为:请求类型+访问的URI

    get:/user
    
    post:/user
    

    包括在配置文件中也可以采用前缀的方式来区分相同的URI。

    欢迎加入我的知识星球,一起交流技术,免费学习猿天地的课程(http://cxytiandi.com/course)

    微信扫码加入猿天地知识星球

  • 相关阅读:
    服务器Jmail配置问题
    Silverlight视频教程、资源下载。如果你觉得看图文不够形象,不够生动,那就看看视频吧。
    AspNetPager,asp.net分页的最终解决方案!
    VS2008的Web Application——net 1.1 CodeBehind 模式的回归(非编译模式)
    修复Jscript(IE浏览器脚本引擎)异常
    SQL Server中查看SQL句子执行所用的时间
    SilverLight开发系列第1步:搭建开发环境
    SilverLight开发系列第2步:使用vs2008和Blend 2.5打造Hellow World程序
    谨慎使用Paypal一类的 支付 中介公司
    一个典型的数据库操作事务死锁分析
  • 原文地址:https://www.cnblogs.com/yinjihuan/p/10464221.html
Copyright © 2011-2022 走看看