将应用程序仅限于 JSF 中的登录用户

Restricting application only to logged users in JSF

在我的项目中,从今天开始,我有一个使用 JSF 表单身份验证和简单 XHTML j_security_check 登录页面(使用 primefaces 进行布局)的登录 + 注销按钮。

<form action="j_security_check" method="post">
    <p:panelGrid id="loginContentPanel" columns="2">
        <p:outputLabel for="j_username" value="Login" />
        <p:inputText id="j_username" />
        <p:outputLabel for="j_password" value="Password" />
        <p:password id="j_password"></p:password>
        <f:facet name="footer">
            <div id="loginButtonCenter">
                <h:commandButton id="loginButton"
                    styleClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
                    value="Login" />
            </div>
        </f:facet>
    </p:panelGrid>
</form>

对页面的访问受到 web.xml

中的以下条目的限制
<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>User Auth</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/login.xhtml?s=err</form-error-page>
    </form-login-config>
</login-config>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>User Auth</web-resource-name>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

但是我被要求更改登录系统,所以它是这样工作的:

我修改了 web.xml 以授予对所有角色的访问权限:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>User Auth</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>*</role-name>
    </auth-constraint>
</security-constraint>

但它仍然要求我至少拥有一个角色,而不仅仅是登录。 (而且我不得不将登录页面更改为 html-only,因为我正在重定向到 jquery 样式文件(?))

我会询问这样做的最佳方法,但最好是封闭式问题,这里有很多问题:

正在使用 TomEE 1.7.3

but it still requires me to have at least one role, not being only logged-in.

只需添加代表 "user without role" 的角色,例如user。管理员显然拥有这两个角色。


(and I had to change login page to html-only, cause I was getting redirects to jquery style files(?))

/* 的 URL 模式适用于所有请求,包括 JSF 资源请求(来自 WAR/resourcesWAR/WEB-INF/lib/*.jar!/META-INF/resources 的 CSS/JS/image 文件)。要解决此问题,只需将 /javax.faces.resource/* URL 模式添加到公开允许的资源集中(即没有身份验证约束)。

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

另见 PrimeFaces CSS skin not showing in login page, also JavaScript undefined errors


For now my xhtml files are placed directly in WebContent directory, and in admin directory for admin pages. Should I move rest of the files (except login page) to for example user directory, so I can restrict only area of my project and use PF styles and images on login page?

没有必要。只需将 URL 模式 /* 约束到 user 角色,将 /admin/* 约束到 admin 角色。


I've read about filters, so I could just redirect users that are not logged to login page. Is it secure and cannot be interrupted? Does j_security_check "works" well with filters?

不要混用。过滤器仅用于自主开发的安全性。另见 How to handle authentication/authorization with users in a database? Moreover, container managed security runs far before first filter is hit, so you won't have any chance to do something in a filter anyway. See also a.o. .