Class CurrentSecurityContextArgumentResolver
java.lang.Object
org.springframework.security.web.method.annotation.CurrentSecurityContextArgumentResolver
- All Implemented Interfaces:
org.springframework.web.method.support.HandlerMethodArgumentResolver
public final class CurrentSecurityContextArgumentResolver
extends Object
implements org.springframework.web.method.support.HandlerMethodArgumentResolver
Allows resolving the
SecurityContext
using the CurrentSecurityContext
annotation. For example, the following Controller
:
@Controller public class MyController { @RequestMapping("/im") public void security(@CurrentSecurityContext SecurityContext context) { // do something with context } }it can also support the spring SPEL expression to get the value from SecurityContext
@Controller public class MyController { @RequestMapping("/im") public void security(@CurrentSecurityContext(expression="authentication") Authentication authentication) { // do something with context } }
Will resolve the SecurityContext
argument using
SecurityContextHolder.getContext()
from the SecurityContextHolder
. If
the SecurityContext
is null
, it will return null
. If the types
do not match, null
will be returned unless
CurrentSecurityContext.errorOnInvalidType()
is true
in which case a
ClassCastException
will be thrown.
- Since:
- 5.2
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionresolveArgument
(org.springframework.core.MethodParameter parameter, org.springframework.web.method.support.ModelAndViewContainer mavContainer, org.springframework.web.context.request.NativeWebRequest webRequest, org.springframework.web.bind.support.WebDataBinderFactory binderFactory) void
setBeanResolver
(org.springframework.expression.BeanResolver beanResolver) Set theBeanResolver
to be used on the expressionsvoid
setSecurityContextHolderStrategy
(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategy
to use.void
setTemplateDefaults
(AnnotationTemplateExpressionDefaults templateDefaults) Configure CurrentSecurityContext template resolutionboolean
supportsParameter
(org.springframework.core.MethodParameter parameter)
-
Constructor Details
-
CurrentSecurityContextArgumentResolver
public CurrentSecurityContextArgumentResolver()
-
-
Method Details
-
supportsParameter
public boolean supportsParameter(org.springframework.core.MethodParameter parameter) - Specified by:
supportsParameter
in interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver
-
resolveArgument
public Object resolveArgument(org.springframework.core.MethodParameter parameter, org.springframework.web.method.support.ModelAndViewContainer mavContainer, org.springframework.web.context.request.NativeWebRequest webRequest, org.springframework.web.bind.support.WebDataBinderFactory binderFactory) - Specified by:
resolveArgument
in interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver
-
setSecurityContextHolderStrategy
public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) Sets theSecurityContextHolderStrategy
to use. The default action is to use theSecurityContextHolderStrategy
stored inSecurityContextHolder
.- Since:
- 5.8
-
setBeanResolver
public void setBeanResolver(org.springframework.expression.BeanResolver beanResolver) Set theBeanResolver
to be used on the expressions- Parameters:
beanResolver
- theBeanResolver
to use
-
setTemplateDefaults
Configure CurrentSecurityContext template resolutionBy default, this value is
null
, which indicates that templates should not be resolved.- Parameters:
templateDefaults
- - whether to resolve CurrentSecurityContext templates parameters- Since:
- 6.4
-