请问在重构validateCodeFilter无法提交图形验证码
来源:4-13 小结

向量烟灰
2019-03-08
请问在重构validateCodeFilter中
为什么validateCodeProcessorHolder有时有值有时又是null
我在提交图形验证码时validateCodeProcessorHolder是null
所以没有办法进入验证validate的过程。
请帮忙指点一下,谢谢!
@Component("validateCodeFilter")
public class ValidateCodeFilter extends OncePerRequestFilter implements InitializingBean {
private Logger logger = LoggerFactory.getLogger(getClass());
//======================================================
/**
* 验证码校验失败处理器
*/
@Autowired
private AuthenticationFailureHandler authenticationFailureHandler;
/**
* 系统配置信息
*/
@Autowired
private SecurityProperties securityProperties;
/**
* 系统中的校验码处理器
*/
@Autowired
private ValidateCodeProcessorHolder validateCodeProcessorHolder;
/**
* 存放所有需要校验验证码的url
*/
private Map<String, ValidateCodeType> urlMap = new HashMap<>();
/**
* 验证请求url与配置的url是否匹配的工具类
*/
private AntPathMatcher pathMatcher = new AntPathMatcher();
//=========================================================
/**
* 初始化要拦截的url配置信息
* @throws ServletException
*/
@Override
public void afterPropertiesSet() throws ServletException {
super.afterPropertiesSet();
//放入图形验证码相关配置信息
urlMap.put(SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_FORM, ValidateCodeType.IMAGE);
addUrlToMap(securityProperties.getCode().getImage().getUrl(), ValidateCodeType.IMAGE);
//放入短信验证码相关配置信息
urlMap.put(SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_MOBILE, ValidateCodeType.SMS);
addUrlToMap(securityProperties.getCode().getSms().getUrl(), ValidateCodeType.SMS);
}
/**
* 让系统中配置的需要校验验证码的URL根据校验的类型放入map
*
* @param urlString
* @param type
*/
protected void addUrlToMap(String urlString, ValidateCodeType type) {
if (StringUtils.isNotBlank(urlString)) {
String[] urls = StringUtils.splitByWholeSeparatorPreserveAllTokens(urlString, ",");
for (String url : urls) {
urlMap.put(url, type);
}
}
}
/**
* 执行验证码过滤器
* @param request
* @param response
* @param filterChain
* @throws ServletException
* @throws IOException
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//获取验证类型
ValidateCodeType type = getValidateCodeType(request);
if (type != null) {
logger.info("验证码请求【{}】中的验证码,验证码类型为【{}】",request.getRequestURI(), type );
try {
logger.error(String.valueOf(validateCodeProcessorHolder));
logger.error(String.valueOf(validateCodeProcessorHolder.findValidateCodeProcessor(type)));
//使用依赖查找方式相关验证码处理方法
validateCodeProcessorHolder.findValidateCodeProcessor(type)
.validate(new ServletWebRequest(request, response));
logger.info("{}验证码校验通过!",type);
} catch (ValidateCodeException exception) {
authenticationFailureHandler.onAuthenticationFailure(request, response, exception);
return;
}
}
filterChain.doFilter(request,response);
}
/**
* 获取校验码的类型,如果当前请求不需要校验,则返回null
* @param request
* @return
*/
private ValidateCodeType getValidateCodeType(HttpServletRequest request) {
ValidateCodeType result = null;
if (!StringUtils.equalsIgnoreCase(request.getMethod(), "get")) {
Set<String> urls = urlMap.keySet();
for (String url : urls) {
if (pathMatcher.match(url, request.getRequestURI())) {
result = urlMap.get(url);
}
}
}
return result;
}
}
写回答
1回答
-
看代码没什么问题,你可以检查下 ValidateCodeFilter 是不是有另外的实例,导致注入时注入的不对。
00
Spring Security技术栈开发企业级认证与授权
Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全
2662 学习 · 1561 问题
相似问题