All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.yourinspiration.spring.jwt.JwtSubjectArgumentResolver Maven / Gradle / Ivy

The newest version!
package de.yourinspiration.spring.jwt;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * Injects the current JWT subject into a method argument.
 * 
 * @author Marcel Härle - [email protected]
 * 
 */
public class JwtSubjectArgumentResolver implements HandlerMethodArgumentResolver {

    private final JwtService jwtService;

    /**
     * Constructs a new object.
     * 
     * @param jwtService
     *            the JWT service, must not be null
     */
    public JwtSubjectArgumentResolver(final JwtService jwtService) {
        if (jwtService == null) {
            throw new IllegalArgumentException("jwtService must not be null");
        }
        this.jwtService = jwtService;
    }

    @Override
    public boolean supportsParameter(final MethodParameter methodParameter) {
        return methodParameter.getParameterAnnotation(JwtSubject.class) != null
                && methodParameter.getParameterType().equals(String.class);
    }

    @Override
    public Object resolveArgument(final MethodParameter parameter, final ModelAndViewContainer mavContainer,
            final NativeWebRequest webRequest, final WebDataBinderFactory binderFactory) {
        if (webRequest != null
                && webRequest.getAttribute(jwtService.getRequestAttribute(), NativeWebRequest.SCOPE_REQUEST) != null) {
            return webRequest.getAttribute(jwtService.getRequestAttribute(), NativeWebRequest.SCOPE_REQUEST);
        } else {
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy