osiągnąłem że wdrażając własne AccessDecisionManager
że delegaci dostęp decyzji do mojego specjalnego interfejsu AccessDecisionStrategy
:
public interface AccessDecisionStrategy {
void decide(Authentication authentication, MethodInvocation methodInvocation, ConfigAttribute configAttribute);
}
Każdy dostęp strategia decyzja reprezentuje inny sposób podejmowania decyzji dostępu.
można łatwo zaimplementować własną strategię (nawet w innym języku - na przykład Scala):
public class SomeStrategy implements AccessDecisionStrategy { ...
Jak widać, mój AccessDecisionManager
ma mapę strategii. Strategia używana przez menedżera opiera się na argumencie adnotacji.
public class MethodSecurityAccessDecisionManager implements AccessDecisionManager {
private Map<String, AccessDecisionStrategy> strategyMap;
public MethodSecurityAccessDecisionManager(Map<String, AccessDecisionStrategy> strategyMap) {
this.strategyMap = strategyMap;
}
@Override
public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
ConfigAttribute configAttribute = getSingleConfigAttribute(configAttributes);
AccessDecisionStrategy accessDecisionStrategy = strategyMap.get(configAttribute.getAttribute());
if (accessDecisionStrategy == null) {
throw new IllegalStateException("AccessDecisionStrategy with name "
+ configAttribute.getAttribute() + " was not found!");
}
try {
accessDecisionStrategy.decide(authentication, (MethodInvocation) object, configAttribute);
} catch (ClassCastException e) {
throw new IllegalStateException();
}
}
private ConfigAttribute getSingleConfigAttribute(Collection<ConfigAttribute> configAttributes) {
if (configAttributes == null || configAttributes.size() != 1) {
throw new IllegalStateException("Invalid config attribute configuration");
}
return configAttributes.iterator().next();
}
@Override
public boolean supports(ConfigAttribute attribute) {
return true;
}
@Override
public boolean supports(Class<?> clazz) {
return clazz.equals(MethodInvocation.class);
}
}
Teraz, gdy chcę chronić mój sposób mogę umieścić @Secured
adnotacji z argumentem, że to nazwa strategii:
@Secured("GetByOwner")
FlightSpotting getFlightSpotting(Long id);
można zaimplementować i skonfigurować jak wiele strategii, jak chcesz:
<bean id="methodSecurityAccessDecisionManager"
class="some.package.MethodSecurityAccessDecisionManager">
<constructor-arg>
<map>
<entry key="GetByOwner">
<bean class="some.package.GetByOwnerStrategy"/>
</entry>
<entry key="SomeOther">
<bean class="some.package.SomeOtherStrategy"/>
</entry>
</map>
</constructor-arg>
</bean>
Aby wprowadzić ten menedżer decyzji dotyczącej dostępu, należy wpisać:
<sec:global-method-security secured-annotations="enabled"
access-decision-manager-ref="methodSecurityAccessDecisionManager">
</sec:global-method-security>
Ja również realizowany klasa pomocnika do obsługi MethodInvocation
argumenty:
import org.aopalliance.intercept.MethodInvocation;
public class MethodInvocationExtractor<ArgumentType> {
private MethodInvocation methodInvocation;
public MethodInvocationExtractor(MethodInvocation methodInvocation) {
this.methodInvocation = methodInvocation;
}
public ArgumentType getArg(int num) {
try {
Object[] arguments = methodInvocation.getArguments();
return (ArgumentType) arguments[num];
} catch (ClassCastException | ArrayIndexOutOfBoundsException e) {
throw new IllegalStateException();
}
}
}
Teraz można łatwo wyodrębnić ciekawe argumenty w kodzie swojej strategii do podjęcia decyzji:
Powiedzmy chcę uzyskać liczbę argumentów 0
że jest typu Long
:
MethodInvocationExtractor<Long> extractor = new MethodInvocationExtractor<>(methodInvocation);
Long id = extractor.getArg(0);
+1 dla wskaźnika do oddania obiektu do MethodInvocation w zadecydować(). W dokumentacji Spring brakuje tej ważnej informacji ... –
Jeśli ktoś jej potrzebuje, dodałem, jak to zaimplementować za pomocą Java Config (mój projekt używa Spring-Boot) [w tym nowym pytaniu] (http: // stackoverflow. com/q/24983046/2796922). Zobacz moją odpowiedź. – elysch
W wielu przypadkach nie ma potrzeby wdrażania własnego menedżera decyzyjnego. Zamiast tego możesz po prostu napisać metodę i wywołać ją bezpośrednio z '@ Preauthorize', zobacz tę odpowiedź: http://stackoverflow.com/a/39972346/1169324 –