用于webapp的Apache Shiro授权和身份验证配置

时间:2015-09-26 10:16:35

标签: apache java-ee web-applications login shiro

有一些配置问题。

我试图将Apache Shiro与mongo DB领域集成。

ini文件:

[main]
mongoDBRealm = realm.MongoRealm
securityManager.realms = $mongoDBRealm

# specify login page
shiro.loginUrl = /<ProjFolderNameInEclipse>/SuppliersLogin.html

# name of request parameter with username; if not present filter assumes 'username'
#authc.usernameParam = user

# name of request parameter with password; if not present filter assumes 'password'
#authc.passwordParam = pass

# does the user wish to be remembered?; if not present filter assumes 'rememberMe'
#authc.rememberMeParam = remember


# redirect after successful login
authc.successUrl  = /<ProjFolderNameInEclipse>/pass.html

[urls]
# enable authc filter for all application pages
/<ProjFolderNameInEclipse>/SuppliersLogin.html = authc

这个ini正在使用:

public static void main(String[] args) {
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("shiro.ini");
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
    Subject currentUser = SecurityUtils.getSubject();
    ..
    some logic
    ..
}

以上代码只是为了确保领域正常运行。

web.xml



 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <listener>
        <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>

    </listener>    
    <context-param>
        <param-name>shiroConfigLocations</param-name>
        <param-value>classpath:shiro.ini</param-value>
    </context-param>
    <filter>
        <filter-name>ShiroFilter</filter-name>
        <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>


    <filter-mapping>
        <filter-name>ShiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>   

</web-app>

现在我有一个位于WebContent文件夹内的简单登录页面: 使用以下表格:

<form method="POST" action="" name="loginform">
                                <header>
                                    Sign In
                                </header>

                                <fieldset>

                                    <section>
                                        <label class="label">User name</label>
                                        <label class="input"> <i class="icon-append fa fa-user"></i>
                                            <input type="text" name="username">
                                            <b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i> Please enter email address/username</b></label>
                                    </section>

                                    <section>
                                        <label class="label">Password</label>
                                        <label class="input"> <i class="icon-append fa fa-lock"></i>
                                            <input type="password" name="password">
                                            <b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i> Enter your password</b> </label>
                                        <div class="note">
                                            <a href="forgotpassword.html">Forgot password?</a>
                                        </div>
                                    </section>

                                    <section>
                                        <label class="checkbox">
                                            <input type="checkbox" name="remember" checked="">
                                            <i></i>Stay signed in</label>
                                    </section>
                                </fieldset>
                                <footer>
                                    <!-- <button type="submit" class="btn btn-primary" value="Sign In"> -->
                                    <button type="submit" class="btn btn-primary" value="Login" name="submit">
                                        Sign in
                                    </button>
                                </footer>
                            </form>

在点击提交btn时,页面旁边的任何内容都无法重新加载..

我做错了什么?

1 个答案:

答案 0 :(得分:2)

In my opinion, shiro doesn't support MongoDB out of the box. Please review the following posts, to find a possible solution. Please note that I'm not the author of the possible solutions:

As you describe the behavior of your web application, shiro can't find the realm you specified in your shiro.ini (mongoDBRealm = realm.MongoRealm), so it redirects you to the login page.