feat: refactor way to get CallerId
All checks were successful
Release / release (push) Successful in 1m54s
All checks were successful
Release / release (push) Successful in 1m54s
This commit is contained in:
@@ -1,28 +1,15 @@
|
||||
package fr.lucasdupont.security;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.jwt.Jwt;
|
||||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
||||
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 org.springframework.stereotype.Component;
|
||||
|
||||
public class CallerIdArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return CallerId.class.isAssignableFrom(parameter.getParameterType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter,
|
||||
ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest,
|
||||
WebDataBinderFactory binderFactory) throws IllegalAccessException {
|
||||
@Component
|
||||
public class CallerIdResolver {
|
||||
|
||||
public CallerId get() throws IllegalAccessException {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (!(auth instanceof JwtAuthenticationToken jwtAuth)) {
|
||||
throw new IllegalAccessException("Authentication is not of type JwtAuthenticationToken");
|
||||
@@ -44,4 +31,5 @@ public class CallerIdArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
return new CallerId(sub, CallerId.Type.USER);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,10 +9,6 @@ import org.springframework.security.config.annotation.web.configurers.AbstractHt
|
||||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnClass(HttpSecurity.class)
|
||||
@@ -47,19 +43,4 @@ public class KeycloakAutoConfiguration {
|
||||
return new RequiredRoleFilter(props);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebMvcConfigurer callerIdArgumentResolverConfigurer(CallerIdArgumentResolver resolver) {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
||||
resolvers.add(resolver);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CallerIdArgumentResolver callerIdArgumentResolver() {
|
||||
return new CallerIdArgumentResolver();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user