In my project to since today I had a login + logout buttons that used JSF Form authentication and simple XHTML j_security_check login page (using primefaces for layout).
<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>
Access to page was restricted by following entries in 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>
However I'm asked to change login system, so it works in this way:
- Unauthorized user - login page
- Authorized user - portal functionality
- Authorized user with admin role - portal + admin dedicated pages
I modified web.xml to grant access to all roles:
<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>
but it still requires me to have at least one role, not being only logged-in. (and I had to change login page to html-only, cause I was getting redirects to jquery style files(?))
I would ask about best approach to do so, but it's best to have closed questions, soooo here is a bunch:
- 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?
- 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?
- Is it even possible to restrict application only to logged users using only application web.xml file?
Working on TomEE 1.7.3