[OmniFaces utilities] The
getRemoteUser()
method returns the name of the logged-in user for container managed FORM
based authentication, if any.[OmniFaces utilities] The
isUserInRole()
method returns whether the currently logged-in user has the given role.Method Faces#getRemoteUser() - returns the name of the logged-in user for container managed FORM based authentication, if any
See also: Faces#getContext()
Method Faces#isUserInRole()- returns whether the currently logged-in user has the given role
See also: Faces#getContext()
Usage:The configuration of a FORM based authentication is specific to container. For example, if you are familiar with GlassFish 3/4, then you know that you must follow an entire process of configurations. Basically, you need to register a realm, declare the roles and groups (e.g. via <security-role-mapping>, <role-name> and <group-name>) and declare the security constrains (e.g. via <security-constraint>). At the end, configure the login, which may look like this (in web.xml/glassfish-web.xml):
<login-config>
<auth-method>FORM</auth-method>
<realm-name>my-realm</realm-name>
<form-login-config>
<form-login-page>/faces/login/login.xhtml</form-login-page>
<form-error-page>/faces/login/error.xhtml</form-error-page>
</form-login-config>
</login-config>
Finally, you
will write the form that it is used by users to login (in login/login.xhtml):
<form
action="j_security_check" method="POST">
<input id="j_username"
type="text" name="j_username"
placeholder="Username"/>
<input id="j_password"
type="password" name="j_password"
placeholder="Password"/>
</form>
In order to
login, an user need to type his credentials via this form. Now,
programmatically you find the name of the logged-in user (if any) via Faces#getRemoteUser():
String user =
Faces.getRemoteUser();
Moreover, if
you need to check if the currently logged-in user has the given role, then use Faces#isUserInRole():
...
<security-role-mapping>
<role-name>myRole</role-name>
<group-name>admin</group-name>
</security-role-mapping>
...
boolean isinrole =
Faces.isUserInRole("myRole");
if(isinrole){
// do something
}
Niciun comentariu :
Trimiteți un comentariu