springboot1.5.x 第三部分 浏览器用户名密码加入验证码

验证码功能就是加入一个过滤器

  1 /**
  2  * 
  3  */
  4 package com.imooc.security.core.validate.code;
  5 
  6 import java.io.IOException;
  7 import java.util.HashSet;
  8 import java.util.Set;
  9 
 10 import javax.servlet.FilterChain;
 11 import javax.servlet.ServletException;
 12 import javax.servlet.http.HttpServletRequest;
 13 import javax.servlet.http.HttpServletResponse;
 14 
 15 import org.apache.commons.lang.StringUtils;
 16 import org.springframework.beans.factory.InitializingBean;
 17 import org.springframework.security.web.authentication.AuthenticationFailureHandler;
 18 import org.springframework.social.connect.web.HttpSessionSessionStrategy;
 19 import org.springframework.social.connect.web.SessionStrategy;
 20 import org.springframework.util.AntPathMatcher;
 21 import org.springframework.web.bind.ServletRequestBindingException;
 22 import org.springframework.web.bind.ServletRequestUtils;
 23 import org.springframework.web.context.request.ServletWebRequest;
 24 import org.springframework.web.filter.OncePerRequestFilter;
 25 
 26 import com.imooc.security.core.properties.SecurityProperties;
 27 
 28 /**
 29  * @author zhailiang
 30  *
 31  */
 32 public class ValidateCodeFilter extends OncePerRequestFilter implements InitializingBean {
 33     
 34     private AuthenticationFailureHandler authenticationFailureHandler;
 35     
 36     private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
 37     
 38     private Set<String> urls = new HashSet<>();
 39     
 40     private SecurityProperties securityProperties;
 41     
 42     private AntPathMatcher pathMatcher = new AntPathMatcher();
 43     
 44     @Override
 45     public void afterPropertiesSet() throws ServletException {
 46         super.afterPropertiesSet();
 47         String[] configUrls = StringUtils.splitByWholeSeparatorPreserveAllTokens(securityProperties.getCode().getImage().getUrl(), ",");
 48         for (String configUrl : configUrls) {
 49             urls.add(configUrl);
 50         }
 51         urls.add("/authentication/form");
 52     }
 53 
 54     /* (non-Javadoc)
 55      * @see org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
 56      */
 57     @Override
 58     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
 59             throws ServletException, IOException {
 60         
 61         boolean action = false;
 62         for (String url : urls) {
 63             if(pathMatcher.match(url, request.getRequestURI())){
 64                 action = true;
 65             }
 66         }
 67         
 68         if(action) {
 69             
 70             try {
 71                 validate(new ServletWebRequest(request));
 72             } catch (ValidateCodeException e) {
 73                 authenticationFailureHandler.onAuthenticationFailure(request, response, e);
 74                 return;
 75             }
 76             
 77         }
 78         
 79         filterChain.doFilter(request, response);
 80         
 81     }
 82 
 83     private void validate(ServletWebRequest request) throws ServletRequestBindingException {
 84         
 85         ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(request,
 86                 ValidateCodeController.SESSION_KEY);
 87 
 88         String codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(), "imageCode");
 89 
 90         if (StringUtils.isBlank(codeInRequest)) {
 91             throw new ValidateCodeException("验证码的值不能为空");
 92         }
 93         
 94         if(codeInSession == null){
 95             throw new ValidateCodeException("验证码不存在");
 96         }
 97 
 98         if(codeInSession.isExpried()){
 99             sessionStrategy.removeAttribute(request, ValidateCodeController.SESSION_KEY);
100             throw new ValidateCodeException("验证码已过期");
101         }
102         
103         if(!StringUtils.equals(codeInSession.getCode(), codeInRequest)) {
104             throw new ValidateCodeException("验证码不匹配");
105         }
106         
107         sessionStrategy.removeAttribute(request, ValidateCodeController.SESSION_KEY);
108     }
109 
110     public AuthenticationFailureHandler getAuthenticationFailureHandler() {
111         return authenticationFailureHandler;
112     }
113 
114     public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
115         this.authenticationFailureHandler = authenticationFailureHandler;
116     }
117 
118     public SessionStrategy getSessionStrategy() {
119         return sessionStrategy;
120     }
121 
122     public void setSessionStrategy(SessionStrategy sessionStrategy) {
123         this.sessionStrategy = sessionStrategy;
124     }
125 
126     public Set<String> getUrls() {
127         return urls;
128     }
129 
130     public void setUrls(Set<String> urls) {
131         this.urls = urls;
132     }
133 
134     public SecurityProperties getSecurityProperties() {
135         return securityProperties;
136     }
137 
138     public void setSecurityProperties(SecurityProperties securityProperties) {
139         this.securityProperties = securityProperties;
140     }
141     
142 
143 }

原文地址:https://www.cnblogs.com/buxiu/p/14064036.html