Managing credentials using ZK Sessions"

From Documentation
m
Line 1: Line 1:
 
{{ZKEssentialsPageHeader}}
 
{{ZKEssentialsPageHeader}}
  
One of the paradigms used within the essentials guide is the singleton managers which live within the session. The basic premise for storing singletons in the session is the fact that they are available anywhere at any time and user credential specific, when the session expires the login credentials are conveniently cleared.
+
One of the paradigms used within the essentials guide is the singleton managers which live within the session. The basic premise for storing singletons in the session is the fact that they are available anywhere at any time and are also user credential specific. When the session expires, the login credentials are conveniently cleared.
  
 
===The Credentials Manager===
 
===The Credentials Manager===
  
For managing credentials we create a singleton named <mp>UserCredentialManager</mp> which wraps a <mp>UserDAO</mp>. Additionally the <mp>UserCredentialManager</mp> will expose two <mp>getInstance</mp> methods, one which takes a <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc> and one that doesn’t. The basic concept is that when the instance is retrieved it will check the <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc> for an existing credential manager and if there isn’t one present it will create one.  
+
For managing credentials we create a singleton named <mp>UserCredentialManager</mp> which wraps a <mp>UserDAO</mp>. Besides, the <mp>UserCredentialManager</mp> will unviel two <mp>getInstance</mp> methods, one taking a <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc> and the other one does not. The basic concept is that when the instance is retrieved, it checks the <javadoc type="interface">org.zkoss.zk.ui.Session</javadoc> for an existing credential manager. lf there is no present, it creates one.  
  
 
The code below demonstrates the two <mp>getInstance</mp> methods along with the creation of the <mp>userDAO</mp> in the constructor.
 
The code below demonstrates the two <mp>getInstance</mp> methods along with the creation of the <mp>userDAO</mp> in the constructor.
Line 45: Line 45:
 
</syntax>
 
</syntax>
  
The manager is very standard exposing a login method which if successful sets the <mp>User</mp> object and a <mp>isAuthenticated</mp> method which checks to see whether the user is null and returns accordingly.
+
The manager is very standardized, exposing a login method. If successful, it sets the <mp>User</mp> object and a <mp>isAuthenticated</mp> method which checks to see whether the user is null and returns accordingly.
Having put this into place we can now make use of it in our controller to change the page flow of the application.
+
Once this is put into practice, we can now make use of it in our controller to change the page flow of the application.
  
 
===Redirecting the user depending on the UserCredentialManager===
 
===Redirecting the user depending on the UserCredentialManager===
  
If we think back to our login page and use case we have two places we need to check for a valid user. Once when the user navigates to the page and when the user presses the confirm button.  
+
If we think back to our login page and use case, we have two situations we need to check for a valid user: when users navigate to the page and the when users press the confirm button.  
If the user is authenticated then we need a way to redirect to the index page. This is handled using the <javadoc type="interface">org.zkoss.zk.ui.Execution</javadoc> class which provides information about the current execution, such as the request parameters.  
+
If the user is authenticated, we need a way to redirect to the index page. This should be dealt with the <javadoc type="interface">org.zkoss.zk.ui.Execution</javadoc> class which provides information about the current execution, such as the request parameters.  
  
A GenericForwardComposer contains an Execution object named execution which is accessible. If we need to access it outside of a <javadoc>org.zkoss.zk.ui.util.GenericForwardComposer</javadoc> we can retrieve the current execution by calling the <javadoc method="getCurrent()" class="false">org.zkoss.zk.ui.Executions</javadoc> method in the <javadoc>org.zkoss.zk.ui.Executions</javadoc> class as it is static.
+
A GenericForwardComposer contains an Execution object, execution, which is accessible. If we need to access it outside of a <javadoc>org.zkoss.zk.ui.util.GenericForwardComposer</javadoc> we can retrieve the current execution by calling the <javadoc method="getCurrent()" class="false">org.zkoss.zk.ui.Executions</javadoc> method in the <javadoc>org.zkoss.zk.ui.Executions</javadoc> class as it is static.
  
The <javadoc method="sendRedirect(java.lang.String)">org.zkoss.zk.ui.Executions</javadoc> class has a method named <javadoc>org.zkoss.zk.ui.Executions</javadoc> which will redirect the user to a page you specify, in this case “index.zul.”
+
The <javadoc method="sendRedirect(java.lang.String)">org.zkoss.zk.ui.Executions</javadoc> class has a method named <javadoc>org.zkoss.zk.ui.Executions</javadoc>, which redirects the user to a page you indicate. In this case, it is “index.zul.”
  
 
<syntax lang="java" high="2">
 
<syntax lang="java" high="2">
Line 63: Line 63:
 
</syntax>
 
</syntax>
  
This concludes the login topic, next we will deal with displaying information to the user using a <javadoc>org.zkoss.zul.Grid</javadoc> and <javadoc>org.zkoss.zul.Listbox</javadoc>.
+
This concludes the login topic, next we will deal with displaying information to users using a <javadoc>org.zkoss.zul.Grid</javadoc> and <javadoc>org.zkoss.zul.Listbox</javadoc>.
  
  
 
{{ZKEssentialsPageFooter}}
 
{{ZKEssentialsPageFooter}}

Revision as of 02:43, 23 June 2011

Stop.png This article is out of date, please refer to http://books.zkoss.org/zkessentials-book/master/ for more up to date information.


One of the paradigms used within the essentials guide is the singleton managers which live within the session. The basic premise for storing singletons in the session is the fact that they are available anywhere at any time and are also user credential specific. When the session expires, the login credentials are conveniently cleared.

The Credentials Manager

For managing credentials we create a singleton named UserCredentialManager which wraps a UserDAO. Besides, the UserCredentialManager will unviel two getInstance methods, one taking a Session and the other one does not. The basic concept is that when the instance is retrieved, it checks the Session for an existing credential manager. lf there is no present, it creates one.

The code below demonstrates the two getInstance methods along with the creation of the userDAO in the constructor.

<syntax lang="java" high="11,18"> public class UserCredentialManager {

private static final String KEY_USER_MODEL = UserCredentialManager.class.getName()+"_MODEL"; private UserDAO userDAO; private User user;

private UserCredentialManager(){ userDAO = new UserDAO(); }

public static UserCredentialManager getIntance(){ return getIntance(Sessions.getCurrent()); } /** * * @return */ public static UserCredentialManager getIntance(Session zkSession){ HttpSession httpSession = (HttpSession) zkSession.getNativeSession();


// Session session = Executions.getCurrent().getDesktop().getSession(); // Session session = Executions.getCurrent().getSession(); Session session = Sessions.getCurrent(); synchronized(zkSession){ UserCredentialManager userModel = (UserCredentialManager) zkSession.getAttribute(KEY_USER_MODEL); if(userModel==null){ zkSession.setAttribute(KEY_USER_MODEL, userModel = new UserCredentialManager()); } return userModel; } } } </syntax>

The manager is very standardized, exposing a login method. If successful, it sets the User object and a isAuthenticated method which checks to see whether the user is null and returns accordingly. Once this is put into practice, we can now make use of it in our controller to change the page flow of the application.

Redirecting the user depending on the UserCredentialManager

If we think back to our login page and use case, we have two situations we need to check for a valid user: when users navigate to the page and the when users press the confirm button. If the user is authenticated, we need a way to redirect to the index page. This should be dealt with the Execution class which provides information about the current execution, such as the request parameters.

A GenericForwardComposer contains an Execution object, execution, which is accessible. If we need to access it outside of a GenericForwardComposer we can retrieve the current execution by calling the getCurrent() method in the Executions class as it is static.

The Executions.sendRedirect(String) class has a method named Executions, which redirects the user to a page you indicate. In this case, it is “index.zul.”

<syntax lang="java" high="2"> if(UserCredentialManager.getIntance(session).isAuthenticated()){ execution.sendRedirect("index.zul"); } </syntax>

This concludes the login topic, next we will deal with displaying information to users using a Grid and Listbox.



Last Update : 2011/06/23

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.