Browse Source
Also deprecated the existing AspectJ interceptors. This will also allow future simplification of the AbstractMethodSecurityMetadataSource, as it no longer needs to support JoinPoints.3.0.x
13 changed files with 315 additions and 56 deletions
@ -1,5 +1,6 @@
@@ -1,5 +1,6 @@
|
||||
|
||||
dependencies { |
||||
compile project(':spring-security-core'), |
||||
"org.springframework:spring-beans:$springVersion" |
||||
"org.springframework:spring-beans:$springVersion", |
||||
"org.springframework:spring-context:$springVersion" |
||||
} |
||||
@ -0,0 +1,110 @@
@@ -0,0 +1,110 @@
|
||||
package org.springframework.security.access.intercept.aspectj.aspect; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.mockito.Mock; |
||||
import org.mockito.MockitoAnnotations; |
||||
import org.springframework.security.access.AccessDecisionManager; |
||||
import org.springframework.security.access.AccessDecisionVoter; |
||||
import org.springframework.security.access.AccessDeniedException; |
||||
import org.springframework.security.access.annotation.Secured; |
||||
import org.springframework.security.access.annotation.SecuredAnnotationSecurityMetadataSource; |
||||
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; |
||||
import org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory; |
||||
import org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice; |
||||
import org.springframework.security.access.intercept.aspectj.AspectJMethodSecurityInterceptor; |
||||
import org.springframework.security.access.prepost.PreAuthorize; |
||||
import org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter; |
||||
import org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource; |
||||
import org.springframework.security.access.vote.AffirmativeBased; |
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; |
||||
import org.springframework.security.authentication.AuthenticationManager; |
||||
import org.springframework.security.authentication.TestingAuthenticationToken; |
||||
import org.springframework.security.core.context.SecurityContextHolder; |
||||
|
||||
/** |
||||
* |
||||
* @author Luke Taylor |
||||
* @since 3.0.3 |
||||
*/ |
||||
public class AnnotationSecurityAspectTests { |
||||
private @Mock AccessDecisionManager adm; |
||||
private @Mock AuthenticationManager authman; |
||||
private TestingAuthenticationToken anne = new TestingAuthenticationToken("anne", "", "ROLE_A"); |
||||
// private TestingAuthenticationToken bob = new TestingAuthenticationToken("bob", "", "ROLE_B");
|
||||
private AspectJMethodSecurityInterceptor interceptor; |
||||
private SecuredImpl secured = new SecuredImpl(); |
||||
private PrePostSecured prePostSecured = new PrePostSecured(); |
||||
|
||||
@Before |
||||
public final void setUp() throws Exception { |
||||
MockitoAnnotations.initMocks(this); |
||||
interceptor = new AspectJMethodSecurityInterceptor(); |
||||
interceptor.setAccessDecisionManager(adm); |
||||
interceptor.setAuthenticationManager(authman); |
||||
interceptor.setSecurityMetadataSource(new SecuredAnnotationSecurityMetadataSource()); |
||||
AnnotationSecurityAspect secAspect = AnnotationSecurityAspect.aspectOf(); |
||||
secAspect.setSecurityInterceptor(interceptor); |
||||
} |
||||
|
||||
@After |
||||
public void clearContext() { |
||||
SecurityContextHolder.clearContext(); |
||||
} |
||||
|
||||
@Test |
||||
public void securedInterfaceMethodAllowsAllAccess() throws Exception { |
||||
secured.securedMethod(); |
||||
} |
||||
|
||||
@Test(expected=AuthenticationCredentialsNotFoundException.class) |
||||
public void securedClassMethodDeniesUnauthenticatedAccess() throws Exception { |
||||
secured.securedClassMethod(); |
||||
} |
||||
|
||||
@Test |
||||
public void securedClassMethodAllowsAccessToRoleA() throws Exception { |
||||
SecurityContextHolder.getContext().setAuthentication(anne); |
||||
secured.securedClassMethod(); |
||||
} |
||||
|
||||
// SEC-1262
|
||||
@Test(expected=AccessDeniedException.class) |
||||
public void denyAllPreAuthorizeDeniesAccess() throws Exception { |
||||
SecurityContextHolder.getContext().setAuthentication(anne); |
||||
interceptor.setSecurityMetadataSource(new PrePostAnnotationSecurityMetadataSource( |
||||
new ExpressionBasedAnnotationAttributeFactory(new DefaultMethodSecurityExpressionHandler()))); |
||||
AffirmativeBased adm = new AffirmativeBased(); |
||||
AccessDecisionVoter[] voters = new AccessDecisionVoter[] |
||||
{new PreInvocationAuthorizationAdviceVoter(new ExpressionBasedPreInvocationAdvice())}; |
||||
adm.setDecisionVoters(Arrays.asList(voters)); |
||||
interceptor.setAccessDecisionManager(adm); |
||||
prePostSecured.denyAllMethod(); |
||||
} |
||||
} |
||||
|
||||
interface SecuredInterface { |
||||
@Secured("ROLE_X") |
||||
void securedMethod(); |
||||
} |
||||
|
||||
class SecuredImpl implements SecuredInterface { |
||||
|
||||
// Not really secured because AspectJ doesn't inherit annotations from interfaces
|
||||
public void securedMethod() { |
||||
} |
||||
|
||||
@Secured("ROLE_A") |
||||
public void securedClassMethod() { |
||||
} |
||||
} |
||||
|
||||
class PrePostSecured { |
||||
|
||||
@PreAuthorize("denyAll") |
||||
public void denyAllMethod() { |
||||
} |
||||
} |
||||
@ -0,0 +1,51 @@
@@ -0,0 +1,51 @@
|
||||
package org.springframework.security.access.intercept.aspectj; |
||||
|
||||
import org.aspectj.lang.JoinPoint; |
||||
import org.springframework.security.access.intercept.InterceptorStatusToken; |
||||
import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor; |
||||
|
||||
/** |
||||
* AspectJ {@code JoinPoint} security interceptor which wraps the {@code JoinPoint} in a {@code MethodInvocation} |
||||
* adapter to make it compatible with security infrastructure classes which only support {@code MethodInvocation}s. |
||||
* <p> |
||||
* One of the {@code invoke} methods should be called from the {@code around()} advice in your aspect. |
||||
* Alternatively you can use one of the pre-defined aspects from the aspects module. |
||||
* |
||||
* @author Luke Taylor |
||||
* @since 3.0.3 |
||||
*/ |
||||
public final class AspectJMethodSecurityInterceptor extends MethodSecurityInterceptor { |
||||
|
||||
/** |
||||
* Method that is suitable for user with @Aspect notation. |
||||
* |
||||
* @param jp The AspectJ joint point being invoked which requires a security decision |
||||
* @return The returned value from the method invocation |
||||
* @throws Throwable if the invocation throws one |
||||
*/ |
||||
public Object invoke(JoinPoint jp) throws Throwable { |
||||
return super.invoke(new MethodInvocationAdapter(jp)); |
||||
} |
||||
|
||||
/** |
||||
* Method that is suitable for user with traditional AspectJ-code aspects. |
||||
* |
||||
* @param jp The AspectJ joint point being invoked which requires a security decision |
||||
* @param advisorProceed the advice-defined anonymous class that implements {@code AspectJCallback} containing |
||||
* a simple {@code return proceed();} statement |
||||
* |
||||
* @return The returned value from the method invocation |
||||
*/ |
||||
public Object invoke(JoinPoint jp, AspectJCallback advisorProceed) { |
||||
Object result = null; |
||||
InterceptorStatusToken token = super.beforeInvocation(new MethodInvocationAdapter(jp)); |
||||
|
||||
try { |
||||
result = advisorProceed.proceedWithObject(); |
||||
} finally { |
||||
result = super.afterInvocation(token, result); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
package org.springframework.security.access.intercept.aspectj; |
||||
|
||||
import java.lang.reflect.AccessibleObject; |
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.aopalliance.intercept.MethodInvocation; |
||||
import org.aspectj.lang.JoinPoint; |
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.reflect.CodeSignature; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* Decorates a JoinPoint to allow it to be used with method-security infrastructure |
||||
* classes which support {@code MethodInvocation} instances. |
||||
* |
||||
* @author Luke Taylor |
||||
* @since 3.0.3 |
||||
*/ |
||||
public final class MethodInvocationAdapter implements MethodInvocation { |
||||
private final ProceedingJoinPoint jp; |
||||
private final Method method; |
||||
private final Object target; |
||||
|
||||
MethodInvocationAdapter(JoinPoint jp) { |
||||
this.jp = (ProceedingJoinPoint)jp; |
||||
if (jp.getTarget() != null) { |
||||
target = jp.getTarget(); |
||||
} else { |
||||
// SEC-1295: target may be null if an ITD is in use
|
||||
target = jp.getSignature().getDeclaringType(); |
||||
} |
||||
String targetMethodName = jp.getStaticPart().getSignature().getName(); |
||||
Class<?>[] types = ((CodeSignature) jp.getStaticPart().getSignature()).getParameterTypes(); |
||||
Class<?> declaringType = ((CodeSignature) jp.getStaticPart().getSignature()).getDeclaringType(); |
||||
|
||||
method = ClassUtils.getMethodIfAvailable(declaringType, targetMethodName, types); |
||||
Assert.notNull(method, "Could not obtain target method from JoinPoint: '"+ jp + "'"); |
||||
|
||||
} |
||||
|
||||
public Method getMethod() { |
||||
return method; |
||||
} |
||||
|
||||
public Object[] getArguments() { |
||||
return jp.getArgs(); |
||||
} |
||||
|
||||
public AccessibleObject getStaticPart() { |
||||
return method; |
||||
} |
||||
|
||||
public Object getThis() { |
||||
return target; |
||||
} |
||||
|
||||
public Object proceed() throws Throwable { |
||||
return jp.proceed(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue