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;
|
package fr.lucasdupont.security;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.oauth2.jwt.Jwt;
|
import org.springframework.security.oauth2.jwt.Jwt;
|
||||||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
|
||||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.context.request.NativeWebRequest;
|
|
||||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
||||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
|
||||||
|
|
||||||
public class CallerIdArgumentResolver implements HandlerMethodArgumentResolver {
|
@Component
|
||||||
|
public class CallerIdResolver {
|
||||||
@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 {
|
|
||||||
|
|
||||||
|
public CallerId get() throws IllegalAccessException {
|
||||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||||
if (!(auth instanceof JwtAuthenticationToken jwtAuth)) {
|
if (!(auth instanceof JwtAuthenticationToken jwtAuth)) {
|
||||||
throw new IllegalAccessException("Authentication is not of type JwtAuthenticationToken");
|
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);
|
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.oauth2.server.resource.authentication.JwtAuthenticationConverter;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
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
|
@AutoConfiguration
|
||||||
@ConditionalOnClass(HttpSecurity.class)
|
@ConditionalOnClass(HttpSecurity.class)
|
||||||
@@ -47,19 +43,4 @@ public class KeycloakAutoConfiguration {
|
|||||||
return new RequiredRoleFilter(props);
|
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