|
|
|
@ -16,9 +16,13 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.security.web.access; |
|
|
|
package org.springframework.security.web.access; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.assertj.core.api.Assertions; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
import org.mockito.ArgumentCaptor; |
|
|
|
|
|
|
|
import org.mockito.ArgumentMatchers; |
|
|
|
|
|
|
|
import org.mockito.BDDMockito; |
|
|
|
|
|
|
|
import org.mockito.Mockito; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.context.ApplicationEventPublisher; |
|
|
|
import org.springframework.mock.web.MockServletContext; |
|
|
|
import org.springframework.mock.web.MockServletContext; |
|
|
|
@ -33,15 +37,6 @@ import org.springframework.security.web.FilterInvocation; |
|
|
|
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource; |
|
|
|
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource; |
|
|
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
|
|
|
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.anyList; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.eq; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.willThrow; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Tests |
|
|
|
* Tests |
|
|
|
* {@link org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator}. |
|
|
|
* {@link org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator}. |
|
|
|
@ -61,43 +56,45 @@ public class DefaultWebInvocationPrivilegeEvaluatorTests { |
|
|
|
@BeforeEach |
|
|
|
@BeforeEach |
|
|
|
public final void setUp() { |
|
|
|
public final void setUp() { |
|
|
|
this.interceptor = new FilterSecurityInterceptor(); |
|
|
|
this.interceptor = new FilterSecurityInterceptor(); |
|
|
|
this.ods = mock(FilterInvocationSecurityMetadataSource.class); |
|
|
|
this.ods = Mockito.mock(FilterInvocationSecurityMetadataSource.class); |
|
|
|
this.adm = mock(AccessDecisionManager.class); |
|
|
|
this.adm = Mockito.mock(AccessDecisionManager.class); |
|
|
|
this.ram = mock(RunAsManager.class); |
|
|
|
this.ram = Mockito.mock(RunAsManager.class); |
|
|
|
this.interceptor.setAuthenticationManager(mock(AuthenticationManager.class)); |
|
|
|
this.interceptor.setAuthenticationManager(Mockito.mock(AuthenticationManager.class)); |
|
|
|
this.interceptor.setSecurityMetadataSource(this.ods); |
|
|
|
this.interceptor.setSecurityMetadataSource(this.ods); |
|
|
|
this.interceptor.setAccessDecisionManager(this.adm); |
|
|
|
this.interceptor.setAccessDecisionManager(this.adm); |
|
|
|
this.interceptor.setRunAsManager(this.ram); |
|
|
|
this.interceptor.setRunAsManager(this.ram); |
|
|
|
this.interceptor.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); |
|
|
|
this.interceptor.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class)); |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void permitsAccessIfNoMatchingAttributesAndPublicInvocationsAllowed() { |
|
|
|
public void permitsAccessIfNoMatchingAttributesAndPublicInvocationsAllowed() { |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
given(this.ods.getAttributes(any())).willReturn(null); |
|
|
|
BDDMockito.given(this.ods.getAttributes(ArgumentMatchers.any())).willReturn(null); |
|
|
|
assertThat(wipe.isAllowed("/context", "/foo/index.jsp", "GET", mock(Authentication.class))).isTrue(); |
|
|
|
Assertions.assertThat(wipe.isAllowed("/context", "/foo/index.jsp", "GET", Mockito.mock(Authentication.class))) |
|
|
|
|
|
|
|
.isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void deniesAccessIfNoMatchingAttributesAndPublicInvocationsNotAllowed() { |
|
|
|
public void deniesAccessIfNoMatchingAttributesAndPublicInvocationsNotAllowed() { |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
given(this.ods.getAttributes(any())).willReturn(null); |
|
|
|
BDDMockito.given(this.ods.getAttributes(ArgumentMatchers.any())).willReturn(null); |
|
|
|
this.interceptor.setRejectPublicInvocations(true); |
|
|
|
this.interceptor.setRejectPublicInvocations(true); |
|
|
|
assertThat(wipe.isAllowed("/context", "/foo/index.jsp", "GET", mock(Authentication.class))).isFalse(); |
|
|
|
Assertions.assertThat(wipe.isAllowed("/context", "/foo/index.jsp", "GET", Mockito.mock(Authentication.class))) |
|
|
|
|
|
|
|
.isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void deniesAccessIfAuthenticationIsNull() { |
|
|
|
public void deniesAccessIfAuthenticationIsNull() { |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
assertThat(wipe.isAllowed("/foo/index.jsp", null)).isFalse(); |
|
|
|
Assertions.assertThat(wipe.isAllowed("/foo/index.jsp", null)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void allowsAccessIfAccessDecisionManagerDoes() { |
|
|
|
public void allowsAccessIfAccessDecisionManagerDoes() { |
|
|
|
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX"); |
|
|
|
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX"); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
assertThat(wipe.isAllowed("/foo/index.jsp", token)).isTrue(); |
|
|
|
Assertions.assertThat(wipe.isAllowed("/foo/index.jsp", token)).isTrue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@ -105,8 +102,10 @@ public class DefaultWebInvocationPrivilegeEvaluatorTests { |
|
|
|
public void deniesAccessIfAccessDecisionManagerDoes() { |
|
|
|
public void deniesAccessIfAccessDecisionManagerDoes() { |
|
|
|
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX"); |
|
|
|
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX"); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
willThrow(new AccessDeniedException("")).given(this.adm).decide(any(Authentication.class), any(), anyList()); |
|
|
|
BDDMockito.willThrow(new AccessDeniedException("")) |
|
|
|
assertThat(wipe.isAllowed("/foo/index.jsp", token)).isFalse(); |
|
|
|
.given(this.adm) |
|
|
|
|
|
|
|
.decide(ArgumentMatchers.any(Authentication.class), ArgumentMatchers.any(), ArgumentMatchers.anyList()); |
|
|
|
|
|
|
|
Assertions.assertThat(wipe.isAllowed("/foo/index.jsp", token)).isFalse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -118,8 +117,9 @@ public class DefaultWebInvocationPrivilegeEvaluatorTests { |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor); |
|
|
|
wipe.setServletContext(servletContext); |
|
|
|
wipe.setServletContext(servletContext); |
|
|
|
wipe.isAllowed("/foo/index.jsp", token); |
|
|
|
wipe.isAllowed("/foo/index.jsp", token); |
|
|
|
verify(this.adm).decide(eq(token), filterInvocationArgumentCaptor.capture(), any()); |
|
|
|
Mockito.verify(this.adm) |
|
|
|
assertThat(filterInvocationArgumentCaptor.getValue().getRequest().getServletContext()).isNotNull(); |
|
|
|
.decide(ArgumentMatchers.eq(token), filterInvocationArgumentCaptor.capture(), ArgumentMatchers.any()); |
|
|
|
|
|
|
|
Assertions.assertThat(filterInvocationArgumentCaptor.getValue().getRequest().getServletContext()).isNotNull(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |