Given following filter chain:
@Bean
@Order(0)
public SecurityFilterChain securityFilterChain(
HttpSecurity http,
@Qualifier(OpaqueTokenAuthorizationFilter.OPAQUE_TOKEN_AUTHORIZATION_FILTER)
OncePerRequestFilter authorizationFilter
)
http
.authorizeHttpRequests()
.anyRequest()
.authenticated()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(authorizationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
And a OncePerRequestFilter like this:
@Component(OpaqueTokenAuthorizationFilter.OPAQUE_TOKEN_AUTHORIZATION_FILTER)
@Order(Ordered.HIGHEST_PRECEDENCE)
@RequiredArgsConstructor
public class OpaqueTokenAuthorizationFilter extends OncePerRequestFilter {
public static final String OPAQUE_TOKEN_AUTHORIZATION_FILTER =
"OPAQUE_TOKEN_AUTHORIZATION_FILTER";
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
// Note it always registers autnetication in context
var auth =
UsernamePasswordAuthenticationToken.authenticated(
"test", null, List.of(new SimpleGrantedAuthority("TEST")));
auth.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(auth);
filterChain.doFilter(request, response);
}
}
All of the requests fail in Authorization filter because SecurityContextHolder
returns anonymous authentication when a generic endpoint is being hit. It is being thrown by ObservationDecisionManager
during the decision check (because the authentication object is anonymous).
The custom filter executes and sets authentication.
Is there anything else needed for the new Spring Security configuration?