📄 loginportlet.java
字号:
/* * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a> * @version $Id: LoginPortlet.java 5087 2006-08-18 22:52:23Z novotny $ */package org.gridsphere.portlets.core.login;import org.gridsphere.layout.PortletPageFactory;import org.gridsphere.portlet.impl.PortletURLImpl;import org.gridsphere.portlet.impl.SportletProperties;import org.gridsphere.portlet.service.PortletServiceException;import org.gridsphere.provider.event.jsr.ActionFormEvent;import org.gridsphere.provider.event.jsr.RenderFormEvent;import org.gridsphere.provider.portlet.jsr.ActionPortlet;import org.gridsphere.provider.portletui.beans.HiddenFieldBean;import org.gridsphere.provider.portletui.beans.PasswordBean;import org.gridsphere.provider.portletui.beans.TextFieldBean;import org.gridsphere.services.core.filter.PortalFilter;import org.gridsphere.services.core.filter.PortalFilterService;import org.gridsphere.services.core.mail.MailMessage;import org.gridsphere.services.core.mail.MailService;import org.gridsphere.services.core.portal.PortalConfigService;import org.gridsphere.services.core.request.Request;import org.gridsphere.services.core.request.RequestService;import org.gridsphere.services.core.security.auth.AuthModuleService;import org.gridsphere.services.core.security.auth.AuthenticationException;import org.gridsphere.services.core.security.auth.AuthorizationException;import org.gridsphere.services.core.security.auth.modules.LoginAuthModule;import org.gridsphere.services.core.security.password.PasswordEditor;import org.gridsphere.services.core.security.password.PasswordManagerService;import org.gridsphere.services.core.user.User;import org.gridsphere.services.core.user.UserManagerService;import javax.portlet.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.security.cert.X509Certificate;import java.util.*;public class LoginPortlet extends ActionPortlet { private static String FORGOT_PASSWORD_LABEL = "forgotpassword"; private static long REQUEST_LIFETIME = 1000 * 60 * 60 * 24 * 3; // 3 days public static final String LOGIN_ERROR_FLAG = "LOGIN_FAILED"; public static final Integer LOGIN_ERROR_UNKNOWN = new Integer(-1); public static final String DO_VIEW_USER_EDIT_LOGIN = "login/createaccount.jsp"; //edit user public static final String DO_FORGOT_PASSWORD = "login/forgotpassword.jsp"; public static final String DO_NEW_PASSWORD = "login/newpassword.jsp"; private UserManagerService userManagerService = null; private PortalConfigService portalConfigService = null; private RequestService requestService = null; private MailService mailService = null; private AuthModuleService authModuleService = null; private PasswordManagerService passwordManagerService = null; private PortalFilterService portalFilterService = null; private String newpasswordURL = null; private String redirectURL = null; public void init(PortletConfig config) throws PortletException { super.init(config); userManagerService = (UserManagerService) createPortletService(UserManagerService.class); requestService = (RequestService) createPortletService(RequestService.class); mailService = (MailService) createPortletService(MailService.class); portalConfigService = (PortalConfigService) createPortletService(PortalConfigService.class); portalFilterService = (PortalFilterService) createPortletService(PortalFilterService.class); authModuleService = (AuthModuleService) createPortletService(AuthModuleService.class); passwordManagerService = (PasswordManagerService) createPortletService(PasswordManagerService.class); DEFAULT_VIEW_PAGE = "doViewUser"; } public void doViewUser(RenderFormEvent event) throws PortletException { log.debug("in LoginPortlet: doViewUser"); PortletRequest request = event.getRenderRequest(); RenderResponse response = event.getRenderResponse(); if (newpasswordURL == null) { PortletURL url = response.createActionURL(); ((PortletURLImpl) url).setAction("newpassword"); ((PortletURLImpl) url).setLayout("login"); ((PortletURLImpl) url).setEncoding(false); newpasswordURL = url.toString(); } if (redirectURL == null) { PortletURL url = response.createRenderURL(); ((PortletURLImpl) url).setLayout(PortletPageFactory.USER_PAGE); ((PortletURLImpl) url).setEncoding(false); redirectURL = url.toString(); } PasswordBean pass = event.getPasswordBean("password"); pass.setValue(""); // Check certificates String x509supported = portalConfigService.getProperty(PortalConfigService.SUPPORT_X509_AUTH); if ((x509supported != null) && (x509supported.equalsIgnoreCase("true"))) { X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); if (certs != null && certs.length > 0) { request.setAttribute("certificate", certs[0].getSubjectDN().toString()); } } String remUser = portalConfigService.getProperty(PortalConfigService.REMEMBER_USER); if ((remUser != null) && (remUser.equalsIgnoreCase("TRUE"))) { request.setAttribute("remUser", "true"); } Boolean useSecureLogin = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.USE_HTTPS_LOGIN)); request.setAttribute("useSecureLogin", useSecureLogin.toString()); boolean canUserCreateAccount = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.CAN_USER_CREATE_ACCOUNT)).booleanValue(); if (canUserCreateAccount) request.setAttribute("canUserCreateAcct", "true"); boolean dispUser = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.SEND_USER_FORGET_PASSWORD)).booleanValue(); if (dispUser) request.setAttribute("dispPass", "true"); String errorMsg = (String) request.getPortletSession(true).getAttribute(LOGIN_ERROR_FLAG); if (errorMsg != null) { createErrorMessage(event, errorMsg); request.getPortletSession(true).removeAttribute(LOGIN_ERROR_FLAG); } Boolean useUserName = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.USE_USERNAME_FOR_LOGIN)); if (useUserName) request.setAttribute("useUserName", "true"); setNextState(request, "login/login.jsp"); } public void gs_login(ActionFormEvent event) throws PortletException { log.debug("in LoginPortlet: gs_login"); PortletRequest req = event.getActionRequest(); try { login(event); } catch (AuthorizationException err) { log.debug(err.getMessage()); req.getPortletSession(true).setAttribute(LOGIN_ERROR_FLAG, err.getMessage()); } catch (AuthenticationException err) { log.debug(err.getMessage()); req.getPortletSession(true).setAttribute(LOGIN_ERROR_FLAG, err.getMessage()); } setNextState(req, DEFAULT_VIEW_PAGE); } public void notifyUser(ActionFormEvent evt) { PortletRequest req = evt.getActionRequest(); User user; TextFieldBean emailTF = evt.getTextFieldBean("emailTF"); if (emailTF.getValue().equals("")) { createErrorMessage(evt, this.getLocalizedText(req, "LOGIN_NO_EMAIL")); return; } else { user = userManagerService.getUserByEmail(emailTF.getValue()); } if (user == null) { createErrorMessage(evt, this.getLocalizedText(req, "LOGIN_NOEXIST")); return; } // create a request Request request = requestService.createRequest(FORGOT_PASSWORD_LABEL); long now = Calendar.getInstance().getTime().getTime(); request.setLifetime(new Date(now + REQUEST_LIFETIME)); request.setUserID(user.getID()); requestService.saveRequest(request); MailMessage mailToUser = new MailMessage(); mailToUser.setEmailAddress(emailTF.getValue()); String subjectHeader = portalConfigService.getProperty("LOGIN_FORGOT_SUBJECT"); if (subjectHeader == null) subjectHeader = getLocalizedText(req, "MAIL_SUBJECT_HEADER"); mailToUser.setSubject(subjectHeader); StringBuffer body = new StringBuffer(); String forgotMail = portalConfigService.getProperty("LOGIN_FORGOT_BODY"); if (forgotMail == null) forgotMail = getLocalizedText(req, "LOGIN_FORGOT_MAIL"); body.append(forgotMail).append("\n\n"); body.append(getLocalizedText(req, "USERNAME")).append(" :").append(user.getUserName()).append("\n\n"); body.append(newpasswordURL).append("&reqid=").append(request.getOid()); mailToUser.setBody(body.toString()); mailToUser.setSender(portalConfigService.getProperty(PortalConfigService.MAIL_FROM)); try { mailService.sendMail(mailToUser); createSuccessMessage(evt, this.getLocalizedText(req, "LOGIN_SUCCESS_MAIL")); } catch (PortletServiceException e) { log.error("Unable to send mail message!", e); createErrorMessage(evt, this.getLocalizedText(req, "LOGIN_FAILURE_MAIL")); setNextState(req, DEFAULT_VIEW_PAGE); } } /** * Handles login requests * * @param event a <code>GridSphereEvent</code> * @throws org.gridsphere.services.core.security.auth.AuthenticationException * if auth fails * @throws org.gridsphere.services.core.security.auth.AuthorizationException * if authz fails */ protected void login(ActionFormEvent event) throws AuthenticationException, AuthorizationException { ActionRequest req = event.getActionRequest(); ActionResponse res = event.getActionResponse(); User user = login(req); Long now = Calendar.getInstance().getTime().getTime(); user.setLastLoginTime(now); Integer numLogins = user.getNumLogins(); if (numLogins == null) numLogins = 0; numLogins++; user.setNumLogins(numLogins); user.setAttribute(PortalConfigService.LOGIN_NUMTRIES, "0"); userManagerService.saveUser(user); req.setAttribute(SportletProperties.PORTLET_USER, user); req.getPortletSession(true).setAttribute(SportletProperties.PORTLET_USER, user.getID(), PortletSession.APPLICATION_SCOPE); String query = event.getAction().getParameter("queryString"); if (query != null) { //redirectURL.setParameter("cid", query); } //req.setAttribute(SportletProperties.LAYOUT_PAGE, PortletPageFactory.USER_PAGE); String realuri = redirectURL.toString().substring("http".length()); Boolean useSecureRedirect = Boolean.valueOf(portalConfigService.getProperty(PortalConfigService.USE_HTTPS_REDIRECT)); if (useSecureRedirect.booleanValue()) { realuri = "https" + realuri; } else { realuri = "http" + realuri; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -