我正在使用来自here的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
我得到的警告/错误(玻璃):
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)
答案 0 :(得分:1)
今天我遇到了同样的问题。 BalusC在不同主题中找到答案(或至少是一个提示)。
在我的案例中,页面与FacesServlet的url-pattern不匹配。我所要做的就是在web.xml中更改模式:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
答案 1 :(得分: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添加到你的类路径中:
如果要使用其他形式的日志记录(log4j,commons日志记录,java日志记录,logback),则需要使用以下方法之一替换简单jar:
有关slf4j的更多详细信息,请查看手册:http://www.slf4j.org/manual.html
答案 2 :(得分:-1)
public static final String HOME_URL =&#34; app / index.xhtml&#34;;
错误的地方,我想你现在应该知道你应该更喜欢私人公开声明变量。所以请改用它。 干杯
private static final String HOME_URL =&#34; app / index.xhtml&#34 ;;