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);
}
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;
}
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 };
}
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" });
}
}
}
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 };
}
Aggregations