Search in sources :

Example 66 with Filter

use of javax.servlet.Filter in project shiro by apache.

the class ShiroWebModuleTest method testAddFilterChainGuice3and4.

/**
 * @since 1.4
 */
@Test
public void testAddFilterChainGuice3and4() {
    final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
    ServletContext servletContext = createMock(ServletContext.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    servletContext.setAttribute(eq(EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY), EasyMock.anyObject());
    expect(request.getAttribute("javax.servlet.include.context_path")).andReturn("").anyTimes();
    expect(request.getCharacterEncoding()).andReturn("UTF-8").anyTimes();
    expect(request.getAttribute("javax.servlet.include.request_uri")).andReturn("/test_authc");
    expect(request.getAttribute("javax.servlet.include.request_uri")).andReturn("/test_custom_filter");
    expect(request.getAttribute("javax.servlet.include.request_uri")).andReturn("/test_authc_basic");
    expect(request.getAttribute("javax.servlet.include.request_uri")).andReturn("/test_perms");
    expect(request.getAttribute("javax.servlet.include.request_uri")).andReturn("/multiple_configs");
    replay(servletContext, request);
    Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {

        @Override
        protected void configureShiroWeb() {
            bindRealm().to(ShiroModuleTest.MockRealm.class);
            expose(FilterChainResolver.class);
            this.addFilterChain("/test_authc/**", filterConfig(AUTHC));
            this.addFilterChain("/test_custom_filter/**", Key.get(CustomFilter.class));
            this.addFilterChain("/test_authc_basic/**", AUTHC_BASIC);
            this.addFilterChain("/test_perms/**", filterConfig(PERMS, "remote:invoke:lan,wan"));
            this.addFilterChain("/multiple_configs/**", filterConfig(AUTHC), filterConfig(ROLES, "b2bClient"), filterConfig(PERMS, "remote:invoke:lan,wan"));
        }

        @Provides
        public ShiroModuleTest.MockRealm createRealm() {
            return mockRealm;
        }
    });
    FilterChainResolver resolver = injector.getInstance(FilterChainResolver.class);
    assertThat(resolver, instanceOf(SimpleFilterChainResolver.class));
    SimpleFilterChainResolver simpleFilterChainResolver = (SimpleFilterChainResolver) resolver;
    // test the /test_authc resource
    FilterChain filterChain = simpleFilterChainResolver.getChain(request, null, null);
    assertThat(filterChain, instanceOf(SimpleFilterChain.class));
    Filter nextFilter = getNextFilter((SimpleFilterChain) filterChain);
    assertThat(nextFilter, instanceOf(FormAuthenticationFilter.class));
    // test the /test_custom_filter resource
    filterChain = simpleFilterChainResolver.getChain(request, null, null);
    assertThat(filterChain, instanceOf(SimpleFilterChain.class));
    nextFilter = getNextFilter((SimpleFilterChain) filterChain);
    assertThat(nextFilter, instanceOf(CustomFilter.class));
    // test the /test_authc_basic resource
    filterChain = simpleFilterChainResolver.getChain(request, null, null);
    assertThat(filterChain, instanceOf(SimpleFilterChain.class));
    nextFilter = getNextFilter((SimpleFilterChain) filterChain);
    assertThat(nextFilter, instanceOf(BasicHttpAuthenticationFilter.class));
    // test the /test_perms resource
    filterChain = simpleFilterChainResolver.getChain(request, null, null);
    assertThat(filterChain, instanceOf(SimpleFilterChain.class));
    nextFilter = getNextFilter((SimpleFilterChain) filterChain);
    assertThat(nextFilter, instanceOf(PermissionsAuthorizationFilter.class));
    // test the /multiple_configs resource
    filterChain = simpleFilterChainResolver.getChain(request, null, null);
    assertThat(filterChain, instanceOf(SimpleFilterChain.class));
    assertThat(getNextFilter((SimpleFilterChain) filterChain), instanceOf(FormAuthenticationFilter.class));
    assertThat(getNextFilter((SimpleFilterChain) filterChain), instanceOf(RolesAuthorizationFilter.class));
    assertThat(getNextFilter((SimpleFilterChain) filterChain), instanceOf(PermissionsAuthorizationFilter.class));
    verify(servletContext, request);
}
Also used : FilterChainResolver(org.apache.shiro.web.filter.mgt.FilterChainResolver) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) FilterChain(javax.servlet.FilterChain) Provides(com.google.inject.Provides) BasicHttpAuthenticationFilter(org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter) HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicHttpAuthenticationFilter(org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter) RolesAuthorizationFilter(org.apache.shiro.web.filter.authz.RolesAuthorizationFilter) Filter(javax.servlet.Filter) PermissionsAuthorizationFilter(org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext) RolesAuthorizationFilter(org.apache.shiro.web.filter.authz.RolesAuthorizationFilter) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest) PermissionsAuthorizationFilter(org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter) Test(org.junit.Test) ShiroModuleTest(org.apache.shiro.guice.ShiroModuleTest)

Example 67 with Filter

use of javax.servlet.Filter in project shiro by apache.

the class ShiroFilterFactoryBean method postProcessBeforeInitialization.

/**
 * Inspects a bean, and if it implements the {@link Filter} interface, automatically adds that filter
 * instance to the internal {@link #setFilters(java.util.Map) filters map} that will be referenced
 * later during filter chain construction.
 */
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof Filter) {
        log.debug("Found filter chain candidate filter '{}'", beanName);
        Filter filter = (Filter) bean;
        applyGlobalPropertiesIfNecessary(filter);
        getFilters().put(beanName, filter);
    } else {
        log.trace("Ignoring non-Filter bean '{}'", beanName);
    }
    return bean;
}
Also used : AuthenticationFilter(org.apache.shiro.web.filter.authc.AuthenticationFilter) AuthorizationFilter(org.apache.shiro.web.filter.authz.AuthorizationFilter) AccessControlFilter(org.apache.shiro.web.filter.AccessControlFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) Filter(javax.servlet.Filter)

Example 68 with Filter

use of javax.servlet.Filter in project PublicCMS-preview by sanluan.

the class BaseServletInitializer method getServletFilters.

@Override
protected Filter[] getServletFilters() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding(DEFAULT_CHARSET_NAME);
    characterEncodingFilter.setForceEncoding(true);
    return new Filter[] { characterEncodingFilter };
}
Also used : CharacterEncodingFilter(org.springframework.web.filter.CharacterEncodingFilter) Filter(javax.servlet.Filter) CharacterEncodingFilter(org.springframework.web.filter.CharacterEncodingFilter)

Example 69 with Filter

use of javax.servlet.Filter in project PublicCMS-preview by sanluan.

the class ResourceInitializer method onStartup.

public void onStartup(ServletContext servletContext) throws ServletException {
    Dynamic registration = servletContext.addServlet("defaultServlet", new HttpRequestHandlerServlet());
    registration.setLoadOnStartup(1);
    registration.addMapping(new String[] { "/resource/*", "/favicon.ico" });
    Dynamic webfileRegistration = servletContext.addServlet("webfileServlet", new HttpRequestHandlerServlet());
    webfileRegistration.setLoadOnStartup(0);
    webfileRegistration.addMapping(new String[] { "/webfile/*" });
    Filter[] filters = getServletFilters();
    if (!ObjectUtils.isEmpty(filters)) {
        for (Filter filter : filters) {
            registerServletFilter(servletContext, filter, new String[] { "defaultServlet", "webfileServlet" });
        }
    }
}
Also used : Dynamic(javax.servlet.ServletRegistration.Dynamic) CharacterEncodingFilter(org.springframework.web.filter.CharacterEncodingFilter) Filter(javax.servlet.Filter) HttpRequestHandlerServlet(org.springframework.web.context.support.HttpRequestHandlerServlet)

Example 70 with Filter

use of javax.servlet.Filter in project PublicCMS-preview by sanluan.

the class ResourceInitializer method getServletFilters.

protected Filter[] getServletFilters() {
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding(DEFAULT_CHARSET_NAME);
    characterEncodingFilter.setForceEncoding(true);
    return new Filter[] { characterEncodingFilter };
}
Also used : CharacterEncodingFilter(org.springframework.web.filter.CharacterEncodingFilter) Filter(javax.servlet.Filter) CharacterEncodingFilter(org.springframework.web.filter.CharacterEncodingFilter)

Aggregations

Filter (javax.servlet.Filter)185 FilterChain (javax.servlet.FilterChain)67 Test (org.junit.Test)57 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServletRequest (javax.servlet.ServletRequest)43 ServletResponse (javax.servlet.ServletResponse)43 ServletException (javax.servlet.ServletException)28 FilterConfig (javax.servlet.FilterConfig)25 IOException (java.io.IOException)23 ServletContext (javax.servlet.ServletContext)20 Injector (com.google.inject.Injector)17 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)16 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Map (java.util.Map)9 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)9 ArrayList (java.util.ArrayList)8 Hashtable (java.util.Hashtable)8 DispatcherType (javax.servlet.DispatcherType)8