SEC-2190: Support WebApplicationContext in ServletContext
This commit is contained in:
+5
-4
@@ -42,6 +42,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.FilterInvocation;
|
||||
import org.springframework.security.web.WebAttributes;
|
||||
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;
|
||||
import org.springframework.security.web.context.support.SecurityWebApplicationContextUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
@@ -312,8 +313,7 @@ public abstract class AbstractAuthorizeTag {
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private SecurityExpressionHandler<FilterInvocation> getExpressionHandler() throws IOException {
|
||||
ApplicationContext appContext = WebApplicationContextUtils
|
||||
.getRequiredWebApplicationContext(getServletContext());
|
||||
ApplicationContext appContext = SecurityWebApplicationContextUtils.findRequiredWebApplicationContext(getServletContext());
|
||||
Map<String, SecurityExpressionHandler> handlers = appContext
|
||||
.getBeansOfType(SecurityExpressionHandler.class);
|
||||
|
||||
@@ -335,8 +335,9 @@ public abstract class AbstractAuthorizeTag {
|
||||
return privEvaluatorFromRequest;
|
||||
}
|
||||
|
||||
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
|
||||
Map<String, WebInvocationPrivilegeEvaluator> wipes = ctx.getBeansOfType(WebInvocationPrivilegeEvaluator.class);
|
||||
ApplicationContext ctx = SecurityWebApplicationContextUtils.findRequiredWebApplicationContext(getServletContext());
|
||||
Map<String, WebInvocationPrivilegeEvaluator> wipes = ctx
|
||||
.getBeansOfType(WebInvocationPrivilegeEvaluator.class);
|
||||
|
||||
if (wipes.size() == 0) {
|
||||
throw new IOException(
|
||||
|
||||
+2
-1
@@ -21,6 +21,7 @@ import org.springframework.security.access.PermissionEvaluator;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.taglibs.TagLibConfig;
|
||||
import org.springframework.security.web.context.support.SecurityWebApplicationContextUtils;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -136,7 +137,7 @@ public class AccessControlListTag extends TagSupport {
|
||||
protected ApplicationContext getContext(PageContext pageContext) {
|
||||
ServletContext servletContext = pageContext.getServletContext();
|
||||
|
||||
return WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
|
||||
return SecurityWebApplicationContextUtils.findRequiredWebApplicationContext(servletContext);
|
||||
}
|
||||
|
||||
public Object getDomainObject() {
|
||||
|
||||
+37
-1
@@ -12,12 +12,16 @@
|
||||
*/
|
||||
package org.springframework.security.taglibs.authz;
|
||||
|
||||
import static org.fest.assertions.Assertions.*;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletRequest;
|
||||
@@ -29,10 +33,14 @@ import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.WebAttributes;
|
||||
import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator;
|
||||
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -63,13 +71,41 @@ public class AbstractAuthorizeTagTests {
|
||||
String uri = "/something";
|
||||
WebInvocationPrivilegeEvaluator expected = mock(WebInvocationPrivilegeEvaluator.class);
|
||||
tag.setUrl(uri);
|
||||
request.setAttribute(WebAttributes.WEB_INVOCATION_PRIVILEGE_EVALUATOR_ATTRIBUTE, expected);
|
||||
request.setAttribute(WebAttributes.WEB_INVOCATION_PRIVILEGE_EVALUATOR_ATTRIBUTE,
|
||||
expected);
|
||||
|
||||
tag.authorizeUsingUrlCheck();
|
||||
|
||||
verify(expected).isAllowed(eq(""), eq(uri), eq("GET"), any(Authentication.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void privilegeEvaluatorFromChildContext() throws IOException {
|
||||
String uri = "/something";
|
||||
WebInvocationPrivilegeEvaluator expected = mock(WebInvocationPrivilegeEvaluator.class);
|
||||
tag.setUrl(uri);
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
when(wac.getBeansOfType(WebInvocationPrivilegeEvaluator.class)).thenReturn(Collections.singletonMap("wipe", expected));
|
||||
servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
|
||||
|
||||
tag.authorizeUsingUrlCheck();
|
||||
|
||||
verify(expected).isAllowed(eq(""), eq(uri), eq("GET"), any(Authentication.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void expressionFromChildContext() throws IOException {
|
||||
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "pass","USER"));
|
||||
DefaultWebSecurityExpressionHandler expected = new DefaultWebSecurityExpressionHandler();
|
||||
tag.setAccess("permitAll");
|
||||
WebApplicationContext wac = mock(WebApplicationContext.class);
|
||||
when(wac.getBeansOfType(SecurityExpressionHandler.class)).thenReturn(Collections.<String,SecurityExpressionHandler>singletonMap("wipe", expected));
|
||||
servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
|
||||
|
||||
assertThat(tag.authorize()).isTrue();
|
||||
}
|
||||
|
||||
private class AuthzTag extends AbstractAuthorizeTag {
|
||||
|
||||
@Override
|
||||
|
||||
+32
-8
@@ -26,6 +26,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.jsp.tagext.Tag;
|
||||
import java.util.*;
|
||||
|
||||
@@ -40,7 +41,7 @@ public class AccessControlListTagTests {
|
||||
AccessControlListTag tag;
|
||||
PermissionEvaluator pe;
|
||||
MockPageContext pageContext;
|
||||
Authentication bob = new TestingAuthenticationToken("bob","bobspass","A");
|
||||
Authentication bob = new TestingAuthenticationToken("bob", "bobspass", "A");
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -56,8 +57,10 @@ public class AccessControlListTagTests {
|
||||
when(ctx.getBeansOfType(PermissionEvaluator.class)).thenReturn(beanMap);
|
||||
|
||||
MockServletContext servletCtx = new MockServletContext();
|
||||
servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
|
||||
pageContext = new MockPageContext(servletCtx, new MockHttpServletRequest(), new MockHttpServletResponse());
|
||||
servletCtx.setAttribute(
|
||||
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
|
||||
pageContext = new MockPageContext(servletCtx, new MockHttpServletRequest(),
|
||||
new MockHttpServletResponse());
|
||||
tag.setPageContext(pageContext);
|
||||
}
|
||||
|
||||
@@ -78,7 +81,28 @@ public class AccessControlListTagTests {
|
||||
assertEquals("READ", tag.getHasPermission());
|
||||
|
||||
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
|
||||
assertTrue((Boolean)pageContext.getAttribute("allowed"));
|
||||
assertTrue((Boolean) pageContext.getAttribute("allowed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void childContext() throws Exception {
|
||||
ServletContext servletContext = pageContext.getServletContext();
|
||||
WebApplicationContext wac = (WebApplicationContext) servletContext
|
||||
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
|
||||
servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
|
||||
|
||||
Object domainObject = new Object();
|
||||
when(pe.hasPermission(bob, domainObject, "READ")).thenReturn(true);
|
||||
|
||||
tag.setDomainObject(domainObject);
|
||||
tag.setHasPermission("READ");
|
||||
tag.setVar("allowed");
|
||||
assertSame(domainObject, tag.getDomainObject());
|
||||
assertEquals("READ", tag.getHasPermission());
|
||||
|
||||
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
|
||||
assertTrue((Boolean) pageContext.getAttribute("allowed"));
|
||||
}
|
||||
|
||||
// SEC-2022
|
||||
@@ -95,7 +119,7 @@ public class AccessControlListTagTests {
|
||||
assertEquals("READ,WRITE", tag.getHasPermission());
|
||||
|
||||
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
|
||||
assertTrue((Boolean)pageContext.getAttribute("allowed"));
|
||||
assertTrue((Boolean) pageContext.getAttribute("allowed"));
|
||||
verify(pe).hasPermission(bob, domainObject, "READ");
|
||||
verify(pe).hasPermission(bob, domainObject, "WRITE");
|
||||
verifyNoMoreInteractions(pe);
|
||||
@@ -115,7 +139,7 @@ public class AccessControlListTagTests {
|
||||
assertEquals("1,2", tag.getHasPermission());
|
||||
|
||||
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
|
||||
assertTrue((Boolean)pageContext.getAttribute("allowed"));
|
||||
assertTrue((Boolean) pageContext.getAttribute("allowed"));
|
||||
verify(pe).hasPermission(bob, domainObject, 1);
|
||||
verify(pe).hasPermission(bob, domainObject, 2);
|
||||
verifyNoMoreInteractions(pe);
|
||||
@@ -134,7 +158,7 @@ public class AccessControlListTagTests {
|
||||
assertEquals("1,WRITE", tag.getHasPermission());
|
||||
|
||||
assertEquals(Tag.EVAL_BODY_INCLUDE, tag.doStartTag());
|
||||
assertTrue((Boolean)pageContext.getAttribute("allowed"));
|
||||
assertTrue((Boolean) pageContext.getAttribute("allowed"));
|
||||
verify(pe).hasPermission(bob, domainObject, 1);
|
||||
verify(pe).hasPermission(bob, domainObject, "WRITE");
|
||||
verifyNoMoreInteractions(pe);
|
||||
@@ -150,6 +174,6 @@ public class AccessControlListTagTests {
|
||||
tag.setVar("allowed");
|
||||
|
||||
assertEquals(Tag.SKIP_BODY, tag.doStartTag());
|
||||
assertFalse((Boolean)pageContext.getAttribute("allowed"));
|
||||
assertFalse((Boolean) pageContext.getAttribute("allowed"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user