1

我正在尝试做这样的事情:https ://stackoverflow.com/a/2207147/494826

<security-constraint>
        <display-name>Amministrazione</display-name>
        <web-resource-collection>
            <web-resource-name>wrcollAdmin</web-resource-name>
            <description/>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>admin</role-name>
            <role-name>guest</role-name>
        </auth-constraint>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Risorse</web-resource-name>
            <url-pattern>/javax.faces.resource/*</url-pattern>
        </web-resource-collection>
        <!-- No Auth Contraint! -->
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.htm</form-login-page>
            <form-error-page>/loginError.htm</form-error-page>
        </form-login-config>
    </login-config>  

我的登录 JSF 包含:

<h:form id="loginForm">                 
    <div class="inputlabel">
        <h:outputLabel for="j_username" value="Utente:"/>
    </div>
    <div>
        <h:inputText value="#{loginController.username}" id="j_username" size="25" />
    </div>
    <div class="inputlabel">
        <h:outputLabel for="j_password" value="Password:"/>
    </div>
    <div>
        <h:inputText value="#{loginController.password}" id="j_password" size="25" />
    </div>
    <div>
        <h:commandButton action="#{loginController.login}" value="LOGIN" />
    </div>                  
</h:form>
<h:messages styleClass="errors" />
    ...

这是登录方法:

public void login() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    try {
        log4jLogger.info("LOGIN");
        request.login(username, password);            
        externalContext.redirect("/");
    } catch (ServletException e) {
        log4jLogger.info("DENIED");
        context.addMessage(null, new FacesMessage("Accesso Negato"));        
    }
}

这不起作用,因为表单从不调用该login()方法。为什么?我想它的行为是因为 web.xml 安全配置。也许它将 loginController(命名@ViewScopedbean)视为受保护的资源?

第二个问题更笼统。这是实现程序安全的正确方法吗?

4

1 回答 1

1

由于登录页面本身也被安全约束限制所覆盖,并且登录不是由 处理的j_security_check,因此您还需要将登录页面本身添加到允许的资源集合中。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Risorse</web-resource-name>
        <url-pattern>/javax.faces.resource/*</url-pattern>
        <url-pattern>/login.htm</url-pattern>
    </web-resource-collection>
    <!-- No Auth Contraint! -->
</security-constraint>
于 2013-01-11T18:55:52.787 回答