Polish method-security.adoc

This commit is contained in:
Steve Riesenberg 2023-11-30 12:00:05 -06:00
parent 6e1605314a
commit 57f7eff568
No known key found for this signature in database
GPG Key ID: 5F311AB48A55D521
1 changed files with 18 additions and 16 deletions

View File

@ -1214,9 +1214,9 @@ Kotlin::
companion object { companion object {
@Bean @Bean
fun methodSecurityExpressionHandler(val roleHierarchy: RoleHierarchy) : MethodSecurityExpressionHandler { fun methodSecurityExpressionHandler(val roleHierarchy: RoleHierarchy) : MethodSecurityExpressionHandler {
val handler = DefaultMethodSecurityExpressionHandler(); val handler = DefaultMethodSecurityExpressionHandler()
handler.setRoleHierarchy(roleHierarchy); handler.setRoleHierarchy(roleHierarchy)
return handler; return handler
} }
} }
---- ----
@ -1260,14 +1260,14 @@ Java::
+ +
[source,java,role="primary"] [source,java,role="primary"]
---- ----
import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole; import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole
@Bean @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static Advisor protectServicePointcut() { static Advisor protectServicePointcut() {
AspectJExpressionPointcut pattern = new AspectJExpressionPointcut(); AspectJExpressionPointcut pattern = new AspectJExpressionPointcut()
pattern.setExpression("execution(* com.mycompany.*Service.*(..))"); pattern.setExpression("execution(* com.mycompany.*Service.*(..))")
return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER")); return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER"))
} }
---- ----
@ -1275,26 +1275,28 @@ Kotlin::
+ +
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole; import static org.springframework.security.authorization.AuthorityAuthorizationManager.hasRole
companion object { companion object {
@Bean @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
fun protectServicePointcut(): Advisor { fun protectServicePointcut(): Advisor {
val pattern = AspectJExpressionPointcut(); val pattern = AspectJExpressionPointcut()
pattern.setExpression("execution(* com.mycompany.*Service.*(..))"); pattern.setExpression("execution(* com.mycompany.*Service.*(..))")
return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER")); return new AuthorizationManagerBeforeMethodInterceptor(pattern, hasRole("USER"))
} }
} }
---- ----
======
[source,xml] Xml::
+
[source,xml,role="secondary"]
---- ----
<sec:method-security> <sec:method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="hasRole('USER')"/> <protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="hasRole('USER')"/>
</sec:method-security> </sec:method-security>
---- ----
======
[[weave-aspectj]] [[weave-aspectj]]
=== Integrate with AspectJ Byte-weaving === Integrate with AspectJ Byte-weaving
@ -1445,7 +1447,7 @@ open class MyService {
fun readResource(...): MyResource fun readResource(...): MyResource
@PreAuthorize("@authz.check(#root)") @PreAuthorize("@authz.check(#root)")
fun shareResource(...): MyResource; fun shareResource(...): MyResource
} }
---- ----
@ -1769,8 +1771,8 @@ class MyExpressionHandler: DefaultMethodSecurityExpressionHandler {
val context = super.createEvaluationContext(authentication, mi) as StandardEvaluationContext val context = super.createEvaluationContext(authentication, mi) as StandardEvaluationContext
val delegate = context.getRootObject().getValue() as MethodSecurityExpressionOperations val delegate = context.getRootObject().getValue() as MethodSecurityExpressionOperations
val root = MySecurityExpressionRoot(delegate) val root = MySecurityExpressionRoot(delegate)
context.setRootObject(root); context.setRootObject(root)
return context; return context
} }
} }
---- ----