4-7 이미지 코드 - 코드 사용자 정의 이미지 필터 경로 차단

사용자 정의 이미지 유효성 검사 코드 필터 경로 차단

1.1 구성 파일

security.imageCodeUrl = / 사용자 / 사용자 / * / 인증 / 양식

1.2 코어 코드

패키지 org.xyssmysql.springsecurity.learn.browser.filter; 

수입 때 java.io.IOException; 
수입 java.util.Arrays; 
가져 오기는 java.util.HashSet; 
수입 java.util.Set을; 

수입 javax.servlet.FilterChain; 
수입 javax.servlet.ServletException; 
수입 javax.servlet.ServletRequest; 
수입 javax.servlet.ServletResponse; 
수입 나오는 javax.servlet.http.HttpServletRequest; 
수입의 javax.servlet.http.HttpServletResponse; 

수입 org.apache.commons.lang3.StringUtils; 
수입 org.springframework.beans.factory.InitializingBean을; 
수입 org.springframework.beans.factory.annotation.Autowired; 
수입 org.springframework.stereotype.Component;
수입 org.springframework.util.AntPathMatcher; 
수입 org.springframework.web.filter.GenericFilterBean; 
수입 org.springframework.web.filter.OncePerRequestFilter; 
수입 org.xyssmysql.common.constant.SymbolConstant; 
수입 org.xyssmysql.common.core.exception.BusinessException; 
수입 org.xyssmysql.common.util.RequestUtil; 
수입 org.xyssmysql.springsecurity.learn.browser.controller.CommonAuthenticationFailureHandler; 
수입 org.xyssmysql.springsecurity.learn.browser.  *글로벌 .service.BrowserRedisService; 
수입 org.xyssmysql.springsecurity.learn.core.config.SecurityProperties; 
수입 org.xyssmysql.springsecurity.learn.core.exception.BaseAuthenticationException; 
/ * 
 * OncePerRequestFilter :该过滤器确保只校验一次
 * @Desc 사용자 정의 필터 인증 코드 
 * @author huangshiqing 
 * @date 
 * / 
@Component 
공공  클래스 (가) OncePerRequestFilter 확장 ImageCodeSecurityFilter 구현 InitializingBean 표시 { 

    
    @Autowired 
    BrowserRedisService browserRedisService, 
    
    @Autowired 
    개인 CommonAuthenticationFailureHandler commonAuthenticationFailureHandler, 
    
    공개 설정 <문자열> URL을 = 새로운 새로운 HashSet의를 <문자열> (); 
    
    @Autowired 
    개인 securityProperties securityProperties; 
    
    / * * 
     *로 일치 클래스 
     * /
    @Autowired 
    개인 doFilterInternal (HttpServletRequest의 요청은 응답 HttpServletResponse를 상기 filterChain filterChain)를AntPathMatcher antPathMatcher; 
  
  // 초기화 빈 필드 주입 후
@Override
공개 무효 afterPropertiesSet ()가 발생 ServletException을 { super.afterPropertiesSet (); 문자열 [] urlStrArray = StringUtils.splitByWholeSeparatorPreserveAllTokens (securityProperties.getImageCodeUrl () SymbolConstant.comma) 의 URL. 및 addAll (Arrays.asList (urlStrArray))] } / * * 필터 코어 * 인증 코드 방식 / * @Override가 보호 공간이 문자열 requestURI ServletException을, IOException가 {던졌습니다 문자열 requestMethod = request.getMethod은 (); = request.getRequestURI (); 부울 requireFilter = 거짓 ; 대한 (문자열 URL : URL을) { 경우 (antPathMatcher.match (URL, requestURI)) { requireFilter = 진정한 ; 휴식 ; } } // 不满足需要验证的条件( "/authentication/form".equals(requestURI)&& StringUtils.endsWithIgnoreCase (requestMethod,"POST ")) 경우 (requireFilter) { // 이 세션 코드 1에 대응하는 취득 된 화상 스트링 imageCodeCorrect =browserRedisService.getImageCode (RequestUtil.getSessionId (요청)); // 2. 빈 직접 응답 코드가 재 취득해야하는 경우 IF (StringUtils.isEmpty (imageCodeCorrect가)) { BaseAuthenticationException baseAuthenticationException는 = 새로운 새로운 BaseAuthenticationException ( " 코드가 만료! " ); // 투사 BusinessException.error는 ( "코드가 만료!"); commonAuthenticationFailureHandler.onAuthenticationFailure (요청, 응답, baseAuthenticationException); } // 3. 그렇지 일관성 응답 코드 인 경우 IF (imageCodeCorrect.equals (request.getParameter! ( "imageCode " 공개 무효 doFilter (ServletRequest를 요청 ServletResponse를 응답 FilterChain 쇄)))) { BaseAuthenticationException baseAuthenticationException = 새로운 BaseAuthenticationException ( " 验证码输入错误! " ); commonAuthenticationFailureHandler.onAuthenticationFailure (요청, 응답, baseAuthenticationException); } filterChain.doFilter (요청, 응답); } // 4放行 다른 { filterChain.doFilter (요청, 응답); } } / * @Override가 IOException이, {ServletException이 발생 HttpServletRequest의 HttpServletRequest의 = (HttpServletRequest의) 요구; HttpServletResponse를 HttpServletResponse를 =합니다 (HttpServletResponse를) 응답, 문자열 requestMethod HttpServletRequest.getMethod = (); 문자열 requestURI HttpServletRequest.getRequestURI = (); // 확인하는 데 필요한 조건을 만족하지 않는 경우 (( '/ 인증 / 형태 ".equals을 (requestURI) && StringUtils.endsWithIgnoreCase (requestMethod "POST"))) { . // 1 세션 코드에 대응하는 이미지를 획득한다. 문자열 imageCodeCorrect = browserRedisService.getImageCode (RequestUtil.getSessionId ((HttpServletRequest의) 요구)); // 이 비어있는 경우 2. 당신은 직접 응답 코드를 재 취득 할 필요가 있는 경우 (StringUtils.isEmpty (imageCodeCorrect)) { BaseAuthenticationException baseAuthenticationException는 = 새로운 BaseAuthenticationException ( "코드가 만료!"); } chain.doFilter (요청, 응답); } commonAuthenticationFailureHandler.onAuthenticationFailure (HttpServletRequest의, HttpServletResponse를, baseAuthenticationException); } // 3如果不一致响应验证码不一致. 만약 {(imageCodeCorrect.equals (request.getParameter ( "imageCode"))!) BaseAuthenticationException baseAuthenticationException = 새로운 BaseAuthenticationException ( "验证码输入错误!"); commonAuthenticationFailureHandler.onAuthenticationFailure (HttpServletRequest의, HttpServletResponse를, baseAuthenticationException); . // 4放行 다른 { chain.doFilter (요청, 응답); } } * / }

 

추천

출처www.cnblogs.com/xiayuer0114/p/11390160.html