1 13 package info.magnolia.cms.filters; 14 15 import info.magnolia.cms.beans.config.Server; 16 import info.magnolia.cms.core.Path; 17 import info.magnolia.cms.security.Authenticator; 18 import info.magnolia.cms.security.Listener; 19 import info.magnolia.cms.security.Lock; 20 import info.magnolia.cms.security.SecureURI; 21 import info.magnolia.cms.security.SessionAccessControl; 22 23 import java.io.IOException ; 24 25 import javax.servlet.Filter ; 26 import javax.servlet.FilterChain ; 27 import javax.servlet.FilterConfig ; 28 import javax.servlet.ServletException ; 29 import javax.servlet.ServletRequest ; 30 import javax.servlet.ServletResponse ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.log4j.Logger; 35 36 37 41 public class SecurityFilter implements Filter { 42 43 46 private static Logger log = Logger.getLogger(SecurityFilter.class); 47 48 51 public void init(FilterConfig filterConfig) throws ServletException { 52 } 54 55 58 public void destroy() { 59 } 61 62 66 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException , 67 ServletException { 68 69 HttpServletRequest request = (HttpServletRequest ) req; 70 HttpServletResponse response = (HttpServletResponse ) res; 71 72 if (isAllowed(request, response)) { 73 chain.doFilter(request, response); 74 } 75 } 76 77 84 protected boolean isAllowed(HttpServletRequest req, HttpServletResponse res) throws IOException { 85 if (Lock.isSystemLocked()) { 86 res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); 87 return false; 88 } 89 else if (SessionAccessControl.isSecuredSession(req)) { 90 return true; 91 } 92 else if (SecureURI.isProtected(Path.getURI(req))) { 93 return authenticate(req, res); 94 } 95 else if (!Listener.isAllowed(req)) { 96 res.sendError(HttpServletResponse.SC_FORBIDDEN); 97 return false; 98 } 99 return true; 100 } 101 102 108 private boolean authenticate(HttpServletRequest request, HttpServletResponse response) { 109 try { 110 if (!Authenticator.authenticate(request)) { 111 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 112 response.setHeader("WWW-Authenticate", "BASIC realm=\"" + Server.getBasicRealm() + "\""); 114 SessionAccessControl.invalidateUser(request); 116 return false; 117 } 118 } 119 catch (Exception e) { 120 log.error(e.getMessage(), e); 121 return false; 122 } 123 124 return true; 125 } 126 127 } 128 | Popular Tags |