Methods Summary |
---|
public java.lang.String | authenticate()Autheticates a user with help from the EmployeeRegistryBean.
If the user can be authenticated, the "validUser" session
attribute is set to an instance of the EmployeeBean, to
serve as an authentication token in this application.
Cookies with the user name and password are set or reset
as specified by the "remember" request parameter.
String result = null;
FacesContext context = FacesContext.getCurrentInstance();
EmployeeBean emp = empReg.authenticate(username, password);
if (emp != null) {
Map sessionMap =
context.getExternalContext().getSessionMap();
sessionMap.put("validUser", emp);
setLoginCookies(context, remember, username, password);
// Next page is the originally requested URL or main
if (origURL != null && origURL.length() != 0) {
String newPath = context.getApplication().getViewHandler().
getActionURL(context, origURL);
try {
context.getExternalContext().redirect(newPath);
}
catch (IOException e) {}
context.responseComplete();
result = "newViewSet";
}
else {
result = "success";
}
}
else {
// Invalid login.
FacesMessage msg =
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Invalid username or password", null);
context.addMessage(null, msg);
result = "failure";
}
return result;
|
public java.lang.String | getOrigURL()Returns the originally requested URL, or null if none is set.
// Need to do this because the access control filter uses a
// different parameter name than the login form
if (origURL == null) {
FacesContext context = FacesContext.getCurrentInstance();
Map param =
context.getExternalContext().getRequestParameterMap();
String s = (String) param.get("origURL");
if (s != null) {
origURL = s;
}
}
return origURL;
|
public java.lang.String | getPassword()Returns the current password, or the value of a "password"
cookie if no username is set.
if (password == null) {
FacesContext context = FacesContext.getCurrentInstance();
Map cookies = context.getExternalContext().getRequestCookieMap();
Cookie c = (Cookie) cookies.get("password");
if (c != null) {
password = c.getValue();
}
}
return password;
|
public boolean | getRemember()Returns the "remember" property value, or "true" if this property
isn't set and a "password" cookie has a value.
if (!rememberSet) {
FacesContext context = FacesContext.getCurrentInstance();
Map cookies = context.getExternalContext().getRequestCookieMap();
Cookie c = (Cookie) cookies.get("password");
if (c != null) {
remember = true;
}
}
return remember;
|
public java.lang.String | getUsername()Returns the current username, or the value of a "username"
cookie if no username is set.
if (username == null) {
FacesContext context = FacesContext.getCurrentInstance();
Map cookies = context.getExternalContext().getRequestCookieMap();
Cookie c = (Cookie) cookies.get("username");
if (c != null) {
username = c.getValue();
}
}
return username;
|
private void | setLoginCookies(javax.faces.context.FacesContext context, boolean remember, java.lang.String username, java.lang.String password)Set or "delete" the login cookies, depending on the value of the
"remember" parameter.
HttpServletRequest request =
(HttpServletRequest) context.getExternalContext().getRequest();
HttpServletResponse response =
(HttpServletResponse) context.getExternalContext().getResponse();
Cookie usernameCookie = new Cookie("username", username);
Cookie passwordCookie = new Cookie("password", password);
// Cookie age in seconds: 30 days * 24 hours * 60 minutes * 60 seconds
int maxAge = 30 * 24 * 60 * 60;
if (!remember) {
// maxAge = 0 to delete the cookie
maxAge = 0;
}
usernameCookie.setMaxAge(maxAge);
passwordCookie.setMaxAge(maxAge);
usernameCookie.setPath(request.getContextPath());
passwordCookie.setPath(request.getContextPath());
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
|
public void | setOrigURL(java.lang.String origURL)Sets the originally requested URL.
this.origURL = origURL;
|
public void | setPassword(java.lang.String password)Sets the password.
this.password = password;
|
public void | setRegistry(EmployeeRegistryBean empReg)Sets the registry holding user information.
this.empReg = empReg;
|
public void | setRemember(boolean remember)Sets the "remember" property value.
this.remember = remember;
rememberSet = true;
|
public void | setUsername(java.lang.String username)Sets the username.
this.username = username;
|