【问题标题】:Apache Shiro + Authentication issuesApache Shiro + 身份验证问题
【发布时间】:2016-04-02 11:08:13
【问题描述】:

我正在为我的 Web 应用程序使用 Apache Shiro,但我无法让它按预期工作。

我需要的是 Shiro 框架的授权部分,但我无法遵循这些指南中的任何一个,因为它们都是不同的,我只是无法让它在我的应用程序中工作。

以下是我想使用 Shiro 框架的目的:

  • 将现有的 login.jsp 定义为我的登录页面
  • 定义登录尝试成功时显示的 *.jsp 页面
  • 当登录不成功时,用户会停留在 login.jsp,但会显示一条关于登录尝试失败的错误消息
  • 当用户未登录时,除 login.jsp 之外的所有其他 *.jsp 页面都不应访问

现在我的应用程序是这样做的:

  • 登录表单action参数调用login.java(servlet)
  • 登录成功 -> 显示portal.jsp页面
  • 页面 .../portal.jsp 可以在不登录的情况下调用 -> 这在我的应用程序的最终版本中应该是不可能的

到目前为止,我发现了以下几点:

shiro.ini:

[main]
# define login page
authc.loginUrl = /SSP/login.jsp

# name of request parameter with username;
authc.usernameParam = username

# name of request parameter with password;
authc.passwordParam = password

# redirect after successful login
authc.successUrl  = /SSP/portal.jsp

[urls]
# enable authc filter for all application pages
/SSP/**=authc

我的 web.xml 的 shiro 部分如下所示:

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<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>

我的 pom.xml 的 shiro 部分:

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.2.4</version>
</dependency>

我得到的错误:

java.lang.IllegalArgumentException: Configuration error. 
Specified object [authc] with property [loginUrl] without first defining that object's class.
Please first specify the class property first, e.g. myObject = fully_qualified_class_name and then define additional properties.

编辑:

似乎 shiro.ini 中的这一行成功了:

authc = org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter

但现在我遇到了问题,应用程序没有使用我自己的登录类

login.java:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String url = "/login.jsp";

    // Get Login credentials from Login form
    username = request.getParameter("username");
    password = request.getParameter("password");

    //SecurityManager securityManager = Startup.getSecurityManager();

    //2. Get the current Subject:
    Subject currentUser = SecurityUtils.getSubject();

    //3. Login:
    if (!currentUser.isAuthenticated()) {
        // create UsernamePasswordToken
        UsernamePasswordToken token = new UsernamePasswordToken("cn=" + username + ",ou=People,dc=maxcrc,dc=com", password);
        try {
            currentUser.login(token);

            token.clear();
            url = "/portal.jsp";

            System.out.println("User [" + currentUser.getPrincipal() +"] logged succesfully");

            //4. Create User Session
            Session session = currentUser.getSession();

            // get user_id
            user_id = get_users_id(username);

            // create new object of User class 
            User new_user = new User(user_id, username);

            // Set HTTP Session Parameters
            session.setAttribute("user", username);
            session.setAttribute("user_id", user_id);
            session.setAttribute("obj_user", new_user);
            session.setAttribute("currentUser", currentUser);

        } catch (UnknownAccountException uae) {
            System.out.println("There is no user with username of " + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
        } catch (LockedAccountException lae) {
            System.out.println("The account for username " + token.getPrincipal() + " is locked.  " + "Please contact your administrator to unlock it.");
        }
        // ... catch more exceptions here (maybe custom ones specific to your application?
        catch (AuthenticationException ae) {
            System.out.println("ERROR: " + ae);
        }
        // Done, redirect User to applications main page
        request.getRequestDispatcher(url).forward(request, response);
    } 
}

如何使用我自己的类(参见上面的 login.java sn-p)进行身份验证?

编辑结束

谁能举例说明如何:

  • 启用授权
  • 成功登录后启用页面重定向
  • 启用停留在登录页面,但登录尝试失败后显示错误消息
  • 只有在用户登录时才能访问应用程序的所有其他页面

【问题讨论】:

    标签: java jsp servlets shiro


    【解决方案1】:

    我最终使用了 AuthenticatingFilter 的代码并创建了我自己的过滤器,所以我可以写 authc = com.mycompany.ssp.my_own_authFilter 不知道它是否应该是这样,但它现在似乎可以工作

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 2017-06-28
      • 2014-12-20
      • 2016-10-29
      • 2014-08-31
      • 2015-05-17
      • 2013-01-30
      • 2014-07-09
      • 2014-12-15
      相关资源
      最近更新 更多