Question

I am using BalusC's guide from here in order to learn how to use Apache Shiro. I am using JSF 2.2, Neatbeans 7.4, Shiro 1.2.2, JavaEE 7, Glassfish 4 in Windows 7 x64. My problem is that the components on my login.xhtml page are not rendered when I try the view option through Netbeans or when I try to directly access the page with its url, but works when I run the projects and loads it automatically (it is my welcome page). Plain text is shown normally.

The login.xhtml page:

<?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>

The 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;
}


}

Also I am getting this error

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

even though it seems to work...

The 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>

The 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

The warnings/errors that I get (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.

I have two web pages:

/login.xhtml
/app/index.xhtml

Any suggestions? (I am a noob so please be gentle! :P )

Was it helpful?

Solution

I had exactly same problem today. Found answer (or at least a tip) by BalusC in different topic.

In my case page didn't match url-pattern of FacesServlet. All I had to do is change pattern in web.xml:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

OTHER TIPS

Checkout this log row

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

Shiro is depending on slf4j for logging and you are missing an slf4j dependency. You can download the jars here: http://www.slf4j.org/download.html

I suggest you add the following jars to your classpath:

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

If you want to use other forms of logging (log4j, commons logging, java logging, logback), you need to replace the simple jar with one of these:

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

For more detail on slf4j, check out the manual: http://www.slf4j.org/manual.html

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

That's where the error is, I suppose you should know by now that you should prefer private to public declaration of variables. So use this instead. Cheers

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top