Skip to content

Commit add5c56

Browse files
committed
Update AuthorizeReturnObject Jackson Docs
Now instructs to use MethodAuthorizationDeniedHandler Issue gh-14601
1 parent fd05c5a commit add5c56

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

docs/modules/ROOT/pages/servlet/authorization/method-security.adoc

+22-45
Original file line numberDiff line numberDiff line change
@@ -2200,10 +2200,10 @@ Java::
22002200
----
22012201
@RestController
22022202
public class UserController {
2203-
@Autowired
2203+
@Autowired
22042204
AuthorizationProxyFactory proxyFactory;
22052205
2206-
@GetMapping
2206+
@GetMapping
22072207
User currentUser(@AuthenticationPrincipal User user) {
22082208
return this.proxyFactory.proxy(user);
22092209
}
@@ -2227,7 +2227,7 @@ class UserController {
22272227
----
22282228
======
22292229

2230-
Finally, you will need to publish a <<custom_advice, custom interceptor>> to catch the `AccessDeniedException` thrown for each field, which you can do like so:
2230+
You will need to <<fallback-values-authorization-denied,add a `MethodAuthorizationDeniedHandler`>> like this one:
22312231

22322232
[tabs]
22332233
======
@@ -2236,32 +2236,18 @@ Java::
22362236
[source,java,role="primary"]
22372237
----
22382238
@Component
2239-
public class AccessDeniedExceptionInterceptor implements AuthorizationAdvisor {
2240-
private final AuthorizationAdvisor advisor = AuthorizationManagerBeforeMethodInterceptor.preAuthorize();
2241-
2242-
@Override
2243-
public Object invoke(MethodInvocation invocation) throws Throwable {
2244-
try {
2245-
return invocation.proceed();
2246-
} catch (AccessDeniedException ex) {
2247-
return null;
2248-
}
2249-
}
2250-
2251-
@Override
2252-
public Pointcut getPointcut() {
2253-
return this.advisor.getPointcut();
2254-
}
2239+
public class Null implements MethodAuthorizationDeniedHandler {
2240+
@Override
2241+
public Object handleDeniedInvocation(MethodInvocation methodInvocation, AuthorizationResult authorizationResult) {
2242+
return null;
2243+
}
2244+
}
22552245
2256-
@Override
2257-
public Advice getAdvice() {
2258-
return this;
2259-
}
2246+
// ...
22602247
2261-
@Override
2262-
public int getOrder() {
2263-
return this.advisor.getOrder() - 1;
2264-
}
2248+
@HandleAuthorizationDenied(handlerClass = Null.class)
2249+
public class User {
2250+
...
22652251
}
22662252
----
22672253
@@ -2270,26 +2256,17 @@ Kotlin::
22702256
[source,kotlin,role="secondary"]
22712257
----
22722258
@Component
2273-
class AccessDeniedExceptionInterceptor: AuthorizationAdvisor {
2274-
var advisor: AuthorizationAdvisor = AuthorizationManagerBeforeMethodInterceptor.preAuthorize()
2275-
2276-
@Throws(Throwable::class)
2277-
fun invoke(invocation: MethodInvocation): Any? {
2278-
return try {
2279-
invocation.proceed()
2280-
} catch (ex:AccessDeniedException) {
2281-
null
2282-
}
2259+
class Null : MethodAuthorizationDeniedHandler {
2260+
override fun handleDeniedInvocation(methodInvocation: MethodInvocation?, authorizationResult: AuthorizationResult?): Any? {
2261+
return null
22832262
}
2263+
}
22842264
2285-
val pointcut: Pointcut
2286-
get() = advisor.getPointcut()
2287-
2288-
val advice: Advice
2289-
get() = this
2265+
// ...
22902266
2291-
val order: Int
2292-
get() = advisor.getOrder() - 1
2267+
@HandleAuthorizationDenied(handlerClass = Null.class)
2268+
open class User {
2269+
...
22932270
}
22942271
----
22952272
======
@@ -2317,7 +2294,7 @@ And if they do have that authority, they'll see:
23172294

23182295
[TIP]
23192296
====
2320-
You can also add the Spring Boot property `spring.jackson.default-property-inclusion=non_null` to exclude the null value, if you also don't want to reveal the JSON key to an unauthorized user.
2297+
You can also add the Spring Boot property `spring.jackson.default-property-inclusion=non_null` to exclude the null value from serialization, if you also don't want to reveal the JSON key to an unauthorized user.
23212298
====
23222299

23232300
[[fallback-values-authorization-denied]]

0 commit comments

Comments
 (0)