1 package org.roller.presentation.filters; 2 3 import java.io.IOException ; 4 5 import javax.servlet.Filter ; 6 import javax.servlet.FilterChain ; 7 import javax.servlet.FilterConfig ; 8 import javax.servlet.RequestDispatcher ; 9 import javax.servlet.ServletException ; 10 import javax.servlet.ServletRequest ; 11 import javax.servlet.ServletResponse ; 12 import javax.servlet.http.Cookie ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import org.apache.commons.lang.StringUtils; 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.roller.config.RollerConfig; 20 import org.roller.model.UserManager; 21 import org.roller.pojos.UserData; 22 import org.roller.presentation.util.RequestUtil; 23 import org.roller.presentation.RollerRequest; 24 import org.roller.util.Utilities; 25 26 27 36 public final class LoginFilter implements Filter 37 { 38 40 private Log mLogger = LogFactory.getLog(LoginFilter.class); 41 private boolean enabled = true; 42 43 45 public void doFilter(ServletRequest req, ServletResponse resp, 46 FilterChain chain) 47 throws IOException , ServletException 48 { 49 50 HttpServletRequest request = (HttpServletRequest ) req; 51 HttpServletResponse response = (HttpServletResponse ) resp; 52 53 Cookie c = RequestUtil.getCookie(request, RollerRequest.LOGIN_COOKIE); 55 56 try 57 { 58 RollerRequest rreq = RollerRequest.getRollerRequest(request); 59 UserManager mgr = rreq.getRoller().getUserManager(); 60 61 if (request.getRequestURL().indexOf("logout") != -1) 64 { 65 if (mLogger.isDebugEnabled()) 66 { 67 mLogger.debug("logging out '" + request.getRemoteUser() + "'"); 68 } 69 70 mgr.removeLoginCookies(request.getRemoteUser()); 71 rreq.getRoller().commit(); 72 RequestUtil.deleteCookie(response, c, request.getContextPath()); 73 } 74 else if (c != null && enabled) 75 { 76 String loginCookie = mgr.checkLoginCookie(c.getValue()); 77 rreq.getRoller().commit(); 78 79 if (loginCookie != null) 80 { 81 RequestUtil.setCookie(response, RollerRequest.LOGIN_COOKIE, 82 loginCookie, 83 request.getContextPath()); 84 loginCookie = Utilities.decodeString(loginCookie); 85 86 String [] value = StringUtils.split(loginCookie, '|'); 87 88 UserData user = mgr.getUser( value[0] ); 89 90 String route = "/auth?j_username=" + 92 user.getUserName() + "&j_password=" + 93 user.getPassword(); 94 95 request.setAttribute("encrypt", "false"); 96 request.getSession().setAttribute("cookieLogin", "true"); 97 98 if (mLogger.isDebugEnabled()) 99 { 100 mLogger.debug("I remember you '" + user.getUserName() + 101 "', attempting to authenticate..."); 102 } 103 104 RequestDispatcher dispatcher = 105 request.getRequestDispatcher(route); 106 dispatcher.forward(request, response); 107 108 return; 109 } 110 } 111 112 } catch (Exception e) 113 { 114 mLogger.warn(e.getMessage()); 116 } 117 118 chain.doFilter(req, resp); 119 } 120 121 124 public void init(FilterConfig config) 125 { 126 mLogger.debug("Initializing Login Filter"); 127 128 String param = RollerConfig.getProperty("loginfilter.rememberme.enabled"); 129 enabled = Boolean.valueOf(param).booleanValue(); 130 131 mLogger.info("Remember Me enabled: " + enabled); 132 133 config.getServletContext().setAttribute("rememberMeEnabled", param); 134 } 135 136 139 public void destroy() { 140 } 141 } 142 | Popular Tags |