Analysis of user logon examples and user logon examples
Multi-Point Logon
The project uses SSM (Spring SpringMVC Mybatis );
The process for logging on to the page is as follows:
Welcome Page for project startup: index. jsp
Direct redirect: <% response. sendRedirect ("/login/forwardLogin") %>
> Request to the ControllerForwardLogin ()Method:
1 @ RequestMapping ("/forwardLogin") 2 public ModelAndView forwardLogin (HttpServletRequest request) {3 ModelAndView mav = new ModelAndView ("login "); // mav Model View parser definition return address 4 String msg = request. getParameter ("res_msg") = null? "": Request. getParameter ("res_msg"); // key-value pairs prepared for the error message are obtained and set to 5 mav In the mav object. addObject ("res_msg", msg); 6 return mav; 7}
> Springmvc is parsed to login. jsp.
Information Display label: <p class = "login-error" >$ {res_msg} </p>
Login. jsp is used to encrypt the password with MD5 when you enter the account and password on the logon interface!
<Form action = "<% = contextPath %>/login/userLogin" id = "loginForm" method = "post">
RequestUserLogin ()Method:
1 @ RequestMapping ("/userLogin") 2 public ModelAndView userLogin (HttpServletRequest request) {3 ModelAndView mav = null; 4 String loginAccount = request. getParameter ("loginAccount"); 5 // obtain the account password 6 String password = request. getParameter ("password"); 7/** 8 * business logic layer method call: 9 * parameter: request10 * loginAccount11 * password12 * return value type: procResult13 * 14 * contains the ProcResult code and userService. userLogin () method analysis (Multi-Point login implementation )! 15 **/16 ProcResult result = userService. userLogin (request, loginAccount, password); 17 18 if (Sys. LOGIN_SUCCESS = result. getResCode () {// If the login succeeds, 19 User user = (User) request. getSession (). getAttribute ("USER"); // get the object stored by USER 20 switch (user. getAuthLevel () {// get user level (permission Control) 21 case mav = new ModelAndView ("redirect:/user/forwardHomePage"); 23 break; 24 // mav = new ModelAndView ("redirect:/org/sh OwOrgInfo "); 25 // case :26 // break; 27 // case :28 // break; 29 // case :30 // break; 31 default: 32 mav = new ModelAndView ("redirect:/user/forwardHomePage"); 33 // mav = new ModelAndView ("redirect:/org/showOrgDeptInfo "); 34} 35} else {// failed to redirect :!!! Redirection refers to the request's redirection 36 mav = new ModelAndView ("redirect:/login/ForwardLogin"); // It is directed to index again. method 37 mav. addObject ("res_msg", result. getResMsg (); // Add the relevant error message 38} 39 return mav; 40}
//////////////////////////////////////// ///////////////////////////
1ProcResult class code:2 public class ProcResult implements Serializable {3 // result code 4 private int resCode; 5 // result information 6 private String resMsg; 7 // The first two attributes 8 private Map <String, String> info; 9 private Map <String, Object> extra; 10 public static ProcResult buildResult (int code, string msg) {11 ProcResult result = new ProcResult (code, msg); 12 return result; 13} 14 public ProcResult () {}// No parameter construction! 15 public ProcResult (int code, String msg) {// constructor of two parameters 16 this. resCode = code; 17 this. resMsg = msg; 18 // you can view only the first two attributes! 19 this.info = new HashMap <String, String> (); 20 this. extra = new HashMap <String, Object> (); 21} 22 // getter & setter will not repeat 23}
1LoginAccount code:2/** 3 * Save User Logon Information 4 */5 public class LoginAccount {6 private static Map <String, Map <String, Object> accountInfo = null; 7 private LoginAccount () {} 8 private static LoginAccount instance = null; 9 public static LoginAccount getInstance () {// provides external static public methods 10 if (instance = null) {// 11 synchronized (LoginAccount. class) {// lock the class 12 LoginAccount temp = instance; 13 if (temp = null) {// dual city judgment solves the problem that multiple objects will be created in multiple threads. 14 temp = new LoginAccount (); 15 instance = temp; 16 accountInfo = new HashMap <String, Map <String, object> (); // declare a Map Object for storing the relevant account information at the first creation. 17} 18} 19} 20 return instance; 21} 22 23/*** Save User logon information 24 * @ param userId25 * @ param accountInfo */26 public void addLogin (String userId, Map <String, Object> accountInfo) {27 this. accountInfo. put (userId, accountInfo); // store the Account as a key-value pair to the current map (map is unique) 28 // the key is userId, and value is accountInfo (account information) 29} 30/*** verify that the user has logged on to 31 * @ param userId32 * @ return boolean */33 public boolean checkLogin (String userId) {34 boolean flag = false; 35 if (this. accountInfo. containsKey (userId) {// determines whether the key contains 36 flag = true; 37} 38 return flag; 39} 40/*** Remove User Logon Information 41 * @ param userId */42 public void removeLogin (String userId) {43 this. accountInfo. remove (userId); // Delete 44} 45} from map}
1 // method of the service layer ////// /// // 2 public ProcResultUserLogin(HttpServletRequest request, String loginAccount, String password) {3 User user = null; 4 user = userDao. getLoginUserInfo (loginAccount); // you can find the user information based on the account: 5/** SQL: SELECT m. *, o. duration 6 FROM m_user m left join m_org o ON m. src_org = o.org _ id 7 WHERE 1 = 1 8 AND m. del = 0 // The tag 9 AND (m. login_name = # {loginAccount} // Log On with the account name 10 OR m. mobile = # {loginAccount} // login 11 OR m with the mobile phone number. email = # {loginAccount}) // email logon 12 */ 13 LoginAccount la = LoginAccount. getInstance (); // Save the singleton mode of the logged-on account (the thread creation problem has been solved): map14 // 15 if (null! = User) {// found the user's related information 16 String userPassword = user. getPassword (). trim (); 17 if (userPassword. equals (password) {// If the password Matches successfully 18 boolean loginFlag = la. checkLogin ("" + user. getUserId (); // check whether the account is logged on to 19 int multiLogin = user. getMultiLogin (); // obtain whether the user is allowed to log on to the 20 List <Org> orgs = orgDao. getUserOrgs (user. getUserId (); // obtain information about the user's organization 21 if (loginFlag) {// 22 if (1 = multiLogin | 999 = multiLogin) {// allow multi-point logon to 23 resu Lt = new ProcResult (Sys. LOGIN_SUCCESS, Message. LOGIN_SUCCESS); // logon success mark and information 24 initLoginUserInfo (user, orgs, request ); // method resolution in the following 25} else {// do not allow multi-point login 26 result = new ProcResult (Sys. LOGIN_MULTI_LOGIN, Message. MULTI_LOGIN); // do not allow multiple logon tags and Information 27} 28} else {// 29 Date today = new Date (); 30 Date duration = user. getDuration (); // the user's service life 31 if (null = duration | duration. before (today) {// has expired or is null 32 result = New ProcResult (Sys. LOGIN_OUT_DURATION, Message. OUT_DURATION); // service expiration mark and information 33} else {// 34 successful initLoginUserInfo (user, orgs, request); 35 result = new ProcResult (Sys. LOGIN_SUCCESS, Message. LOGIN_SUCCESS); // logon success mark and information 36} 37 38} 39 // The parameters in initLoginUserInfo have operated on the user. They operate on the same object as the user !! 40 result. getExtra (). put ("USER", user); // put it in ProResult to 41} else {42 result = new ProcResult (Sys. LOGIN_WRONG_PWD, Message. WRONG_PWD); // Password error code and Information 43} 44 45} else {46 result = new ProcResult (Sys. LOGIN_WRONG_USER, Message. NONE_USER); // the user does not have an identifier and information 47} 48 return result; 49}
1 // initLoginUserInfo (user, orgs, request) method Resolution 2 // This method is inBaseServiceMethod In: 3/** 4 * Save user Logon Information in session 5 * @ param user orgs request 6 * @ return 7 */8 protected booleanInitLoginUserInfo(User user, List <Org> orgs, HttpServletRequest request) {9 boolean flag = false; 10 HttpSession session = request. getSession (); 11 Org o = null; 12 13 if (orgs. size () = 0) {// if the user does not have any organization 14 user. setAuthLevel (4); // set the permission level to 415} else if ("mkadmin ". equals (user. getLoginName () | Sys. FACILITATOR_ADMIN = (int) user. getType () {// if it is admin16 user. setAuthLevel (1); // set the user permission to 1 17} else {// 18 int dftO RgIndex =-1; 19 Org psOrg = null; 20 for (int I = 0; I <orgs. size (); I ++) {// traverse list-> orgs21 Org org = orgs. get (I); 22 int orgId = org. getOrgId (); 23 int userDftOrg = user. getDefaultOrgId (); 24 if (orgId = userDftOrg) {// if the default value is this organization? 25 dftOrgIndex = I; 26} 27 // Personal organization 28 if (0 = org. getType () {// personal space 29 psOrg = org; 30} 31} 32 if (-1! = DftOrgIndex) {33 o = orgs. get (dftOrgIndex); 34 // If you log on, the default organization administrator 35 int userId = user. getUserId (); 36 int orgAdminId = o. getAdminId (); 37 if (userId = orgAdminId) {// administrator of the current Ah Organization 38 user. setAuthLevel (2); // The permission level is 239} else {40 user. setAuthLevel (Sys. DPT_ADMIN); // 341} 42 session. setAttribute (Sys. SESSION_ORG, o); // set to "ORG" 43} else {44 if (null! = PsOrg) {45 psOrg. setDefaultOrg (1); 46 session. setAttribute (Sys. SESSION_ORG, psOrg); 47} 48} 49} 50 session. setAttribute ("USER_ORGS", orgs); // All organization information of the user 51 session. setAttribute (Sys. SESSION_USER, user); // user information 52 Map <String, Object> accInfo = new HashMap <String, Object> (); // 53 LoginAccount la = LoginAccount. getInstance (); 54 if (! La. checkLogin ("" + user. getUserId () {// check whether the current user has logged on 55 // comment out 56 // accInfo temporarily. put ("USER", user); 57 // LoginAccount. getInstance (). addLogin ("" + user. getUserId (), accInfo); 58} 59 flag = true; 60 return flag; 61}
Wunian7yulian 16.4.5