|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import jakarta.annotation.security.DenyAll;
|
22 | 22 | import jakarta.annotation.security.PermitAll;
|
23 | 23 | import jakarta.annotation.security.RolesAllowed;
|
| 24 | +import org.aopalliance.intercept.MethodInvocation; |
24 | 25 |
|
25 | 26 | import org.springframework.security.access.annotation.Secured;
|
26 | 27 | import org.springframework.security.access.prepost.PostAuthorize;
|
27 | 28 | import org.springframework.security.access.prepost.PostFilter;
|
28 | 29 | import org.springframework.security.access.prepost.PreAuthorize;
|
29 | 30 | import org.springframework.security.access.prepost.PreFilter;
|
| 31 | +import org.springframework.security.authorization.AuthorizationDecision; |
| 32 | +import org.springframework.security.authorization.method.MethodAccessDeniedHandler; |
| 33 | +import org.springframework.security.authorization.method.MethodInvocationResult; |
30 | 34 | import org.springframework.security.core.Authentication;
|
31 | 35 | import org.springframework.security.core.parameters.P;
|
32 | 36 |
|
@@ -108,4 +112,59 @@ public interface MethodSecurityService {
|
108 | 112 | @RequireAdminRole
|
109 | 113 | void repeatedAnnotations();
|
110 | 114 |
|
| 115 | + @PostAuthorize(value = "hasRole('ADMIN')", handlerClass = CardNumberMaskingHandler.class) |
| 116 | + String postAuthorizeGetCardNumberIfAdmin(String cardNumber); |
| 117 | + |
| 118 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = MaskingHandler.class) |
| 119 | + String preAuthorizeGetCardNumberIfAdmin(String cardNumber); |
| 120 | + |
| 121 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = MaskingHandlerChild.class) |
| 122 | + String preAuthorizeWithHandlerChildGetCardNumberIfAdmin(String cardNumber); |
| 123 | + |
| 124 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = MaskingHandler.class) |
| 125 | + String preAuthorizeThrowAccessDeniedManually(); |
| 126 | + |
| 127 | + @PostAuthorize(value = "hasRole('ADMIN')", handlerClass = PostMaskingHandler.class) |
| 128 | + String postAuthorizeThrowAccessDeniedManually(); |
| 129 | + |
| 130 | + class MaskingHandler implements MethodAccessDeniedHandler<MethodInvocation> { |
| 131 | + |
| 132 | + @Override |
| 133 | + public Object handle(MethodInvocation deniedObject, AuthorizationDecision decision) { |
| 134 | + return "***"; |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + class MaskingHandlerChild extends MaskingHandler { |
| 140 | + |
| 141 | + @Override |
| 142 | + public Object handle(MethodInvocation deniedObject, AuthorizationDecision decision) { |
| 143 | + Object mask = super.handle(deniedObject, decision); |
| 144 | + return mask + "-child"; |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + class PostMaskingHandler implements MethodAccessDeniedHandler<MethodInvocationResult> { |
| 150 | + |
| 151 | + @Override |
| 152 | + public Object handle(MethodInvocationResult deniedObject, AuthorizationDecision decision) { |
| 153 | + return "***"; |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + class CardNumberMaskingHandler implements MethodAccessDeniedHandler<MethodInvocationResult> { |
| 159 | + |
| 160 | + static String MASK = "****-****-****-"; |
| 161 | + |
| 162 | + @Override |
| 163 | + public Object handle(MethodInvocationResult mi, AuthorizationDecision decision) { |
| 164 | + String cardNumber = (String) mi.getResult(); |
| 165 | + return MASK + cardNumber.substring(cardNumber.length() - 4); |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
111 | 170 | }
|
0 commit comments