1 package com.atlassian.seraph.filter; 2 3 import org.apache.log4j.Category; 4 5 import javax.servlet.*; 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpServletResponse ; 8 import java.io.IOException ; 9 import java.util.HashSet ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 import java.security.Principal ; 13 14 import com.atlassian.seraph.config.SecurityConfig; 15 import com.atlassian.seraph.config.SecurityConfigFactory; 16 import com.atlassian.seraph.SecurityService; 17 import com.atlassian.seraph.auth.AuthenticationContext; 18 import com.atlassian.seraph.util.RedirectUtils; 19 20 25 public class SecurityFilter implements Filter 26 { 27 private FilterConfig config = null; 28 private SecurityConfig securityConfig = null; 29 30 private static final Category log = Category.getInstance(SecurityFilter.class); 31 private static final String ALREADY_FILTERED = "os_securityfilter_already_filtered"; 32 public static final String ORIGINAL_URL = "atlassian.core.seraph.original.url"; 33 34 public void init(FilterConfig config) 35 { 36 this.config = config; 38 39 String configFileLocation = null; 40 41 if (config.getInitParameter("config.file") != null) 42 { 43 configFileLocation = config.getInitParameter("config.file"); 44 log.debug("Security config file location: " + configFileLocation); 45 } 46 47 securityConfig = SecurityConfigFactory.getInstance(configFileLocation); 48 config.getServletContext().setAttribute(SecurityConfig.STORAGE_KEY, securityConfig); 49 } 50 51 public void destroy() 52 { 53 securityConfig.destroy(); 55 securityConfig = null; 56 config = null; 57 } 58 59 62 public FilterConfig getFilterConfig() 64 { 65 return config; 66 } 67 68 71 public void setFilterConfig(FilterConfig filterConfig) 73 { 74 if (filterConfig != null) init(filterConfig); 76 } 77 78 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 79 throws IOException , ServletException 80 { 81 if (req.getAttribute(ALREADY_FILTERED) != null || !getSecurityConfig().getController().isSecurityEnabled()) 82 { 83 chain.doFilter(req, res); 84 return; 85 } 86 else 87 { 88 req.setAttribute(ALREADY_FILTERED, Boolean.TRUE); 89 } 90 91 if (req.getAttribute(LoginFilter.ALREADY_FILTERED) == null) 94 { 95 log.warn("LoginFilter not yet applied to this request - terminating filter chain"); 96 return; 97 } 98 99 HttpServletRequest request = (HttpServletRequest ) req; 100 HttpServletResponse response = (HttpServletResponse ) res; 101 102 String originalURL = request.getServletPath() + 103 (request.getPathInfo() == null ? "" : request.getPathInfo()) + 104 (request.getQueryString() == null ? "" : "?" + request.getQueryString()); 105 106 107 request.setAttribute(SecurityFilter.ORIGINAL_URL, originalURL); 109 110 Set requiredRoles = new HashSet (); 111 112 for (Iterator iterator = getSecurityConfig().getServices().iterator(); iterator.hasNext();) 114 { 115 SecurityService service = (SecurityService) iterator.next(); 116 117 Set serviceRoles = service.getRequiredRoles(request); 118 requiredRoles.addAll(serviceRoles); 119 } 120 121 if (log.isDebugEnabled()) { 122 log.debug("requiredRoles = " + requiredRoles); 123 } 124 125 boolean needAuth = false; 127 128 Principal user = getSecurityConfig().getAuthenticator().getUser(request, response); 130 131 getAuthenticationContext().setUser(user); 133 134 for (Iterator iterator = requiredRoles.iterator(); iterator.hasNext();) 137 { 138 String role = (String ) iterator.next(); 139 140 if (!getSecurityConfig().getRoleMapper().hasRole(user, request, role)) 143 { 144 log.info("User '" + user + "' needs (and lacks) role '" + role + "' to access " + originalURL); 145 needAuth = true; 146 } 147 } 148 149 if (request.getServletPath() != null && request.getServletPath().equals(getSecurityConfig().getLoginURL())) 151 { 152 needAuth = false; 153 } 154 155 if (needAuth) 157 { 158 if (log.isDebugEnabled()) 159 log.debug("Need Authentication: Redirecting to: " + getSecurityConfig().getLoginURL() + " from: " + originalURL); 160 request.getSession().setAttribute(getSecurityConfig().getOriginalURLKey(), originalURL); 161 response.sendRedirect(RedirectUtils.getLoginUrl(request)); 162 return; 163 } 164 else 165 { 166 try 167 { 168 chain.doFilter(req, res); 169 } 170 finally 171 { 172 getAuthenticationContext().clearUser(); 174 } 175 } 176 } 177 178 protected SecurityConfig getSecurityConfig() { 179 if (securityConfig == null) 181 { 182 securityConfig = (SecurityConfig) config.getServletContext().getAttribute(SecurityConfig.STORAGE_KEY); 183 } 184 return securityConfig; 185 } 186 187 protected AuthenticationContext getAuthenticationContext() { 188 return getSecurityConfig().getAuthenticationContext(); 189 } 190 191 } 192 | Popular Tags |