target securing advice
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.security;
|
||||
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.springframework.aop.ClassFilter;
|
||||
import org.springframework.aop.MethodMatcher;
|
||||
import org.springframework.aop.Pointcut;
|
||||
import org.springframework.aop.support.AbstractPointcutAdvisor;
|
||||
import org.springframework.aop.support.ClassFilters;
|
||||
import org.springframework.aop.support.MethodMatchers;
|
||||
import org.springframework.aop.support.NameMatchMethodPointcut;
|
||||
import org.springframework.aop.support.RootClassFilter;
|
||||
import org.springframework.integration.message.BlockingTarget;
|
||||
import org.springframework.integration.message.Target;
|
||||
import org.springframework.security.AccessDecisionManager;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class TargetSecuringAdvisor extends AbstractPointcutAdvisor implements Pointcut {
|
||||
|
||||
private ClassFilter classFilter;
|
||||
|
||||
private MethodMatcher matcher;
|
||||
|
||||
private Advice targetSecuringInterceptor;
|
||||
|
||||
public TargetSecuringAdvisor(AccessDecisionManager accessDecisionManager, String securityConfig) {
|
||||
targetSecuringInterceptor = new TargetSecuringInterceptor(new ConfigAttributeDefinition(StringUtils
|
||||
.tokenizeToStringArray(securityConfig, ",")), accessDecisionManager);
|
||||
classFilter = ClassFilters.union(new RootClassFilter(Target.class), new RootClassFilter(BlockingTarget.class));
|
||||
NameMatchMethodPointcut nameMatcher = new NameMatchMethodPointcut();
|
||||
nameMatcher.addMethodName("send");
|
||||
matcher = MethodMatchers.intersection(nameMatcher, new TargetSendMethodArgMatcher());
|
||||
}
|
||||
|
||||
public Pointcut getPointcut() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Advice getAdvice() {
|
||||
return targetSecuringInterceptor;
|
||||
}
|
||||
|
||||
public ClassFilter getClassFilter() {
|
||||
return classFilter;
|
||||
}
|
||||
|
||||
public MethodMatcher getMethodMatcher() {
|
||||
return matcher;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.security;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.security.AccessDecisionManager;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.context.SecurityContext;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class TargetSecuringInterceptor implements MethodInterceptor {
|
||||
|
||||
private final ConfigAttributeDefinition targetSecurityAttributes;
|
||||
|
||||
private final AccessDecisionManager accessDecisionManager;
|
||||
|
||||
public TargetSecuringInterceptor(ConfigAttributeDefinition targetSecurityAttributes,
|
||||
AccessDecisionManager accessDecisionManager) {
|
||||
this.targetSecurityAttributes = targetSecurityAttributes;
|
||||
this.accessDecisionManager = accessDecisionManager;
|
||||
}
|
||||
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Message<?> message = (Message<?>) invocation.getArguments()[0];
|
||||
SecurityContext ctx = SecurityContextUtils.getSecurityContextFromHeader(message);
|
||||
if (ctx != null) {
|
||||
try {
|
||||
SecurityContextHolder.setContext(ctx);
|
||||
accessDecisionManager.decide(SecurityContextHolder.getContext().getAuthentication(), invocation
|
||||
.getThis(), targetSecurityAttributes);
|
||||
return invocation.proceed();
|
||||
}
|
||||
finally {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
else {
|
||||
accessDecisionManager.decide(SecurityContextHolder.getContext().getAuthentication(), invocation.getThis(),
|
||||
targetSecurityAttributes);
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.security;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.support.StaticMethodMatcher;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class TargetSendMethodArgMatcher extends StaticMethodMatcher{
|
||||
|
||||
|
||||
public boolean matches(Method method, Class targetClass) {
|
||||
return argsTypesMatch(method.getParameterTypes());
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected boolean argsTypesMatch(Class[] args){
|
||||
if(args.length > 2){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(args.length > 0){
|
||||
if(!Message.class.isAssignableFrom(args[0] )){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length > 1 ){
|
||||
if(!long.class.isAssignableFrom(args[1])){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user