Search in sources :

Example 1 with AuthenticationPrincipal

use of org.springframework.security.web.bind.annotation.AuthenticationPrincipal in project spring-security by spring-projects.

the class AuthenticationPrincipalArgumentResolver method resolveArgument.

/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.web.method.support.HandlerMethodArgumentResolver#
	 * resolveArgument (org.springframework.core.MethodParameter,
	 * org.springframework.web.method.support.ModelAndViewContainer,
	 * org.springframework.web.context.request.NativeWebRequest,
	 * org.springframework.web.bind.support.WebDataBinderFactory)
	 */
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        return null;
    }
    Object principal = authentication.getPrincipal();
    if (principal != null && !parameter.getParameterType().isAssignableFrom(principal.getClass())) {
        AuthenticationPrincipal authPrincipal = findMethodAnnotation(AuthenticationPrincipal.class, parameter);
        if (authPrincipal.errorOnInvalidType()) {
            throw new ClassCastException(principal + " is not assignable to " + parameter.getParameterType());
        } else {
            return null;
        }
    }
    return principal;
}
Also used : Authentication(org.springframework.security.core.Authentication) AuthenticationPrincipal(org.springframework.security.web.bind.annotation.AuthenticationPrincipal)

Aggregations

Authentication (org.springframework.security.core.Authentication)1 AuthenticationPrincipal (org.springframework.security.web.bind.annotation.AuthenticationPrincipal)1