Search in sources :

Example 1 with AbstractShiroFilter

use of org.apache.shiro.web.servlet.AbstractShiroFilter in project dq-easy-cloud by dq-open-cloud.

the class EcAuthorityManager method doUpdateFilterChains.

/**
 * <p>
 * 执行更新过滤器链
 * </p>
 *
 * @param filterChainDefinitionMap
 * @return void
 * @author daiqi
 * @date 2018/6/27 13:00
 */
private void doUpdateFilterChains(Map<String, String> filterChainDefinitionMap) {
    AbstractShiroFilter shiroFilter;
    try {
        shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
    DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
    // 清空老的过滤器链
    manager.getFilterChains().clear();
    shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    // 重新构建生成
    Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
    for (Map.Entry<String, String> entry : chains.entrySet()) {
        String url = entry.getKey();
        String chainDefinition = entry.getValue().trim().replace(" ", "");
        manager.createChain(url, chainDefinition);
    }
}
Also used : DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)

Example 2 with AbstractShiroFilter

use of org.apache.shiro.web.servlet.AbstractShiroFilter in project dropwizard-shiro by silb.

the class ShiroBundle method createFilter.

/**
 * Create the Shiro filter. Overriding this method allows for complete customization of how Shiro is initialized.
 */
protected Filter createFilter(final T configuration) {
    ShiroConfiguration shiroConfig = narrow(configuration);
    final IniWebEnvironment shiroEnv = new IniWebEnvironment();
    shiroEnv.setConfigLocations(shiroConfig.iniConfigs());
    shiroEnv.init();
    AbstractShiroFilter shiroFilter = new AbstractShiroFilter() {

        @Override
        public void init() throws Exception {
            Collection<Realm> realms = createRealms(configuration);
            WebSecurityManager securityManager = realms.isEmpty() ? shiroEnv.getWebSecurityManager() : new DefaultWebSecurityManager(realms);
            setSecurityManager(securityManager);
            setFilterChainResolver(shiroEnv.getFilterChainResolver());
        }
    };
    return shiroFilter;
}
Also used : WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) DefaultWebSecurityManager(org.apache.shiro.web.mgt.DefaultWebSecurityManager) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) IniWebEnvironment(org.apache.shiro.web.env.IniWebEnvironment) Realm(org.apache.shiro.realm.Realm)

Example 3 with AbstractShiroFilter

use of org.apache.shiro.web.servlet.AbstractShiroFilter in project shiro by apache.

the class ShiroFilterFactoryBeanTest method testFilterDefinitionWithInit.

/**
 * Verifies fix for <a href="https://issues.apache.org/jira/browse/SHIRO-167">SHIRO-167</a>
 *
 * @throws Exception if there is any unexpected error
 */
@Test
public void testFilterDefinitionWithInit() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
    AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
    FilterConfig mockFilterConfig = createNiceMock(FilterConfig.class);
    ServletContext mockServletContext = createNiceMock(ServletContext.class);
    expect(mockFilterConfig.getServletContext()).andReturn(mockServletContext).anyTimes();
    HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
    expect(mockRequest.getContextPath()).andReturn("/").anyTimes();
    expect(mockRequest.getRequestURI()).andReturn("/").anyTimes();
    HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
    replay(mockFilterConfig);
    replay(mockServletContext);
    shiroFilter.init(mockFilterConfig);
    verify(mockServletContext);
    verify(mockFilterConfig);
    FilterChain filterChain = new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            assertNotNull(request.getSession());
            // this line asserts the fix for the user-reported issue:
            assertNotNull(request.getSession().getServletContext());
        }
    };
    replay(mockRequest);
    replay(mockResponse);
    shiroFilter.doFilter(mockRequest, mockResponse, filterChain);
    verify(mockResponse);
    verify(mockRequest);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 4 with AbstractShiroFilter

use of org.apache.shiro.web.servlet.AbstractShiroFilter in project shiro by apache.

the class ShiroFilterFactoryBeanTest method testFilterDefinition.

@Test
public void testFilterDefinition() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
    AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
    PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
    DefaultFilterChainManager fcManager = (DefaultFilterChainManager) resolver.getFilterChainManager();
    NamedFilterList chain = fcManager.getChain("/test");
    assertNotNull(chain);
    assertEquals(chain.size(), 2);
    Filter[] filters = new Filter[chain.size()];
    filters = chain.toArray(filters);
    assertTrue(filters[0] instanceof DummyFilter);
    assertTrue(filters[1] instanceof FormAuthenticationFilter);
}
Also used : DefaultFilterChainManager(org.apache.shiro.web.filter.mgt.DefaultFilterChainManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) FormAuthenticationFilter(org.apache.shiro.web.filter.authc.FormAuthenticationFilter) AbstractShiroFilter(org.apache.shiro.web.servlet.AbstractShiroFilter) NamedFilterList(org.apache.shiro.web.filter.mgt.NamedFilterList) PathMatchingFilterChainResolver(org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver) Test(org.junit.Test)

Aggregations

AbstractShiroFilter (org.apache.shiro.web.servlet.AbstractShiroFilter)4 DefaultFilterChainManager (org.apache.shiro.web.filter.mgt.DefaultFilterChainManager)2 PathMatchingFilterChainResolver (org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver)2 Test (org.junit.Test)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Realm (org.apache.shiro.realm.Realm)1 IniWebEnvironment (org.apache.shiro.web.env.IniWebEnvironment)1 FormAuthenticationFilter (org.apache.shiro.web.filter.authc.FormAuthenticationFilter)1 NamedFilterList (org.apache.shiro.web.filter.mgt.NamedFilterList)1 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)1 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)1