0

我在这里使用 BalusC 的指南来学习如何使用 Apache Shiro。我在 Windows 7 x64 中使用 JSF 2.2、Neatbeans 7.4、Shiro 1.2.2、JavaEE 7、Glassfish 4。我的问题是,当我通过 Netbeans 尝试查看选项或尝试使用其 url 直接访问页面时,我的 login.xhtml 页面上的组件没有呈现,但是当我运行项目并自动加载它时工作(它是我的欢迎页面)。纯文本正常显示。

login.xhtml 页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h2>Login</h2>
    <h:form id="login">
        <h:panelGrid columns="3">
            Plain Text
            <h:outputLabel for="username" value="Username:" />
            <h:inputText id="username" value="#{login.username}" required="true">
                <f:ajax event="blur" render="m_username" />
            </h:inputText>
            <h:message id="m_username" for="username" />

            <h:outputLabel for="password" value="Password:" />
            <h:inputSecret id="password" value="#{login.password}" required="true">
                <f:ajax event="blur" render="m_password" />
            </h:inputSecret>
            <h:message id="m_password" for="password" />

            <h:outputLabel for="rememberMe" value="Remember Me:" />
            <h:selectBooleanCheckbox id="rememberMe" value="#{login.remember}" />

            <h:commandButton value="Login" action="#{login.submit}" >
                <f:ajax execute="@form" render="@form" />
            </h:commandButton>
            <h:messages globalOnly="true" layout="table" />
        </h:panelGrid>
    </h:form>
</h:body>

Login.java bean:

package test;

import java.io.IOException;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;

@Named
@RequestScoped
public class Login {

public static final String HOME_URL = "app/index.xhtml";

private String username;
private String password;
private boolean remember;

public void submit() throws IOException {
    try {
        SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, remember));
        SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(Faces.getRequest());
        Faces.redirect(savedRequest != null ? savedRequest.getRequestUrl() : HOME_URL);
    }
    catch (AuthenticationException e) {
        Messages.addGlobalError("Unknown user, please try again");
        e.printStackTrace(); // TODO: logger.
    }
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public boolean isRemember() {
    return remember;
}

public void setRemember(boolean remember) {
    this.remember = remember;
}


}

我也收到此错误

A managed bean with a public field (“HOME_URL”) should not declares any scope other than @Dependent

即使它似乎工作......

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<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>    
<welcome-file-list>
    <welcome-file>login.xhtml</welcome-file>
</welcome-file-list>

shiro.ini:

[main]
authc.loginUrl = /login.xhtml
authc.usernameParam = login:username
authc.passwordParam = login:password
authc.rememberMeParam = login:rememberMe
user.loginUrl = /login.xhtml

[users]
admin = password

[urls]
/login.xhtml = authc
/app/** = user

我得到的警告/错误(glassgish):

WARNING:   The web application [unknown] registered the JDBC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
SEVERE:   Unable to load annotated class: model.LoginBean, reason: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
WARNING:   WELD-001519 An InjectionTarget implementation is created for an abstract class org.omnifaces.eventlistener.DefaultServletContextListener. It will not be possible to produce instances of this type!
WARNING:   WELD-001519 An InjectionTarget implementation is created for an abstract class org.omnifaces.filter.HttpFilter. It will not be possible to produce instances of this type!
WARNING:   Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
WARNING:   Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
WARNING:   WELD-001529 An InjectionTarget implementation is created for a class org.omnifaces.application.OmniApplicationFactory which does not have any appropriate constructor.
WARNING:   WELD-001529 An InjectionTarget implementation is created for a class org.primefaces.context.PrimeFacesContextFactory which does not have any appropriate constructor.
WARNING:   WELD-001529 An InjectionTarget implementation is created for a class org.omnifaces.context.OmniPartialViewContextFactory which does not have any appropriate constructor.
WARNING:   WELD-001529 An InjectionTarget implementation is created for a class org.primefaces.context.PrimePartialViewContextFactory which does not have any appropriate constructor.
SEVERE:   [main] INFO org.apache.shiro.web.env.EnvironmentLoader - Starting Shiro environment initialization.
SEVERE:   [main] INFO org.apache.shiro.web.env.EnvironmentLoader - Shiro environment initialized in 361 ms.

我有两个网页:

/login.xhtml
/app/index.xhtml

有什么建议么?(我是菜鸟,所以请温柔!:P)

4

3 回答 3

1

我今天遇到了完全相同的问题。BalusC 在不同主题中找到了答案(或至少是一个提示)。

在我的情况下,页面与 FacesServlet 的 url-pattern 不匹配。我所要做的就是改变 web.xml 中的模式:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
于 2013-12-29T01:23:21.697 回答
0

签出此日志行

SEVERE:   Unable to load annotated class: model.LoginBean, reason: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Shiro 依赖 slf4j 进行日志记录,而您缺少 slf4j 依赖项。你可以在这里下载罐子:http ://www.slf4j.org/download.html

我建议您将以下 jar 添加到您的类路径中:

  • slf4j-api-1.7.5.jar
  • slf4j-simple-1.7.5.jar

如果您想使用其他形式的日志记录(log4j、commons logging、java logging、logback),您需要将简单的 jar 替换为以下之一:

  • slf4j-log4j12-1.7.5.jar
  • slf4j-jcl-1.7.5.jar
  • slf4j-jdk14-1.7.5.jar
  • logback-classic-1.0.13.jar(需要 logback-core-1.0.13.jar)

有关 slf4j 的更多详细信息,请查看手册:http ://www.slf4j.org/manual.html

于 2013-11-08T06:31:00.957 回答
-1

公共静态最终字符串 HOME_URL = "app/index.xhtml";

这就是错误所在,我想您现在应该知道您应该更喜欢私有变量声明而不是公共声明变量。所以改用这个。干杯

私有静态最终字符串 HOME_URL = "app/index.xhtml";

于 2015-03-02T03:44:40.657 回答