content: package com.open.capacity.client.oauth2.filter;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.InitializingBean;
  import org.springframework.security.oauth2.common.OAuth2AccessToken;
  import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
  import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  import org.springframework.util.AntPathMatcher;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.util.Enumeration;
  import java.util.HashSet;
  import java.util.Set;
  
  /**
   * IP过滤器
   *
   * @author owen 624191343@qq.com
   * <p>
   * 2017年10月14日
   */
  public class IPFilter extends OncePerRequestFilter implements InitializingBean {
  
      Logger logger = LoggerFactory.getLogger(getClass());
  
      private AuthenticationFailureHandler authenticationFailureHandler;
  
      public AuthenticationFailureHandler getAuthenticationFailureHandler() {
          return authenticationFailureHandler;
      }
  
      public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
          this.authenticationFailureHandler = authenticationFailureHandler;
      }
  
      // 所有需要拦截的url
      private Set<String> urls = new HashSet<>();
      // url匹配器
      private AntPathMatcher pathMatcher = new AntPathMatcher();
  
      @Override
      public void afterPropertiesSet() throws ServletException {
          super.afterPropertiesSet(); // 初始化其他bean参数
  
      }
  
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
              throws ServletException, IOException {
  
          String accessToken = null;
  
          accessToken = extractToken(request);
  
  
  //		if(!pathMatcher.match("/auth/**", request.getRequestURI())){
  //			
  //			if (accessToken == null) {
  //				authenticationFailureHandler.onAuthenticationFailure(request, response,
  //						new AuthenticationException("token 不存在") {
  //
  //							private static final long serialVersionUID = 6154557943607468780L;
  //
  //						});
  //				return;
  //			}
  //		}
  
  //		String ipAddr = this.getIpAddr(req);
  //		logger.info("请求IP地址为：[{}]", ipAddr);
  //		// 配置本地IP白名单，生产环境可放入数据库或者redis中
  //		List<String> ips = new ArrayList<String>();
  //		ips.add("127.0.0.1");
  //
  //		if (!ips.contains(ipAddr)) {
  //			logger.info("IP地址校验不通过！！！");
  //			ctx.setResponseStatusCode(401);
  //			ctx.setSendZuulResponse(false);
  //			ctx.setResponseBody("IpAddr is forbidden!");
  //		}
  //		logger.info("IP校验通过。");
  
  
          filterChain.doFilter(request, response);
      }
  
      protected String extractToken(HttpServletRequest request) {
          // first check the header...
          String token = extractHeaderToken(request);
  
          // bearer type allows a request parameter as well
          if (token == null) {
              logger.debug("Token not found in headers. Trying request parameters.");
              token = request.getParameter(OAuth2AccessToken.ACCESS_TOKEN);
              if (token == null) {
                  logger.debug("Token not found in request parameters.  Not an OAuth2 request.");
              } else {
                  request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, OAuth2AccessToken.BEARER_TYPE);
              }
          }
  
          return token;
      }
  
      /**
       * Extract the OAuth bearer token from a header.
       *
       * @param request The request.
       * @return The token, or null if no OAuth authorization header was supplied.
       */
      protected String extractHeaderToken(HttpServletRequest request) {
          Enumeration<String> headers = request.getHeaders("Authorization");
          while (headers.hasMoreElements()) { // typically there is only one (most
              // servers enforce that)
              String value = headers.nextElement();
              if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) {
                  String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim();
                  // Add this here for the auth details later. Would be better to
                  // change the signature of this method.
                  request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE,
                          value.substring(0, OAuth2AccessToken.BEARER_TYPE.length()).trim());
                  int commaIndex = authHeaderValue.indexOf(',');
                  if (commaIndex > 0) {
                      authHeaderValue = authHeaderValue.substring(0, commaIndex);
                  }
                  return authHeaderValue;
              }
          }
  
          return null;
      }
  
  }
  