java ee - How to configure the filter chain programmatically? -
is there way programmatic restriction of user
access particular [url]
based on said users
role
.
example in shiro.ini
file define :
[urls] ... 1. /app/**= user
which can achieved in code defining filter chain
so:
..... userfilter user = new userfilter();//create user filter user.setloginurl("login.xhtml"); filterchainmanager fcman = new defaultfilterchainmanager(); fcman.addfilter("user", user);//add filter filter chain fcman.createchain("/app/**", "user");//define url path expression filtername .....
my question here how achieve roles
have done in shiro.ini
file :
[urls] ... /api/admin/**= user, roles[admin] ...
for example:
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");
i author of book "pairing apache shiro , java ee 7" can grab free here.
in book, wrote shiro components programmatically, without need shiro.ini file.
i used cdi events produce 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; }
next, inject filterchainresolver in :
@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; } ... }
now, filterchainresolver applied, , go wanted.
Comments
Post a Comment