|
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.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.method.configuration;
|
18 | 18 |
|
| 19 | +import java.lang.annotation.ElementType; |
| 20 | +import java.lang.annotation.Inherited; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
19 | 24 | import java.util.List;
|
20 | 25 |
|
21 | 26 | import jakarta.annotation.security.DenyAll;
|
22 | 27 | import jakarta.annotation.security.PermitAll;
|
23 | 28 | import jakarta.annotation.security.RolesAllowed;
|
| 29 | +import org.aopalliance.intercept.MethodInvocation; |
24 | 30 |
|
| 31 | +import org.springframework.context.ApplicationContext; |
| 32 | +import org.springframework.core.annotation.AnnotationUtils; |
| 33 | +import org.springframework.expression.EvaluationContext; |
| 34 | +import org.springframework.expression.Expression; |
25 | 35 | import org.springframework.security.access.annotation.Secured;
|
| 36 | +import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; |
26 | 37 | import org.springframework.security.access.prepost.PostAuthorize;
|
27 | 38 | import org.springframework.security.access.prepost.PostFilter;
|
28 | 39 | import org.springframework.security.access.prepost.PreAuthorize;
|
29 | 40 | import org.springframework.security.access.prepost.PreFilter;
|
| 41 | +import org.springframework.security.authorization.AuthorizationResult; |
| 42 | +import org.springframework.security.authorization.method.MethodAuthorizationDeniedHandler; |
| 43 | +import org.springframework.security.authorization.method.MethodAuthorizationDeniedPostProcessor; |
| 44 | +import org.springframework.security.authorization.method.MethodInvocationResult; |
30 | 45 | import org.springframework.security.core.Authentication;
|
| 46 | +import org.springframework.security.core.context.SecurityContextHolder; |
31 | 47 | import org.springframework.security.core.parameters.P;
|
| 48 | +import org.springframework.util.StringUtils; |
32 | 49 |
|
33 | 50 | /**
|
34 | 51 | * @author Rob Winch
|
35 | 52 | */
|
| 53 | +@MethodSecurityService.Mask("classmask") |
36 | 54 | public interface MethodSecurityService {
|
37 | 55 |
|
38 | 56 | @PreAuthorize("denyAll")
|
@@ -108,4 +126,178 @@ public interface MethodSecurityService {
|
108 | 126 | @RequireAdminRole
|
109 | 127 | void repeatedAnnotations();
|
110 | 128 |
|
| 129 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = StarMaskingHandler.class) |
| 130 | + String preAuthorizeGetCardNumberIfAdmin(String cardNumber); |
| 131 | + |
| 132 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = StartMaskingHandlerChild.class) |
| 133 | + String preAuthorizeWithHandlerChildGetCardNumberIfAdmin(String cardNumber); |
| 134 | + |
| 135 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = StarMaskingHandler.class) |
| 136 | + String preAuthorizeThrowAccessDeniedManually(); |
| 137 | + |
| 138 | + @PostAuthorize(value = "hasRole('ADMIN')", postProcessorClass = CardNumberMaskingPostProcessor.class) |
| 139 | + String postAuthorizeGetCardNumberIfAdmin(String cardNumber); |
| 140 | + |
| 141 | + @PostAuthorize(value = "hasRole('ADMIN')", postProcessorClass = PostMaskingPostProcessor.class) |
| 142 | + String postAuthorizeThrowAccessDeniedManually(); |
| 143 | + |
| 144 | + @PreAuthorize(value = "denyAll()", handlerClass = MaskAnnotationHandler.class) |
| 145 | + @Mask("methodmask") |
| 146 | + String preAuthorizeDeniedMethodWithMaskAnnotation(); |
| 147 | + |
| 148 | + @PreAuthorize(value = "denyAll()", handlerClass = MaskAnnotationHandler.class) |
| 149 | + String preAuthorizeDeniedMethodWithNoMaskAnnotation(); |
| 150 | + |
| 151 | + @NullDenied(role = "ADMIN") |
| 152 | + String postAuthorizeDeniedWithNullDenied(); |
| 153 | + |
| 154 | + @PostAuthorize(value = "denyAll()", postProcessorClass = MaskAnnotationPostProcessor.class) |
| 155 | + @Mask("methodmask") |
| 156 | + String postAuthorizeDeniedMethodWithMaskAnnotation(); |
| 157 | + |
| 158 | + @PostAuthorize(value = "denyAll()", postProcessorClass = MaskAnnotationPostProcessor.class) |
| 159 | + String postAuthorizeDeniedMethodWithNoMaskAnnotation(); |
| 160 | + |
| 161 | + @PreAuthorize(value = "hasRole('ADMIN')", handlerClass = MaskAnnotationHandler.class) |
| 162 | + @Mask(expression = "@myMasker.getMask()") |
| 163 | + String preAuthorizeWithMaskAnnotationUsingBean(); |
| 164 | + |
| 165 | + @PostAuthorize(value = "hasRole('ADMIN')", postProcessorClass = MaskAnnotationPostProcessor.class) |
| 166 | + @Mask(expression = "@myMasker.getMask(returnObject)") |
| 167 | + String postAuthorizeWithMaskAnnotationUsingBean(); |
| 168 | + |
| 169 | + class StarMaskingHandler implements MethodAuthorizationDeniedHandler { |
| 170 | + |
| 171 | + @Override |
| 172 | + public Object handle(MethodInvocation methodInvocation, AuthorizationResult result) { |
| 173 | + return "***"; |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + class StartMaskingHandlerChild extends StarMaskingHandler { |
| 179 | + |
| 180 | + @Override |
| 181 | + public Object handle(MethodInvocation methodInvocation, AuthorizationResult result) { |
| 182 | + return super.handle(methodInvocation, result) + "-child"; |
| 183 | + } |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + class MaskAnnotationHandler implements MethodAuthorizationDeniedHandler { |
| 188 | + |
| 189 | + MaskValueResolver maskValueResolver; |
| 190 | + |
| 191 | + MaskAnnotationHandler(ApplicationContext context) { |
| 192 | + this.maskValueResolver = new MaskValueResolver(context); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public Object handle(MethodInvocation methodInvocation, AuthorizationResult result) { |
| 197 | + Mask mask = AnnotationUtils.getAnnotation(methodInvocation.getMethod(), Mask.class); |
| 198 | + if (mask == null) { |
| 199 | + mask = AnnotationUtils.getAnnotation(methodInvocation.getMethod().getDeclaringClass(), Mask.class); |
| 200 | + } |
| 201 | + return this.maskValueResolver.resolveValue(mask, methodInvocation, null); |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + class MaskAnnotationPostProcessor implements MethodAuthorizationDeniedPostProcessor { |
| 207 | + |
| 208 | + MaskValueResolver maskValueResolver; |
| 209 | + |
| 210 | + MaskAnnotationPostProcessor(ApplicationContext context) { |
| 211 | + this.maskValueResolver = new MaskValueResolver(context); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public Object postProcessResult(MethodInvocationResult methodInvocationResult, |
| 216 | + AuthorizationResult authorizationResult) { |
| 217 | + MethodInvocation mi = methodInvocationResult.getMethodInvocation(); |
| 218 | + Mask mask = AnnotationUtils.getAnnotation(mi.getMethod(), Mask.class); |
| 219 | + if (mask == null) { |
| 220 | + mask = AnnotationUtils.getAnnotation(mi.getMethod().getDeclaringClass(), Mask.class); |
| 221 | + } |
| 222 | + return this.maskValueResolver.resolveValue(mask, mi, methodInvocationResult.getResult()); |
| 223 | + } |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + class MaskValueResolver { |
| 228 | + |
| 229 | + DefaultMethodSecurityExpressionHandler expressionHandler; |
| 230 | + |
| 231 | + MaskValueResolver(ApplicationContext context) { |
| 232 | + this.expressionHandler = new DefaultMethodSecurityExpressionHandler(); |
| 233 | + this.expressionHandler.setApplicationContext(context); |
| 234 | + } |
| 235 | + |
| 236 | + String resolveValue(Mask mask, MethodInvocation mi, Object returnObject) { |
| 237 | + if (StringUtils.hasText(mask.value())) { |
| 238 | + return mask.value(); |
| 239 | + } |
| 240 | + Expression expression = this.expressionHandler.getExpressionParser().parseExpression(mask.expression()); |
| 241 | + EvaluationContext evaluationContext = this.expressionHandler |
| 242 | + .createEvaluationContext(() -> SecurityContextHolder.getContext().getAuthentication(), mi); |
| 243 | + if (returnObject != null) { |
| 244 | + this.expressionHandler.setReturnObject(returnObject, evaluationContext); |
| 245 | + } |
| 246 | + return expression.getValue(evaluationContext, String.class); |
| 247 | + } |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | + class PostMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor { |
| 252 | + |
| 253 | + @Override |
| 254 | + public Object postProcessResult(MethodInvocationResult contextObject, AuthorizationResult result) { |
| 255 | + return "***"; |
| 256 | + } |
| 257 | + |
| 258 | + } |
| 259 | + |
| 260 | + class CardNumberMaskingPostProcessor implements MethodAuthorizationDeniedPostProcessor { |
| 261 | + |
| 262 | + static String MASK = "****-****-****-"; |
| 263 | + |
| 264 | + @Override |
| 265 | + public Object postProcessResult(MethodInvocationResult contextObject, AuthorizationResult result) { |
| 266 | + String cardNumber = (String) contextObject.getResult(); |
| 267 | + return MASK + cardNumber.substring(cardNumber.length() - 4); |
| 268 | + } |
| 269 | + |
| 270 | + } |
| 271 | + |
| 272 | + class NullPostProcessor implements MethodAuthorizationDeniedPostProcessor { |
| 273 | + |
| 274 | + @Override |
| 275 | + public Object postProcessResult(MethodInvocationResult methodInvocationResult, |
| 276 | + AuthorizationResult authorizationResult) { |
| 277 | + return null; |
| 278 | + } |
| 279 | + |
| 280 | + } |
| 281 | + |
| 282 | + @Target({ ElementType.METHOD, ElementType.TYPE }) |
| 283 | + @Retention(RetentionPolicy.RUNTIME) |
| 284 | + @Inherited |
| 285 | + @interface Mask { |
| 286 | + |
| 287 | + String value() default ""; |
| 288 | + |
| 289 | + String expression() default ""; |
| 290 | + |
| 291 | + } |
| 292 | + |
| 293 | + @Target({ ElementType.METHOD, ElementType.TYPE }) |
| 294 | + @Retention(RetentionPolicy.RUNTIME) |
| 295 | + @Inherited |
| 296 | + @PostAuthorize(value = "hasRole('{value}')", postProcessorClass = NullPostProcessor.class) |
| 297 | + @interface NullDenied { |
| 298 | + |
| 299 | + String role(); |
| 300 | + |
| 301 | + } |
| 302 | + |
111 | 303 | }
|
0 commit comments