如何以编程方式配置过滤器链?

时间:2017-08-15 10:48:45

标签: java-ee shiro java-ee-7

是否有办法根据上述user [URL]users role shiro.ini的特定 [urls] ... 1. /app/**= user 访问进行编程限制。

例如,在filter chain文件中,您可以定义:

 .....

 UserFilter user = new UserFilter();//create user filter
 user.setLoginUrl("login.xhtml");


FilterChainManager fcMan = new DefaultFilterChainManager();
fcMan.addFilter("user", user);//add filter to filter chain
fcMan.createChain("/app/**", "user");//define url path expression for filterName

.....

可以在代码中实现定义roles如下:

shiro.ini

我的问题是如何使用[urls] ... /api/admin/**= user, roles[admin] ... 实现这一点,就像我在RolesAuthorizationFilter adminRole = new RolesAuthorizationFilter();//eg create role filter adminRole.setLoginUrl("login.xhtml"); UserFilter user = new UserFilter();//create user filter user.setLoginUrl("login.xhtml"); FilterChainManager fcMan = new DefaultFilterChainManager(); fcMan.addFilter("user", user); fcMan.addFilter("admin", adminRole); fcMan.createChain("/api/admin/**", "admin"); 文件中所做的那样:

string.format

例如:

def foo(var1):
    #Setup cx_Oracle and connect to DB
    sql = 'SELECT value FROM table WHERE ref = {}'.format(var1)
    c.execute(sql)

1 个答案:

答案 0 :(得分:4)

我是“配对Apache Shiro和Java EE 7 ”一书的作者,您可以免费获取here

enter image description here

在本书中,我以编程方式编写了所有Shiro组件,而不需要 shiro.ini 文件。

我使用CDI事件生成 FilterChainResolver

@Produces
public FilterChainResolver getFilterChainResolver() {
    FilterChainResolver filterChainResolver = null;
    if (filterChainResolver == null) {
        FormAuthenticationFilter authc = new FormAuthenticationFilter();
        AnonymousFilter anon = new AnonymousFilter();
        UserFilter user = new UserFilter();

        authc.setLoginUrl(WebPages.LOGIN_URL);
        user.setLoginUrl(WebPages.LOGIN_URL);

        FilterChainManager fcMan = new DefaultFilterChainManager();
        fcMan.addFilter("authc", authc);
        fcMan.addFilter("anon", anon);
        fcMan.addFilter("user", user);

        fcMan.createChain("/index.html", "anon");
        fcMan.createChain("/css/**", "anon");
        fcMan.createChain("/api/**", "anon");
        fcMan.createChain(WebPages.LOGIN_URL, "authc");
        fcMan.createChain("/**", "user");

        PathMatchingFilterChainResolver resolver = new PathMatchingFilterChainResolver();
        resolver.setFilterChainManager(fcMan);
        filterChainResolver = resolver;
    }
    return filterChainResolver;
}

接下来,我们在

中注入 FilterChainResolver
@WebListener
public class ShiroListener extends EnvironmentLoaderListener {

    @Inject
    WebSecurityManager securityManager;

    @Inject
    FilterChainResolver filterChainResolver;

    @Override
    protected WebEnvironment createEnvironment(ServletContext sc) {
        DefaultWebEnvironment webEnvironment = (DefaultWebEnvironment) super.createEnvironment(sc);

        webEnvironment.setSecurityManager(securityManager);
        webEnvironment.setFilterChainResolver(filterChainResolver);

        return webEnvironment;
    }
    ...
}

现在,应用了FilterChainResolver,一切都会按照需要进行。