1

プログラムによるフォームベースの認証を実行しようとしていますが、

私のweb.xml:

          <web-app>

            <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>*.jsf</url-pattern>
            </servlet-mapping>

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

            <welcome-file-list>
                <welcome-file>confirmauthentication.xhtml</welcome-file>
            </welcome-file-list>

            <session-config>
                 <session-timeout>10</session-timeout>
            </session-config>

            <security-constraint>
                <display-name>Authentication Ex Login</display-name>
                <web-resource-collection>
                    <web-resource-name>SecuredArea</web-resource-name>
                    <url-pattern>/*</url-pattern>
                    <http-method>GET</http-method>
                    <http-method>POST</http-method>
                </web-resource-collection>
                <auth-constraint>
                    <description/>
                    <role-name>*</role-name>
                </auth-constraint>  
            </security-constraint>

            <login-config>
                <auth-method>FORM</auth-method>
                <realm-name>mysqldomain</realm-name>
                <form-login-config>
                <form-login-page>/authentication.xhtml</form-login-page>
                <form-error-page>/error.xhtml</form-error-page>
                </form-login-config>
            </login-config>

            <security-role>
                <description/>
                <role-name>*</role-name>
            </security-role>
                </web-app>

authentication.xhtmlと呼ばれる私のjsfページ:

            <h:form>
                <h:panelGrid border="0" columns="2"> 
                   <h:outputText value="Username"/>
                   <h:inputText value="#{beanJSF.userName}" required="true" />

                   <h:outputText value="Password"/>
                  <h:inputSecret value="#{beanJSF.password}" required="true" />

                   <h:commandButton value="Log in" action="#{beanJSF.submit}">
                         <f:ajax execute="@form" render="@form" />
                   </h:commandButton>
                   <h:messages />    
                </h:panelGrid>
              </h:form>

「ログイン」ボタンを押しても送信メソッドが呼び出されず、理由がわからないようです。ボタンを押してもserver.logに何も表示されません(このメッセージは「(「ログイン方法を使用しています!!!!!!!! ...」)と表示されません)。

私のManagedBean:

               @URLMappings(mappings={
                @URLMapping(id="success", pattern = "/authentication/", viewId =    "/confirmauthentication.jsf")})
                public class BeanJSF implements Serializable  {

            private String password;
            private String userName;
                // User is the Entity
                private User loggedUser;



            @EJB
            UserEJB services;


                public String submit() throws IOException {
            System.out.println("I am in the login method!!!!!!!! " + getUserName()+ "  " + getPassword());   

                FacesContext context = FacesContext.getCurrentInstance();
                ExternalContext externalContext = context.getExternalContext();
                HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

                try {
                         request.login(userName, password);


               User user = services.authenticationUser(userName, password);  
                    this.setLoggedUser(user);
                    return "home?faces-redirect-true";

                } catch (ServletException e) {
                    // Handle unknown username/password in request.login().
                    context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Unknown login", null));
                    return null;
                }
            }

            public void logout() throws IOException {
                ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                externalContext.invalidateSession();
                externalContext.redirect(externalContext.getRequestContextPath() + "/authentication.xhtml");
            }

               //+ setters and getters for username, password and loggedUser
        }

そして私のEJBは次のとおりです。

                @Stateless
                public class UserEJB{


                @PersistenceContext(unitName = "PyPersistenceUnit")
                private EntityManager entityManager;

                 public UserEJB(){}

                @TransactionAttribute(TransactionAttributeType.REQUIRED)
                public User authenticationUser(String userName, String password){


                try{
                 User user = entityManager.createNamedQuery(User.FIND_USER,User.class). setParameter("userName", userName).setParameter("password", password).getSingleResult();
                 return user;
            }
            catch(NonUniqueResultException ex){
                ex.printStackTrace();
                return null;
            }
            catch(NoResultException ex){
                ex.printStackTrace();
                return null;
            }
                }
4

1 に答える 1

3

あなたのBeanはJSFによって管理されていないため、JSFはどこでもBeanを見つけることができません。

JSFBean管理アノテーションをクラスに追加します。

@ManagedBean
@RequestScoped
public class BeanJSF implements Serializable {
    // ...
}
于 2013-02-04T14:42:01.553 に答える