AuthenticationPrincipalArgumentResolver
instead.@Deprecated public final class AuthenticationPrincipalArgumentResolver extends Object implements HandlerMethodArgumentResolver
Authentication.getPrincipal()
using the
AuthenticationPrincipal
annotation. For example, the following
Controller
:
@Controller public class MyController { @RequestMapping("/user/current/show") public String show(@AuthenticationPrincipal CustomUser customUser) { // do something with CustomUser return "view"; } }
Will resolve the CustomUser argument using Authentication.getPrincipal()
from
the SecurityContextHolder
. If the Authentication
or
Authentication.getPrincipal()
is null, it will return null. If the types do not
match, null will be returned unless
AuthenticationPrincipal.errorOnInvalidType()
is true in which case a
ClassCastException
will be thrown.
Alternatively, users can create a custom meta annotation as shown below:
@Target({ ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @AuthenticationPrincipal public @interface CurrentUser { }
The custom annotation can then be used instead. For example:
@Controller public class MyController { @RequestMapping("/user/current/show") public String show(@CurrentUser CustomUser customUser) { // do something with CustomUser return "view"; } }
Constructor and Description |
---|
AuthenticationPrincipalArgumentResolver()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
Object |
resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory)
Deprecated.
|
boolean |
supportsParameter(MethodParameter parameter)
Deprecated.
|
public AuthenticationPrincipalArgumentResolver()
public boolean supportsParameter(MethodParameter parameter)
supportsParameter
in interface HandlerMethodArgumentResolver
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception
resolveArgument
in interface HandlerMethodArgumentResolver
Exception