ISE-ComProg-After-Midterm/screwdriver-3.5.6/box/snippets/java/LoggedUserMethodArgumentRes...

46 lines
1.6 KiB
Java
Raw Permalink Normal View History

2020-10-29 09:38:56 +00:00
package +zoccolo+.web.resolver;
import +zoccolo+.security.Belonger;
import +zoccolo+.web.annotation.LoggedUser;
import java.security.Principal;
import org.springframework.core.MethodParameter;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebArgumentResolver;
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;
@Component
public class LoggedUserMethodArgumentResolver
implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return
methodParameter.getParameterAnnotation(LoggedUser.class) != null
&& methodParameter.getParameterType().equals(Belonger.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
if (this.supportsParameter(methodParameter)) {
Principal principal = webRequest.getUserPrincipal();
return principal==null?null:(Belonger) ((Authentication) principal).getPrincipal();
}
else {
return WebArgumentResolver.UNRESOLVED;
}
}
/* screwdriver_knife */
}