ValidateCodeFilter.java 2.9 KB
Newer Older
zlt2000's avatar
zlt2000 已提交
1 2 3
package com.central.oauth.filter;

import com.central.common.constant.SecurityConstants;
4
import com.central.common.utils.ResponseUtil;
zlt2000's avatar
zlt2000 已提交
5 6 7 8
import com.central.oauth.exception.ValidateCodeException;
import com.central.oauth.service.IValidateCodeService;
import com.central.oauth2.common.properties.SecurityProperties;
import com.central.oauth2.common.util.AuthUtils;
9
import com.fasterxml.jackson.databind.ObjectMapper;
zlt2000's avatar
zlt2000 已提交
10 11
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpStatus;
zlt2000's avatar
zlt2000 已提交
13 14 15 16
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.filter.OncePerRequestFilter;

17
import javax.annotation.Resource;
zlt2000's avatar
zlt2000 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zlt
 * @date 2018/11/21
 */
@Slf4j
@Component("validateCodeFilter")
public class ValidateCodeFilter extends OncePerRequestFilter {
    @Autowired
    private IValidateCodeService validateCodeService;

    @Autowired
    private SecurityProperties securityProperties;

37 38
    @Resource
    private ObjectMapper objectMapper;
zlt2000's avatar
zlt2000 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    /**
     * 验证请求url与配置的url是否匹配的工具类
     */
    private AntPathMatcher pathMatcher = new AntPathMatcher();

    /**
     * 返回true代表不执行过滤器,false代表执行
     */
    @Override
    protected boolean shouldNotFilter(HttpServletRequest request) {
        //登录提交的时候验证验证码
        if (pathMatcher.match(SecurityConstants.PASSWORD_LOGIN_PRO_URL, request.getRequestURI())) {
            //判断是否有不验证验证码的client
            if (securityProperties.getCode().getIgnoreClientCode().length > 0) {
                try {
                    final String[] clientInfos = AuthUtils.extractClient(request);
                    String clientId = clientInfos[0];
                    for (String client : securityProperties.getCode().getIgnoreClientCode()) {
                        if (client.equals(clientId)) {
                            return true;
                        }
                    }
62
                } catch (Exception e) {
zlt2000's avatar
zlt2000 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
                    log.error("解析client信息失败", e);
                }
            }
            return false;
        }
        return true;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            validateCodeService.validate(request);
        } catch (ValidateCodeException e) {
76
            ResponseUtil.responseWriter(objectMapper, response, e.getMessage(), HttpStatus.BAD_REQUEST.value());
zlt2000's avatar
zlt2000 已提交
77 78 79 80 81
            return;
        }
        chain.doFilter(request, response);
    }
}