1 package org.roller.presentation; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.roller.presentation.util.SslUtil; 6 import org.roller.util.Utilities; 7 8 import java.io.IOException ; 9 10 import javax.servlet.ServletContext ; 11 import javax.servlet.ServletException ; 12 import javax.servlet.http.HttpServlet ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import org.roller.config.RollerConfig; 16 17 18 19 41 public class LoginServlet extends HttpServlet 42 { 43 static final long serialVersionUID = 8054268881470797053L; 44 45 protected static String mAuthURL = "/j_security_check"; 46 protected static String mAlgorithm = "SHA"; 47 protected static Boolean mEncrypt = Boolean.FALSE; 48 49 private boolean secureLogin = false; 50 private String loginHttpsPort = SslUtil.STD_HTTPS_PORT; 51 52 private static Log mLogger = LogFactory.getLog(LoginServlet.class); 56 57 59 65 public void init() throws ServletException 66 { 67 if (getInitParameter("authURL") != null) 70 { 71 mAuthURL = getInitParameter("authURL"); 72 } 73 74 String secureLogin = RollerConfig.getProperty("securelogin.enabled"); 75 if(secureLogin != null && "true".equalsIgnoreCase(secureLogin)) 76 this.secureLogin = true; 77 78 String secureLoginPort = RollerConfig.getProperty("securelogin.https.port"); 79 if(secureLoginPort != null) 80 this.loginHttpsPort = secureLoginPort; 81 82 mLogger.info("secure login enabled: "+this.secureLogin); 83 mLogger.info("secure login port: "+this.loginHttpsPort); 84 } 85 86 95 public void doGet(HttpServletRequest request, HttpServletResponse response) 96 throws IOException , ServletException 97 { 98 execute(request, response); 99 } 100 101 110 public void doPost(HttpServletRequest request, HttpServletResponse response) 111 throws IOException , ServletException 112 { 113 execute(request, response); 114 } 115 116 126 public void execute(HttpServletRequest request, 127 HttpServletResponse response) throws IOException , 128 ServletException 129 { 130 if (request.getRemoteUser() != null) { 134 if (mLogger.isDebugEnabled()) { 135 mLogger.debug("User '" + request.getRemoteUser() + 136 "' already logged in, routing to main"); 137 } 138 response.sendRedirect(request.getContextPath() + "/main.do"); 139 return; 140 } 141 142 String username = request.getParameter("j_username"); 144 String password = request.getParameter("j_password"); 145 146 String encryptedPassword = getEncryptedPassword(request, username, password); 147 148 String req = null; 149 String contextUrl = null; 150 if (this.secureLogin) 151 { 152 StringBuffer sb = new StringBuffer (""); 154 sb.append("https://"); 155 if (this.loginHttpsPort.equals(SslUtil.STD_HTTPS_PORT)) 156 { 157 sb.append(request.getServerName()); 158 } 159 else 160 { 161 sb.append(request.getServerName() + ":" + this.loginHttpsPort); 162 } 163 sb.append(request.getContextPath()); 164 contextUrl = sb.toString(); 165 } 166 else 167 { 168 contextUrl = request.getContextPath(); 169 } 170 req = contextUrl + mAuthURL 172 + "?j_username=" + username 173 + "&j_password=" + encryptedPassword 174 + "&j_uri=" + request.getParameter("j_uri"); 175 176 if (mLogger.isDebugEnabled()) 177 { 178 mLogger.debug("Authenticating user '" + username + "'"); 179 } 180 181 response.sendRedirect(response.encodeRedirectURL(req)); 182 } 183 184 193 public static String getEncryptedPassword( 194 HttpServletRequest request, String username, String password) 195 { 196 mEncrypt = new Boolean (RollerConfig.getProperty("passwds.encryption.enabled")); 198 mAlgorithm = RollerConfig.getProperty("passwds.encryption.algorithm"); 199 200 if (mLogger.isDebugEnabled()) 201 { 202 mLogger.debug("Authentication URL: " + mAuthURL); 203 mLogger.debug("Programmatic encryption of password? " + mEncrypt); 204 mLogger.debug("Encryption algorithm: " + mAlgorithm); 205 } 206 207 if (request.getParameter("rememberMe") != null) { 208 request.getSession().setAttribute(RollerRequest.LOGIN_COOKIE, "true"); 209 } 210 String encryptedPassword = ""; 211 if (mEncrypt.booleanValue() && (request.getAttribute("encrypt") == null)) 212 { 213 if (mLogger.isDebugEnabled()) 214 { 215 mLogger.debug("Encrypting password for user '" + username + "'"); 216 } 217 encryptedPassword = Utilities.encodePassword(password, mAlgorithm); 218 } 219 else 220 { 221 encryptedPassword = password; 222 } 223 return encryptedPassword; 224 } 225 } | Popular Tags |