📄 loginaction.java
字号:
/* * LoginAction.java * * Created on July 3, 2003, 12:01 PM */package gov.nist.struts.webapp.login;import gov.nist.security.authentication.UserTag;import java.io.File;import java.util.*;import org.apache.log4j.Logger;import org.apache.struts.action.ActionError;import org.apache.struts.action.ActionErrors;import javax.servlet.*;/** * This class represents the login action. It will find the user in the database * and load his services' information in the session context * @author DERUELLE Jean */public class LoginAction extends org.apache.struts.action.Action { /** log4j logging*/ static Logger logger = Logger.getLogger(LoginAction.class); /** Creates a new instance of LoginAction */ public LoginAction() { } /** * @see org.apache.struts.action.Action#perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) */ public org.apache.struts.action.ActionForward perform(org.apache.struts.action.ActionMapping actionMapping, org.apache.struts.action.ActionForm actionForm, javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws java.io.IOException, javax.servlet.ServletException { //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); if (actionForm instanceof LoginForm) { LoginForm loginForm = (LoginForm) actionForm; //retrieve the user name String userName=loginForm.getUserName(); //retrieve the password String password=loginForm.getPassword(); //Identification UserTag user=this.login(userName,password); if(user==null){ ActionErrors errors = new ActionErrors(); errors.add("login", new ActionError("error.login.authenticate")); saveErrors(httpServletRequest, errors); return actionMapping.findForward("login.error"); } String group=user.getUserGroup(); //Put the user name in the session httpServletRequest.getSession().setAttribute("user",user); //Load the services of the user logger.info("User " + userName + " belonging to the group : "+group+" logged"); logger.info("Loading of all the services of the user " + userName); logger.info("Services of the user " + userName+" loaded"); //Get the upload directory name in the application context String uploadLocation=(String)applicationContext.getAttribute("uploadDirectory"); //Set the path to upload the file String uploadPath=uploadLocation.concat(group)+File.separatorChar+userName; //Put the upload path in the session httpServletRequest.getSession().setAttribute( "uploadPath", uploadPath+File.separatorChar); if(group.equals("admin")) return actionMapping.findForward("admin"); return actionMapping.findForward("monitor"); } //Should not happen return null; } /** * this method check the user database to see if the user exists * @param userName - identifier to retrieve the user in the database * @param password - password associated to the username to retrieve the user in the database * @return a UserTag object containing all the user information */ public UserTag login(String userName,String password){ //Get the application context ServletContext applicationContext=this.getServlet().getServletContext(); //Get the database of the users Vector users=(Vector)applicationContext.getAttribute("users"); if(users!=null){ for(int i=0;i<users.size();i++){ UserTag user=(UserTag)users.elementAt(i); if(user.getUserName().equals(userName) && user.getUserPassword().equals(password)) return user; } } return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -