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
| @Component public class VerifyCodeFilter extends GenericFilterBean { private String defaultFilterProcessUrl = "/login"; @Autowired private RedisUtil redisUtil;
@Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; if ("POST".equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) { String verCode = request.getParameter("verCode"); String verKey = request.getParameter("verKey"); String redisCode = redisUtil.get(verKey); Result result = Result.error(); if (verCode == null) { verCodeReturn(response, result, "验证码不能为空!"); return; } if (redisCode == null) { verCodeReturn(response, result, "不存在该验证码,请刷新验证码!"); return; } if (!redisCode.equals(verCode.trim().toLowerCase())) { verCodeReturn(response, result, "验证码错误!"); return; } } chain.doFilter(request, response); }
private void verCodeReturn(HttpServletResponse response, Result result, String msg) throws IOException { result.message(msg); response.setContentType("application/json;charset=utf-8"); PrintWriter out = response.getWriter(); out.write(JSON.toJSONString(result)); out.flush(); out.close(); } }
|