Last active
July 23, 2018 19:16
-
-
Save MaZderMind/e415be1c5e15b7c36e07df85a91bb673 to your computer and use it in GitHub Desktop.
A Variant of the AuthenticationPrincipalArgumentResolver which injects an AuthenticationPrincipal merged into the correct EntityManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.seibertmedia.team-rocket.someproject.configuration.mergedAuthenticationPrincipal; | |
import java.lang.annotation.Annotation; | |
import org.springframework.core.MethodParameter; | |
import org.springframework.core.annotation.AnnotationUtils; | |
public class AnnotationUtil { | |
private AnnotationUtil() { | |
} | |
/** | |
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}. | |
* | |
* @param annotationClass the class of the {@link Annotation} to find on the | |
* {@link MethodParameter} | |
* @param parameter the {@link MethodParameter} to search for an {@link Annotation} | |
* @return the {@link Annotation} that was found or null. | |
*/ | |
public static <T extends Annotation> T findMethodAnnotation(Class<T> annotationClass, | |
MethodParameter parameter) { | |
T annotation = parameter.getParameterAnnotation(annotationClass); | |
if (annotation != null) { | |
return annotation; | |
} | |
Annotation[] annotationsToSearch = parameter.getParameterAnnotations(); | |
for (Annotation toSearch : annotationsToSearch) { | |
annotation = AnnotationUtils.findAnnotation(toSearch.annotationType(), | |
annotationClass); | |
if (annotation != null) { | |
return annotation; | |
} | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.seibertmedia.team-rocket.someproject.configuration.mergedAuthenticationPrincipal; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.MethodParameter; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.annotation.AuthenticationPrincipal; | |
import org.springframework.security.core.context.SecurityContextHolder; | |
import org.springframework.stereotype.Component; | |
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; | |
import net.seibertmedia.team-rocket.someproject.model.security.User; | |
import net.seibertmedia.team-rocket.someproject.model.security.UserRepository; | |
import net.seibertmedia.team-rocket.someproject.security.SomeProjectNotAuthenticatedException; | |
@Component | |
public class MergedAuthenticationPrincipalArgumentResolver implements HandlerMethodArgumentResolver { | |
@Autowired | |
private UserRepository userRepository; | |
public Object resolveArgument( | |
MethodParameter parameter, | |
ModelAndViewContainer mavContainer, NativeWebRequest webRequest, | |
WebDataBinderFactory binderFactory | |
) throws Exception { | |
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | |
if (authentication == null) { | |
return null; | |
} | |
User principal = (User) authentication.getPrincipal(); | |
AuthenticationPrincipal authPrincipal = AnnotationUtil.findMethodAnnotation(AuthenticationPrincipal.class, parameter); | |
if (principal == null) { | |
throw new SomeProjectNotAuthenticatedException(); | |
} | |
if (!parameter.getParameterType().isAssignableFrom(principal.getClass())) { | |
throw new ClassCastException(principal + " is not assignable to " + parameter.getParameterType()); | |
} | |
return userRepository.findById(principal.getId()) | |
.orElseThrow(SomeProjectNotAuthenticatedException::new); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver# | |
* supportsParameter (org.springframework.core.MethodParameter) | |
*/ | |
public boolean supportsParameter(MethodParameter parameter) { | |
return AnnotationUtil.findMethodAnnotation(AuthenticationPrincipal.class, parameter) != null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.seibertmedia.team-rocket.someproject.configuration.mergedAuthenticationPrincipal; | |
import java.util.List; | |
import javax.persistence.EntityManagerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
@Configuration | |
public class MergedAuthenticationPrincipalArgumentResolverConfigurer implements WebMvcConfigurer { | |
@Autowired | |
private EntityManagerFactory entityManagerFactory; | |
@Override | |
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { | |
argumentResolvers.add(0, new MergedAuthenticationPrincipalArgumentResolver(entityManagerFactory)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment