parent
4661b22589
commit
7a8faf85d9
|
@ -1070,7 +1070,7 @@ It also has access to the full Java language.
|
||||||
[[custom-authorization-managers]]
|
[[custom-authorization-managers]]
|
||||||
=== Using a Custom Authorization Manager
|
=== Using a Custom Authorization Manager
|
||||||
|
|
||||||
The second way to authorize a method programmatically is two create a custom xref:servlet/authorization/architecture.adoc#_the_authorizationmanager[`AuthorizationManager`].
|
The second way to authorize a method programmatically is to create a custom xref:servlet/authorization/architecture.adoc#_the_authorizationmanager[`AuthorizationManager`].
|
||||||
|
|
||||||
First, declare an authorization manager instance, perhaps like this one:
|
First, declare an authorization manager instance, perhaps like this one:
|
||||||
|
|
||||||
|
@ -1081,10 +1081,16 @@ Java::
|
||||||
[source,java,role="primary"]
|
[source,java,role="primary"]
|
||||||
----
|
----
|
||||||
@Component
|
@Component
|
||||||
public class MyAuthorizationManager implements AuthorizationManager<MethodInvocation> {
|
public class MyAuthorizationManager implements AuthorizationManager<MethodInvocation>, AuthorizationManager<MethodInvocationResult> {
|
||||||
|
@Override
|
||||||
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation invocation) {
|
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocation invocation) {
|
||||||
// ... authorization logic
|
// ... authorization logic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthorizationDecision check(Supplier<Authentication> authentication, MethodInvocationResult invocation) {
|
||||||
|
// ... authorization logic
|
||||||
|
}
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
@ -1092,9 +1098,13 @@ Kotlin::
|
||||||
+
|
+
|
||||||
[source,kotlin,role="secondary"]
|
[source,kotlin,role="secondary"]
|
||||||
----
|
----
|
||||||
@Component("authz")
|
@Component
|
||||||
open class MyAuthorizationManager: AuthorizationManager<MethodInvocation> {
|
class MyAuthorizationManager : AuthorizationManager<MethodInvocation>, AuthorizationManager<MethodInvocationResult> {
|
||||||
fun check(val authentication: Supplier<Authentication>, val invocation: MethodInvocation): AuthorizationDecision {
|
override fun check(authentication: Supplier<Authentication>, invocation: MethodInvocation): AuthorizationDecision {
|
||||||
|
// ... authorization logic
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun check(authentication: Supplier<Authentication>, invocation: MethodInvocationResult): AuthorizationDecision {
|
||||||
// ... authorization logic
|
// ... authorization logic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1104,7 +1114,7 @@ open class MyAuthorizationManager: AuthorizationManager<MethodInvocation> {
|
||||||
Then, publish the method interceptor with a pointcut that corresponds to when you want that `AuthorizationManager` to run.
|
Then, publish the method interceptor with a pointcut that corresponds to when you want that `AuthorizationManager` to run.
|
||||||
For example, you could replace how `@PreAuthorize` and `@PostAuthorize` work like so:
|
For example, you could replace how `@PreAuthorize` and `@PostAuthorize` work like so:
|
||||||
|
|
||||||
.Only @PostAuthorize Configuration
|
.Only @PreAuthorize and @PostAuthorize Configuration
|
||||||
[tabs]
|
[tabs]
|
||||||
======
|
======
|
||||||
Java::
|
Java::
|
||||||
|
@ -1116,7 +1126,7 @@ Java::
|
||||||
class MethodSecurityConfig {
|
class MethodSecurityConfig {
|
||||||
@Bean
|
@Bean
|
||||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||||
Advisor postAuthorize(MyAuthorizationManager manager) {
|
Advisor preAuthorize(MyAuthorizationManager manager) {
|
||||||
return AuthorizationManagerBeforeMethodInterceptor.preAuthorize(manager);
|
return AuthorizationManagerBeforeMethodInterceptor.preAuthorize(manager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1157,7 +1167,7 @@ Xml::
|
||||||
|
|
||||||
<aop:config/>
|
<aop:config/>
|
||||||
|
|
||||||
<bean id="postAuthorize"
|
<bean id="preAuthorize"
|
||||||
class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
|
class="org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor"
|
||||||
factory-method="preAuthorize">
|
factory-method="preAuthorize">
|
||||||
<constructor-arg ref="myAuthorizationManager"/>
|
<constructor-arg ref="myAuthorizationManager"/>
|
||||||
|
|
Loading…
Reference in New Issue