包名错误修复,验证码过滤器逻辑修改

This commit is contained in:
hesling 2018-04-26 10:33:20 +08:00
parent 5a8c62c843
commit c6f4770d9a
10 changed files with 35 additions and 33 deletions

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.config; package com.github.pig.gateway.component.config;
import com.github.pig.common.constant.ServiceNameConstant; import com.github.pig.common.constant.ServiceNameConstant;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;

View File

@ -1,8 +1,8 @@
package com.github.pig.gateway.componet.config; package com.github.pig.gateway.component.config;
import com.github.pig.common.bean.config.FilterUrlsPropertiesConfig; import com.github.pig.common.bean.config.FilterUrlsPropertiesConfig;
import com.github.pig.gateway.componet.filter.ValidateCodeFilter; import com.github.pig.gateway.component.filter.ValidateCodeFilter;
import com.github.pig.gateway.componet.handler.PigAccessDeniedHandler; import com.github.pig.gateway.component.handler.PigAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.fallback; package com.github.pig.gateway.component.fallback;
import com.github.pig.common.constant.ServiceNameConstant; import com.github.pig.common.constant.ServiceNameConstant;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.fallback; package com.github.pig.gateway.component.fallback;
import com.github.pig.common.constant.ServiceNameConstant; import com.github.pig.common.constant.ServiceNameConstant;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.filter; package com.github.pig.gateway.component.filter;
import com.github.pig.common.constant.SecurityConstants; import com.github.pig.common.constant.SecurityConstants;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.filter; package com.github.pig.gateway.component.filter;
import com.github.pig.gateway.service.LogSendService; import com.github.pig.gateway.service.LogSendService;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.filter; package com.github.pig.gateway.component.filter;
import com.github.pig.gateway.service.LogSendService; import com.github.pig.gateway.service.LogSendService;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.filter; package com.github.pig.gateway.component.filter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
@ -31,6 +31,9 @@ import java.io.PrintWriter;
@Component("validateCodeFilter") @Component("validateCodeFilter")
public class ValidateCodeFilter extends OncePerRequestFilter { public class ValidateCodeFilter extends OncePerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(ValidateCodeFilter.class); private static final Logger logger = LoggerFactory.getLogger(ValidateCodeFilter.class);
private static final String EXPIRED_CAPTCHA_ERROR = "验证码已过期,请重新获取验证码";
@Value("${security.validate.code:true}") @Value("${security.validate.code:true}")
private boolean isValidate; private boolean isValidate;
@Autowired @Autowired
@ -64,35 +67,38 @@ public class ValidateCodeFilter extends OncePerRequestFilter {
private void checkCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException { private void checkCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException {
String code = httpServletRequest.getParameter("code"); String code = httpServletRequest.getParameter("code");
if (StringUtils.isBlank(code)) {
throw new ValidateCodeException("请输入验证码");
}
String randomStr = httpServletRequest.getParameter("randomStr"); String randomStr = httpServletRequest.getParameter("randomStr");
if (StringUtils.isBlank(randomStr)) { if (StringUtils.isBlank(randomStr)) {
randomStr = httpServletRequest.getParameter("mobile"); randomStr = httpServletRequest.getParameter("mobile");
} }
Object codeObj = redisTemplate.opsForValue().get(SecurityConstants.DEFAULT_CODE_KEY + randomStr);
String key = SecurityConstants.DEFAULT_CODE_KEY + randomStr;
if (!redisTemplate.hasKey(key)) {
throw new ValidateCodeException(EXPIRED_CAPTCHA_ERROR);
}
Object codeObj = redisTemplate.opsForValue().get(key);
if (codeObj == null) { if (codeObj == null) {
throw new ValidateCodeException("验证码为空或已过期"); throw new ValidateCodeException(EXPIRED_CAPTCHA_ERROR);
} }
String saveCode = codeObj.toString(); String saveCode = codeObj.toString();
if (StringUtils.isBlank(saveCode)) {
if (StringUtils.isBlank(code)) { redisTemplate.delete(key);
redisTemplate.delete(SecurityConstants.DEFAULT_CODE_KEY + randomStr); throw new ValidateCodeException(EXPIRED_CAPTCHA_ERROR);
throw new ValidateCodeException("验证码的值不能为空");
}
if (StringUtils.isEmpty(saveCode)) {
redisTemplate.delete(SecurityConstants.DEFAULT_CODE_KEY + randomStr);
throw new ValidateCodeException("验证码已过期或已过期");
} }
if (!StringUtils.equals(saveCode, code)) { if (!StringUtils.equals(saveCode, code)) {
redisTemplate.delete(SecurityConstants.DEFAULT_CODE_KEY + randomStr); redisTemplate.delete(key);
throw new ValidateCodeException("验证码不匹配"); throw new ValidateCodeException("验证码错误,请重新输入");
} }
if (StringUtils.equals(code, saveCode)) { redisTemplate.delete(key);
redisTemplate.delete(SecurityConstants.DEFAULT_CODE_KEY + randomStr);
filterChain.doFilter(httpServletRequest, httpServletResponse); filterChain.doFilter(httpServletRequest, httpServletResponse);
} }
}
} }

View File

@ -1,15 +1,11 @@
package com.github.pig.gateway.componet.handler; package com.github.pig.gateway.component.handler;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R; import com.github.pig.common.util.R;
import com.github.pig.common.util.exception.PigDeniedException; import com.github.pig.common.util.exception.PigDeniedException;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.util.URLUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler; import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;

View File

@ -1,4 +1,4 @@
package com.github.pig.gateway.componet.handler; package com.github.pig.gateway.component.handler;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.DefaultRateLimiterErrorHandler; import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.DefaultRateLimiterErrorHandler;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.RateLimiterErrorHandler; import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.RateLimiterErrorHandler;