📄 logincontroller.java
字号:
package net.java.workeffort.webapp.action;import java.util.Locale;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import net.java.workeffort.service.domain.Login;import net.java.workeffort.service.support.InvalidPartyException;import net.java.workeffort.service.support.InvalidPasswordException;import net.java.workeffort.webapp.security.RoleManager;import net.java.workeffort.webapp.security.SecurityProfile;import net.java.workeffort.webapp.support.BreadCrumbs;import net.java.workeffort.webapp.support.WebConstants;import org.apache.commons.lang.Validate;import org.springframework.validation.BindException;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.support.RequestContextUtils;/** * The login controller. * Does the following: * <ol> * <li>Validates userid and password.</li> * <li>if above step is successful, creates a SecurityProfile for the user and * stores it in users session</li> * <li>Sets the users locale. (Application is configured to use * <code>SessionLocaleResolver</code>)</li> * <li>Creates an empty <code>BreadCrumbs</code> and stores it in users session</li> * <li>Forwards user to the welcome page</li> * </ol> * @author Antony Joseph */public class LoginController extends BaseFormController { public LoginController() { setCommandClass(Login.class); setFormView(".login"); setSuccessView(".welcome"); } protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { //Invalidate the current user session if any and create a new session. HttpSession session = request.getSession(); if (session != null) { logger.info("invalidating current session while logging in: " + session.getId()); try { session.invalidate(); } catch (Exception e) { logger.error("session.invalidate() exception in LoginController." + e); } } session = request.getSession(true); Login login = (Login) command; SecurityProfile securityProfile = new SecurityProfile(login .getPartyCd(), login.getPassword()); RoleManager roleManager = (RoleManager) getWebApplicationContext() .getBean("roleManager"); Validate.notNull("Could not find 'roleManager' in application context"); Map rolePermissions = null; try { rolePermissions = roleManager.getPartyPermissions(securityProfile); } catch (InvalidPartyException ipe) { errors.reject("invalid.userId"); return showForm(request, response, errors); } catch (InvalidPasswordException ipwe) { errors.reject("invalid.password"); return showForm(request, response, errors); } // If the program reaches here the user has been successully // authenticated and has some permissions. Create a new security profile // with the permissions and store in session SecurityProfile securityProfileWithRoles = new SecurityProfile( securityProfile.getPartyCd(), rolePermissions); request.getSession().setAttribute(WebConstants.SECURITY_PROFILE, securityProfileWithRoles); // Since the application is using 'SessionLocaleResolver' set the locale // here so that it gets stored in the users session Locale locale = RequestContextUtils.getLocale(request); LocaleResolver resolver = RequestContextUtils .getLocaleResolver(request); resolver.setLocale(request, response, locale); // store an empty bread crumbs in session. request.getSession().setAttribute(WebConstants.BREAD_CRUMBS, new BreadCrumbs()); return new ModelAndView(".welcome"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -