1 13 package info.magnolia.cms.security; 14 15 import info.magnolia.cms.beans.config.Server; 16 import info.magnolia.cms.core.Path; 17 import info.magnolia.cms.util.FreeMarkerUtil; 18 19 import java.io.IOException ; 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 import javax.servlet.Filter ; 24 import javax.servlet.FilterChain ; 25 import javax.servlet.FilterConfig ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.ServletResponse ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 33 import org.apache.commons.lang.StringUtils; 34 import org.slf4j.Logger; 35 import org.slf4j.LoggerFactory; 36 import freemarker.template.Template; 37 38 39 43 public class SecurityFilter implements Filter { 44 45 48 private static Logger log = LoggerFactory.getLogger(SecurityFilter.class); 49 50 53 protected static final String LOGIN_FORM = "LoginForm"; 54 55 58 protected static final String UNSECURED_URI = "UnsecuredPath"; 59 60 63 protected static final String AUTH_TYPE = "AuthType"; 64 65 68 protected static final String AUTH_TYPE_BASIC = "Basic"; 69 70 73 protected static final String AUTH_TYPE_FORM = "Form"; 74 75 78 public void init(FilterConfig filterConfig) throws ServletException { 79 } 81 82 85 public void destroy() { 86 } 88 89 93 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException , 94 ServletException { 95 96 HttpServletRequest request = (HttpServletRequest ) req; 97 HttpServletResponse response = (HttpServletResponse ) res; 98 99 if (isAllowed(request, response)) { 100 chain.doFilter(request, response); 101 } 102 } 103 104 111 protected boolean isAllowed(HttpServletRequest req, HttpServletResponse res) throws IOException { 112 if (Lock.isSystemLocked()) { 113 res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); 114 return false; 115 } 116 else if (Authenticator.isAuthenticated(req)) { 117 return true; 118 } 119 else if (SecureURI.isUnsecure(Path.getURI(req))) { 120 return true; 121 } 122 else if (SecureURI.isProtected(Path.getURI(req))) { 123 return authenticate(req, res); 124 } 125 else if (!Listener.isAllowed(req)) { 126 res.sendError(HttpServletResponse.SC_FORBIDDEN); 127 return false; 128 } 129 return true; 130 } 131 132 138 protected boolean authenticate(HttpServletRequest request, HttpServletResponse response) { 139 try { 140 141 String unsecuredUri = (String ) Server.getInstance().getLoginConfig().get(UNSECURED_URI); 142 143 if (unsecuredUri != null) { 144 if (Path.getURI(request).startsWith(unsecuredUri)) { 145 return true; 146 } 147 } 148 149 if (!Authenticator.authenticate(request)) { 150 152 String authType = (String ) Server.getInstance().getLoginConfig().get(AUTH_TYPE); 153 154 HttpSession httpsession = request.getSession(false); 155 if (httpsession != null) { 156 httpsession.invalidate(); 157 } 158 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 159 160 if (StringUtils.equalsIgnoreCase(authType, AUTH_TYPE_FORM) 161 && !StringUtils.equals(request.getParameter(AUTH_TYPE), AUTH_TYPE_BASIC)) { String loginUrl = (String ) Server.getInstance().getLoginConfig().get(LOGIN_FORM); 163 log.debug("Using login url: {}", loginUrl); 164 165 if (StringUtils.equalsIgnoreCase(loginUrl, "/.resources/loginForm/login.html")) { 168 loginUrl = "/mgnl-resources/loginForm/login.html"; 169 log.error("Incorrect login form", new Exception ()); 170 log.error("config/server/LoginForm default value is changed to - /mgnl-resources/loginForm/login.html"); 171 log.error("Please bootstrap new config, or change the value manually "); 172 } 173 174 try { 175 Template tmpl = FreeMarkerUtil.getDefaultConfiguration().getTemplate(loginUrl); 177 Map data = new HashMap (); 178 data.put("contextPath", request.getContextPath()); 179 tmpl.process(data, response.getWriter()); 180 } 181 catch (Exception e) { 182 log.error("exception while writing login template", e); 183 } 184 } 185 else { 186 doBasicAuthentication(response); 187 } 188 189 return false; 190 } 191 } 192 catch (Exception e) { 193 log.error(e.getMessage(), e); 194 return false; 195 } 196 197 return true; 198 } 199 200 203 private void doBasicAuthentication(HttpServletResponse response) { 204 response.setHeader("WWW-Authenticate", "BASIC realm=\"" + Server.getBasicRealm() + "\""); 205 } 206 207 } 208 | Popular Tags |