代码之家  ›  专栏  ›  技术社区  ›  Daniel Szalay

OmniFaces无扩展URL和登录重定向

  •  3
  • Daniel Szalay  · 技术社区  · 11 年前

    我已将应用程序配置为使用 OmniFaces's Extensionless URLs 功能,但现在我在 web.xml ,无扩展请求不会被 <security-constraint> .

    web.xml

    <!-- JSF configuration -->
    
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <!-- OmniFaces configuration -->
    
    <context-param>
        <param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
        <param-value>/*.xhtml</param-value>
    </context-param>
    
    <!-- Servlets and filters. -->
    
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    
    <!-- Welcome files, error pages and mime types. -->
    
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    
    <!-- Security constraints -->
    
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Allowed resources</web-resource-name>
            <url-pattern>/javax.faces.resource/*</url-pattern>
        </web-resource-collection>
    </security-constraint>
    <security-constraint>
        <display-name>SSL transport</display-name>
        <web-resource-collection>
            <web-resource-name>Secure Area</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>ADMINISTRATOR</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    
    <!-- Security roles -->
    
    <security-role>
        <role-name>ADMINISTRATOR</role-name>
    </security-role>
    
    <!-- Login config -->
    
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>myRealm</realm-name>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    

    登录.xhtml

    <h:form>
        <h:panelGrid columns="1">
            <h:outputText value="Username:" />
            <h:inputText id="username" required="true"
                value="#{appSession.loginUsername}"
                requiredMessage="Username is required" />
            <h:message for="username" />
            <hr />
    
            <h:outputText value="Password:" />
            <h:inputSecret id="password" required="true"
                value="#{appSession.loginPassword}"
                requiredMessage="Password is required" />
            <h:message for="password" />
    
            <h:commandButton value="Login" action="#{appSession.login}" />
        </h:panelGrid>
    
        <h:messages globalOnly="true" showDetail="false" />
    </h:form>
    

    应用会话.java

    @SessionScoped
    @ManagedBean
    public class AppSession {
    
        private String loginUsername;
        private String loginPassword;
    
        public AppSession() {   }
    
        public String login() {
            try {
                Faces.login(loginUsername, loginPassword);
    
                return "index.xhtml?faces-redirect=true";
            } catch (ServletException e) {
                e.printStackTrace();
    
                return "login.xhtml";
            }
        }
    
        public void logout() throws IOException {
            Faces.invalidateSession();
            Faces.redirect("index.xhtml");
        }
    
        //Getters and setters
    
    }
    

    所以,如果我浏览到 index.xhtml ,它会被正确重定向到 login 。但是,如果我浏览到 index ,没有重定向,允许浏览器从 指数 。我知道这正是 web.xml 通过 <url-pattern>*.xhtml</url-pattern> ,但如何配置应用程序,以便无扩展URL也受到登录限制?

    如果我用 <url-pattern>/*</url-pattern> 在下面 <安全约束> ,它被成功重定向,但我的登录表单在这种情况下不起作用。我必须使用JSF进行编程登录,因为我想重用Web服务的凭据。有什么办法让我得到一个有效的配置吗?

    我在GlassFish 4.1上使用OmniFaces 2.0和Mojarra 2.2.7。

    1 回复  |  直到 11 年前
        1
  •  2
  •   Daniel Szalay    11 年前

    如果我添加 login 和 login.xhtml 到允许的资源:

        <web-resource-collection>
            <web-resource-name>Allowed resources</web-resource-name>
            <url-pattern>/javax.faces.resource/*</url-pattern>
            <url-pattern>/login.xhtml</url-pattern>
            <url-pattern>/login</url-pattern>
        </web-resource-collection>
    
    推荐文章