Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Last active July 23, 2018 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaZderMind/e415be1c5e15b7c36e07df85a91bb673 to your computer and use it in GitHub Desktop.
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
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;
}
}
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;
}
}
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