Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Security-自定义过滤器定位(添加自定义过滤器后的空白页)_Java_Spring_Spring Mvc_Spring Security_Filter - Fatal编程技术网

Java Spring Security-自定义过滤器定位(添加自定义过滤器后的空白页)

Java Spring Security-自定义过滤器定位(添加自定义过滤器后的空白页),java,spring,spring-mvc,spring-security,filter,Java,Spring,Spring Mvc,Spring Security,Filter,嗨,我已经创建了一个自定义过滤器来过滤传入的IP。然而,过滤器运行后,我得到一个空白页。没有错误!。我的过滤器是: public class IPFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2 )throws IOException, ServletException

嗨,我已经创建了一个自定义过滤器来过滤传入的IP。然而,过滤器运行后,我得到一个空白页。没有错误!。我的过滤器是:

  public class IPFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain    arg2 )throws IOException, ServletException {

String ipAddress = request.getHeader("X-FORWARDED-FOR");
        if (ipAddress == null) {
               ipAddress = request.getRemoteAddr();
        }

 if(!ipAddress.equals(ValidatedIP))
        {
            logger.debug("The IP address is invalid");
            throw new BadCredentialsException("The IP address is not registered");
        }


    }

}
调试视图:

2016-12-14 16:02:18 DEBUG HttpSessionSecurityContextRepository:337 - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2016-12-14 16:02:18 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed
2016-12-14 16:02:18 DEBUG HttpSessionSecurityContextRepository:337 - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2016-12-14 16:02:18 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 1 of 13 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2016-12-14 16:02:46 DEBUG HttpSessionSecurityContextRepository:192 - Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication'
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 2 of 13 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 3 of 13 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2016-12-14 16:02:46 DEBUG HstsHeaderWriter:128 - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@df877d
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 4 of 13 in additional filter chain; firing Filter: 'CsrfFilter'
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 5 of 13 in additional filter chain; firing Filter: 'LogoutFilter'
2016-12-14 16:02:46 DEBUG AntPathRequestMatcher:131 - Request 'GET /appointments/' doesn't match 'POST /logout
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 6 of 13 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2016-12-14 16:02:46 DEBUG AntPathRequestMatcher:131 - Request 'GET /appointments/' doesn't match 'POST /login
2016-12-14 16:02:46 DEBUG FilterChainProxy:324 - /appointments/ at position 7 of 13 in additional filter chain; firing Filter: 'IPFilter'
       User's IP add                0:0:0:0:0:0:0:1
2016-12-14 16:02:46 DEBUG HttpSessionSecurityContextRepository:337 - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2016-12-14 16:02:46 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed
在没有自定义筛选器的正常处理过程中,调试列表为:

2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 1 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2016-12-14 16:06:20 DEBUG HttpSessionSecurityContextRepository:171 - HttpSession returned null object for SPRING_SECURITY_CONTEXT
2016-12-14 16:06:20 DEBUG HttpSessionSecurityContextRepository:101 - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@1e215c6. A new one will be created.
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 2 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
2016-12-14 16:06:20 DEBUG HstsHeaderWriter:128 - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@1f6220e
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 4 of 12 in additional filter chain; firing Filter: 'CsrfFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 5 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
2016-12-14 16:06:20 DEBUG AntPathRequestMatcher:131 - Request 'GET /resources/js/global.js' doesn't match 'POST /logout
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 6 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
2016-12-14 16:06:20 DEBUG AntPathRequestMatcher:131 - Request 'GET /resources/js/global.js' doesn't match 'POST /login
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 7 of 12 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 8 of 12 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 9 of 12 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
2016-12-14 16:06:20 DEBUG AnonymousAuthenticationFilter:100 - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 5F2FDA8C9019F2D6559984A169289A9F; Granted Authorities: ROLE_ANONYMOUS'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 10 of 12 in additional filter chain; firing Filter: 'SessionManagementFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 11 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
2016-12-14 16:06:20 DEBUG FilterChainProxy:324 - /resources/js/global.js at position 12 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
2016-12-14 16:06:20 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/resources/js/global.js'; against '/appointments/*'
2016-12-14 16:06:20 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/resources/js/global.js'; against '/schedule/*'
2016-12-14 16:06:20 DEBUG FilterSecurityInterceptor:218 - Secure object: FilterInvocation: URL: /resources/js/global.js; Attributes: [ROLE_ANONYMOUS, ROLE_USER, ROLE_ADMIN]
2016-12-14 16:06:20 DEBUG FilterSecurityInterceptor:347 - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@90541710: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 5F2FDA8C9019F2D6559984A169289A9F; Granted Authorities: ROLE_ANONYMOUS
2016-12-14 16:06:20 DEBUG AffirmativeBased:65 - Voter: org.springframework.security.access.vote.RoleVoter@1bcc1ed, returned: 1
2016-12-14 16:06:20 DEBUG FilterSecurityInterceptor:242 - Authorization successful
2016-12-14 16:06:20 DEBUG FilterSecurityInterceptor:255 - RunAsManager did not change Authentication object
2016-12-14 16:06:20 DEBUG FilterChainProxy:309 - /resources/js/global.js reached end of additional filter chain; proceeding with original chain
2016-12-14 16:06:20 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher-servlet' processing GET request for [/spring-security/resources/js/global.js]
2016-12-14 16:06:20 DEBUG RequestMappingHandlerMapping:294 - Looking up handler method for path /resources/js/global.js
2016-12-14 16:06:20 DEBUG RequestMappingHandlerMapping:302 - Did not find handler method for [/resources/js/global.js]
2016-12-14 16:06:20 DEBUG SimpleUrlHandlerMapping:168 - Matching patterns for request [/resources/js/global.js] are [/resources/**]
2016-12-14 16:06:20 DEBUG SimpleUrlHandlerMapping:193 - URI Template variables for request [/resources/js/global.js] are {}
2016-12-14 16:06:20 DEBUG SimpleUrlHandlerMapping:123 - Mapping [/resources/js/global.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/resources/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1755ea4]]] and 1 interceptor
2016-12-14 16:06:20 DEBUG DispatcherServlet:947 - Last-Modified value for [/spring-security/resources/js/global.js] is: -1
2016-12-14 16:06:20 DEBUG HttpSessionSecurityContextRepository:337 - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
2016-12-14 16:06:20 DEBUG HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper:211 - Skip invoking on
2016-12-14 16:06:20 DEBUG DispatcherServlet:1034 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher-servlet': assuming HandlerAdapter completed request handling
2016-12-14 16:06:20 DEBUG DispatcherServlet:997 - Successfully completed request
2016-12-14 16:06:20 DEBUG ExceptionTranslationFilter:116 - Chain processed normally
2016-12-14 16:06:20 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed
如果添加自定义筛选器,我会发现缺少以下函数:

2016-12-14 16:06:20 DEBUG HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper:211 - Skip invoking on
2016-12-14 16:06:20 DEBUG DispatcherServlet:1034 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher-servlet': assuming HandlerAdapter completed request handling
2016-12-14 16:06:20 DEBUG DispatcherServlet:997 - Successfully completed request
2016-12-14 16:06:20 DEBUG ExceptionTranslationFilter:116 - Chain processed normally
2016-12-14 16:06:20 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed
谁能指导我如何解决这个问题。这是过滤器的定位吗?。我在表单\登录\过滤器之后添加它

注意:使用Spring Security 4.0.2


--谢谢

您的过滤器有缺陷。如果没有引发异常,则停止处理。因此,请求到此结束。您需要调用
filterChain.doFilter(请求、响应)
以继续处理。然而,为什么你甚至需要这个过滤器呢?Spring Security已经通过expression支持了这一点,
hasIpAddress
应该可以做到。@M.Deinum-感谢您的输入。。成功了!你的过滤器有缺陷。如果没有引发异常,则停止处理。因此,请求到此结束。您需要调用
filterChain.doFilter(请求、响应)
以继续处理。然而,为什么你甚至需要这个过滤器呢?Spring Security已经通过expression支持了这一点,
hasIpAddress
应该可以做到。@M.Deinum-感谢您的输入。。成功了!