- * See Securing The Eureka Server - */ - @Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http.csrf(csrf -> csrf.ignoringRequestMatchers("/eureka/**")); - http.authorizeHttpRequests(authz -> authz.requestMatchers("/eureka/**").authenticated()) - .httpBasic(Customizer.withDefaults()); - return http.build(); + /** Implements BASIC instead of spring-security + CORS, CSRF and management exclusions. */ + static final class BasicAuthFilter extends OncePerRequestFilter { + final String expectedAuthorization; + + BasicAuthFilter(String username, String password) { + expectedAuthorization = + "Basic " + Base64.getEncoder().encodeToString((username + ':' + password).getBytes(UTF_8)); + } + + @Override protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, + FilterChain chain) throws ServletException, IOException { + String authHeader = req.getHeader("Authorization"); + if (expectedAuthorization.equals(authHeader)) { + chain.doFilter(req, res); // Pass on the supplied credentials + return; + } + res.setHeader("WWW-Authenticate", "Basic realm=\"Realm'\""); + res.sendError(HttpServletResponse.SC_UNAUTHORIZED); // Return 401 otherwise. + } } }