2

Shiroで遊んでいて、ShiroWebModuleを介して役割を構成したいと思います。

addFilterChain("/**", AUTHC);

これは機能し、ログインページが表示され、ログインできます。

だが

addFilterChain("/test/**", AUTHC, config(ROLES, "test")); //that does not seem to be the right way

ではない。ゲストとして/guest/**と/test/**にアクセスできます。

私のモジュール:

public class HelloMavenShiroModule extends 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");
    }
}

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 = *

まだリリースされていないことは知っていますが、おそらくすでに可能であり、私にはわかりません。

4

1 に答える 1

4

すべてをクリーンアップして再試行した後、私は自分自身を発見しました。

addFilterChain("/test/**", AUTHC, config(ROLES, "test"));

すでにそれを行う正しい方法でした。問題の原因はわかりませんが、「test」ロールを持つユーザーのみが/test/の下のリソースにアクセスできるようになりました。それはまさに私が欲しかったものです。

私は今幸せです!:)

于 2011-09-29T15:52:01.447 に答える