ich erreicht, dass meine eigene durch die Umsetzung AccessDecisionManager
, dass die Delegierten Entscheidungen auf meine spezielle Schnittstelle zugreifen AccessDecisionStrategy
:
public interface AccessDecisionStrategy {
void decide(Authentication authentication, MethodInvocation methodInvocation, ConfigAttribute configAttribute);
}
Jede Zugriffsentscheidungsstrategie stellt unterschiedliche Art und Weise Zugang Entscheidung zu treffen.
Sie können Ihre eigene Strategie (auch in einer anderen Sprache - zum Beispiel Scala) leicht implementieren:
public class SomeStrategy implements AccessDecisionStrategy { ...
Wie Sie sehen können, mein AccessDecisionManager
hat eine Karte von Strategien. Die vom Manager verwendete Strategie basiert auf dem Annotationsargument.
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);
}
}
Nun, wenn ich möchte, dass meine Methode, die ich @Secured
Anmerkung mit dem Argument setzen schützen, die Namen der Strategie ist:
@Secured("GetByOwner")
FlightSpotting getFlightSpotting(Long id);
Sie implementieren können und so viele Strategien konfigurieren, wie Sie wollen:
<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>
Zum Eingeben des Zugriffsentscheidungsmanagers geben Sie Folgendes ein:
<sec:global-method-security secured-annotations="enabled"
access-decision-manager-ref="methodSecurityAccessDecisionManager">
</sec:global-method-security>
ich auch implementiert Hilfsklasse MethodInvocation
Argumente behandeln:
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();
}
}
}
Jetzt können Sie leicht interessante Argumente im Code Ihrer Strategie extrahieren machen Entscheidung:
Sagen wir, ich möchte Argument Nummer bekommen 0
, die vom Typ ist Long
:
MethodInvocationExtractor<Long> extractor = new MethodInvocationExtractor<>(methodInvocation);
Long id = extractor.getArg(0);
+1 für den Zeiger Objekt zu MethodInvocation werfen in entscheiden(). Die Spring-Dokumentation fehlt das wichtige Stück Info ... –
Falls jemand es benötigt, habe ich hinzugefügt, wie man dies mit Java Config (mein Projekt verwendet Spring-Boot) [in dieser neuen Frage] (http: // stackoverflow. com/q/24983046/2796922). Siehe meine Antwort. – elysch
Für viele Anwendungsfälle muss kein eigener Decisionmanager implementiert werden. Stattdessen können Sie einfach eine Methode schreiben und sie direkt von '@ Preauthorize' aufrufen, siehe folgende Antwort: http://stackoverflow.com/a/39972346/1169324 –