How to configure roles with Apache Shiro 1.2-SNAPSHOT and Guice?
I'm playing around with Shiro and would like to configure roles via the ShiroWebModule.
addFilterChain("/**", AUTHC);
this works, my loginpage is shown and I can login.
But
addFilterChain("/test/**", AUTHC, config(ROLES, "test")); //that does not seem to be the right way
does not. I can access /guest/** and /test/** as guest.
My Module:
public class HelloMavenShiroModule ext开发者_如何学Cends ShiroWebModule {
HelloMavenShiroModule(ServletContext sc) {
super(sc);
}
protected void configureShiroWeb() {
try {
bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
} catch (NoSuchMethodException e) {
throw new RuntimeException("Code Problem.", e);
}
addFilterChain("/**", AUTHC);
addFilterChain("/guest/**", AUTHC, config(ROLES, "guest"));
addFilterChain("/test/**", AUTHC, config(ROLES, "[test]"));
//addFilterChain("/**", AUTHC);
bindConstant().annotatedWith(Names.named("shiro.loginUrl")).to("/account/login.jsp");
bindConstant().annotatedWith(Names.named("shiro.globalSessionTimeout")).to(30000L);
bindConstant().annotatedWith(Names.named("shiro.usernameParam")).to("user");
bindConstant().annotatedWith(Names.named("shiro.passwordParam")).to("pass");
bindConstant().annotatedWith(Names.named("shiro.rememberMeParam")).to("remember");
bindConstant().annotatedWith(Names.named("shiro.successUrl")).to("/index.html");
bindConstant().annotatedWith(Names.named("shiro.failureKeyAttribute")).to("helloMavenLoginFailure");
bindConstant().annotatedWith(Names.named("shiro.unauthorizedUrl")).to("/account/denied.jsp");
bind(AuthenticationFilter.class).to(VerboseFormAuthenticationFilter.class);
bind(CredentialsMatcher.class).to(HashedCredentialsMatcher.class);
bind(HashedCredentialsMatcher.class);
bindConstant().annotatedWith(Names.named("shiro.hashAlgorithmName")).to(Sha256Hash.ALGORITHM_NAME);
}
@Provides
Ini loadShiroIni() {
return Ini.fromResourcePath("classpath:shiro.ini");
}
}
The shiro.ini:
# -----------------------------------------------------------------------------
# Users and their (optional) assigned roles
# username = password, role1, role2, ..., roleN
# -----------------------------------------------------------------------------
[users]
root = 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b, admin
guest = 84983c60f7daadc1cb8698621f802c0d9f9a3c3c295c810748fb048115c186ec, guest
# -----------------------------------------------------------------------------
# Roles with assigned permissions
# roleName = perm1, perm2, ..., permN
# -----------------------------------------------------------------------------
[roles]
admin = *
I know it's not released yet, but maybe it's possible already and I just cannot find out.
I've found out myself after cleaning up everything and trying again it turns out that:
addFilterChain("/test/**", AUTHC, config(ROLES, "test"));
was already the right way to do it. I don't know what caused the problem, but now only users with the "test" role are allowed to access resources under /test/ ! Thats exactly what I wanted.
I'm happy now! :)
精彩评论