1

我正在尝试在我的应用程序中创建一个登录名。我正在使用 PrimeFaces 和 Oracle Glassfish 3.1.2。我在 Glassfish 服务器上的文件领域内创建了一个用户。并使用表单选择身份验证。这是代码:

登录页面:

<?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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:head>
        <title>Test</title>
    </h:head>
    <h:body>    
            <p:panel header="Prihlásenie" style="width: 300px; margin-left: auto; margin-right: auto;">
                <form method="POST" action="j_security_check">
                    <h:panelGrid columns="2" id="logingrid" style="width: 100%;">

                        <h:outputLabel for="j_username" value="Meno:" />
                        <p:inputText id="j_username" required="true" label="j_username" style="width: 100%;"/>

                        <h:outputLabel for="j_password" value="Heslo:" />   
                        <p:password id="j_password" label="Heslo" required="true" style="width: 100%;"/>

                        <f:facet name="footer">
                            <h:commandButton type="submit" value="Prihlás" style="width: 100%"/>
                        </f:facet>
                    </h:panelGrid>
                </form>
            </p:panel>
    </h:body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.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>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/secure/temy.xhtml</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <display-name>Sec</display-name>
        <web-resource-collection>
            <web-resource-name>Secure</web-resource-name>
            <description/>
            <url-pattern>/faces/secure/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>PUT</http-method>
            <http-method>HEAD</http-method>
            <http-method>POST</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>spravcovia</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description/>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <security-constraint>
        <display-name>Pub</display-name>
        <web-resource-collection>
            <web-resource-name>Public</web-resource-name>
            <description/>
            <url-pattern>*.css</url-pattern>
            <url-pattern>*.jpg</url-pattern>
            <url-pattern>*.gif</url-pattern>
            <url-pattern>/error.xhtml</url-pattern>
            <url-pattern>/login.xhtml</url-pattern>
            <http-method>GET</http-method>
            <http-method>PUT</http-method>
            <http-method>HEAD</http-method>
            <http-method>POST</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>DELETE</http-method>
        </web-resource-collection>
        <user-data-constraint>
            <description/>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>dbrealm</realm-name>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/error.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description/>
        <role-name>spravcovia</role-name>
    </security-role>
</web-app>

glassfish-web.xml

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <security-role-mapping>
    <role-name>spravcovia</role-name>
    <group-name>spravcovia</group-name>
  </security-role-mapping>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
</glassfish-web-app>

事实是,当我不使用 Primefaces 标签或 jsf 标签进行输入时,只有这种输入,它工作正常:

Username: <input type='text' name='j_username' />
Password: <input type='password' name='j_password' />

我想如果通过阻止/ *我没有阻止使用primefaces,但我不知道如何启用它。

更新: 我已经更新了 web.xml 和 glassfish-web.xml 。这也是我的项目目录(NetBeans)。在 WEB-INF 中只有 glassfish-web.xml 和 web.xml:

项目目录结构

4

2 回答 2

4

您的问题是当用户未登录时您正在阻止所有资源,即使用户未登录,您也应该让 CSS、JavaScript... 等资源得到处理。为此,请在 web.xml 中添加此部分:

<security-constraint>
  <web-resource-collection>
    <web-resource-name>Public</web-resource-name>
    <url-pattern>*.css</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>/javax.faces.resource/*</url-pattern>
  </web-resource-collection>
</security-constraint>
于 2013-04-02T08:48:01.367 回答
0

也许这篇文章可以帮助你

使用 j_security_check 在 Java EE / JSF 中执行用户身份验证

看看 BalusC 的答案。祝你好运。

于 2013-07-13T19:48:22.470 回答