Managing credentials using ZK Sessions"
m (correct highlight (via JWB)) |
|||
(13 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
+ | #REDIRECT [[ZK Essentials]] | ||
+ | |||
{{ZKEssentialsPageHeader}} | {{ZKEssentialsPageHeader}} | ||
+ | __TOC__ | ||
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 specific to user credentials. 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 specific to user credentials. When the session expires, the login credentials are conveniently cleared. | ||
− | ===The Credentials Manager=== | + | ===The Credentials Manager for User Authentication=== |
− | For managing credentials we create a singleton named <mp>UserCredentialManager</mp> which wraps a <mp>UserDAO</mp>. | + | For managing credentials we create a singleton named <mp>UserCredentialManager</mp> which wraps a <mp>UserDAO</mp>. Additionally, the <mp>UserCredentialManager</mp> will unveil two <mp>getInstance</mp> methods, one takes 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 a new 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. | ||
− | <syntax lang="java" | + | <syntax lang="java" highlight="11,15"> |
public class UserCredentialManager { | public class UserCredentialManager { | ||
Line 23: | Line 26: | ||
return getIntance(Sessions.getCurrent()); | return getIntance(Sessions.getCurrent()); | ||
} | } | ||
− | + | ||
− | + | public static UserCredentialManager getIntance(Session zkSession) { | |
− | + | ||
− | |||
− | public static UserCredentialManager getIntance(Session zkSession){ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
synchronized(zkSession){ | synchronized(zkSession){ | ||
UserCredentialManager userModel = (UserCredentialManager) zkSession.getAttribute(KEY_USER_MODEL); | UserCredentialManager userModel = (UserCredentialManager) zkSession.getAttribute(KEY_USER_MODEL); | ||
Line 44: | Line 39: | ||
} | } | ||
</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. Having put this into place 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 | ||
If we think back to our login page and the use case scenarios, we have two situations we need to check for a valid user: one is when the users navigate to the page and the other is when the users press the confirm button. | If we think back to our login page and the use case scenarios, we have two situations we need to check for a valid user: one is when the users navigate to the page and the other is when the 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 <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 | + | A SelectorComposer contains an Execution object, execution, which is accessible. If we need to access it outside of a <javadoc>org.zkoss.zk.ui.select.SelectorComposer</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 | + | The <javadoc>org.zkoss.zk.ui.Executions</javadoc> class has a method named <javadoc method="sendRedirect(java.lang.String)" class="false">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" | + | <syntax lang="java" highlight="2"> |
if(UserCredentialManager.getIntance(session).isAuthenticated()){ | if(UserCredentialManager.getIntance(session).isAuthenticated()){ | ||
− | + | Executions.sendRedirect("index.zul"); | |
} | } | ||
</syntax> | </syntax> |
Latest revision as of 12:33, 19 January 2022
Redirect to:
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 specific to user credentials. When the session expires, the login credentials are conveniently cleared.
The Credentials Manager for User Authentication
For managing credentials we create a singleton named UserCredentialManager which wraps a UserDAO. Additionally, the UserCredentialManager will unveil two getInstance methods, one takes 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 a new one.
The code below demonstrates the two getInstance methods along with the creation of the userDAO in the constructor.
<syntax lang="java" highlight="11,15"> 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()); }
public static UserCredentialManager getIntance(Session zkSession) {
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 standard exposing a login method which if successful sets the User object and a isAuthenticated 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.
Redirecting the User Depending on the UserCredentialManager
If we think back to our login page and the use case scenarios, we have two situations we need to check for a valid user: one is when the users navigate to the page and the other is when the 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 SelectorComposer contains an Execution object, execution, which is accessible. If we need to access it outside of a SelectorComposer we can retrieve the current execution by calling the getCurrent() method in the Executions class as it is static.
The Executions class has a method named sendRedirect(String), which redirects the user to a page you indicate. In this case, it is “index.zul.”
<syntax lang="java" highlight="2"> if(UserCredentialManager.getIntance(session).isAuthenticated()){ Executions.sendRedirect("index.zul"); } </syntax>
This concludes the login topic, and the next session we will see how to display information to users using a Grid and Listbox.