(5条消息) Java接口加密---filter aes加密_学
2022-12-11 本文已影响0人
学习微站
学习微站 已于 2022-10-05 11:07:48 修改 998 收藏
于 2022-05-22 16:51:10 首次发布
本网站刊载的所有内容,包括文字、图片、音频、视频、软件、程序、以及网页版式设计等在网上搜集或者是自己原创的。 访问者可将本网站提供的内容或服务用于时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。除此以外,将本网站任何内容或服务用于其他用途时,须征得本网站及相关权利人的书面许可,并支付报酬。 不得以任何形式侵犯作者权益,最终解释权归本人所有。
filter aes加密
Java接口加密:可以对请求参数、响应参数加密 加密的两种方法推荐
1、使用过滤器加密filter
AES加密
/** * 过滤器拦截请求,实现加密解密功能 * @author samxie * @version 1.0 * @date 2022/5/6 18:13 * @Component 将此Filter交给Spring容器管理 * @WebFilter 通过WebFilter进行Filter声明,这样容器在进行部署的时候就会处理该Filter * */ @SuppressWarnings("PMD") //CHECKSTYLE:OFF @Slf4j @Component public class EncryptFilter implements Filter { //LTAI4FzVG1h.....密文 @Value("${gateway.secret.key}") private String aesKey; //屏蔽的环境:local,dev @Value("${env.encrypt.profile}") private String encryptProfile; @Resource private Environment environment; //屏蔽的url private String[] ignoreUrl = new String[] { "/v1/saas/login", "/v1/saas/sendLoginSms", //自己加 }; private AntPathMatcher antPathMatcher = new AntPathMatcher(); @Override public void init(FilterConfig filterConfig) throws ServletException { // Auto-generated method stub } /** * 有错误相应返回-44 * * @param response * @throws IOException */ private void getFailResponse(HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = null; out = response.getWriter(); //加密后的错误消息 {"code":0,"data":"系统繁忙,请稍后再试","ok":true} String errorMessage = "D9CHXXPAM3SIJmYEyF6QUQhEqfHJldkVqXf4th3Ev7DuagxdHgt5MsRVphBYi7yHTihZmKhKv3YYwAf1Dk77PA=="; out.write(JSONObject.toJSONString(errorMessage)); out.flush(); out.close(); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse)response; // 过滤请求:路径 boolean flag = isIgnore(httpRequest, ignoreUrl); // 环境过滤 String env = environment.getProperty("spring.profiles.active"); //local dev test String profile = encryptProfile; if (null != env && profile.contains(env)) { flag = true; } if(flag) { try { chain.doFilter(httpRequest, httpResponse); } catch (IOException e) { log.error("e:{}", e); } catch (ServletException e) { log.error("e:{}", e); } } else { try { //响应处理 包装响应对象 res 并缓存响应数据 ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response); //执行业务逻辑 交给下一个过滤器或servlet处理 chain.doFilter(request, responseWrapper); byte[] resData = responseWrapper.getResponseData(); //设置响应内容格式,防止解析响应内容时出错 responseWrapper.setContentType("text/plain;charset=UTF-8"); //加密响应报文并响应 String encryptBASE64 = AesEncryptUtils.encrypt(new String(resData), aesKey); PrintWriter out = response.getWriter(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); out.write(JSONObject.toJSONString(encryptBASE64)); //out.print(encryptBASE64); out.flush(); out.close(); } catch(Exception e) { try { getFailResponse((HttpServletResponse)response); } catch (IOException ioException) { ioException.printStackTrace(); } log.error("加密异常信息 ", e); } } } @Override public void destroy() { // Auto-generated method stub } /** * 哪些路径不处理 * @param request * @param strArr * @return */ public boolean isIgnore(HttpServletRequest request, String[] strArr) { String path = request.getRequestURI(); for(String ignore : strArr) { boolean match = antPathMatcher.match(ignore, path); if (match) { return true; } } return false; } } //CHECKSTYLE:OFF
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
工具类
/** * aes加密解密 */ public class AesEncryptUtils { //参数分别代表 算法名称/加密模式/数据填充方式 private static String algorithmstr = "AES/ECB/PKCS5Padding"; public static String getAlgorithmstr() { return algorithmstr; } /** * 加密 * @param content 加密的字符串 * @param encryptKey key值 * @return */ public static String encrypt(String content, String encryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(algorithmstr); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES")); byte[] b = cipher.doFinal(content.getBytes("utf-8")); return Base64.encodeBase64String(b); } /** * 解密 * @param encryptStr 解密的字符串 * @param decryptKey 解密的key值 * @return */ public static String decrypt(String encryptStr, String decryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(algorithmstr); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES")); byte[] encryptBytes = Base64.decodeBase64(encryptStr); byte[] decryptBytes = cipher.doFinal(encryptBytes); return new String(decryptBytes); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
2、使用aop面向切面加密
filter aes加密是拦截器加密,而aop是面向切面方式加密,两者都可以实现不同程度的加密。
本文使用 文章同步助手 同步