My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

vineri, 10 aprilie 2015

[OmniFaces utilities (2.0)] Get/check the currently logged-in user


[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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors