11package org .ihtsdo .rvf .config ;
22
3+ import jakarta .servlet .http .HttpServletResponse ;
34import org .ihtsdo .sso .integration .RequestHeaderAuthenticationDecorator ;
45import org .springframework .context .annotation .Bean ;
56import org .springframework .context .annotation .Configuration ;
1011import org .springframework .security .web .SecurityFilterChain ;
1112import org .springframework .security .web .access .intercept .AuthorizationFilter ;
1213
13- import static org .springframework .security .config .Customizer .withDefaults ;
14-
1514@ Configuration
1615@ EnableWebSecurity
1716public class SecurityConfig {
1817
1918 @ Bean
2019 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
21- http .httpBasic (withDefaults ());
2220 http .csrf (AbstractHttpConfigurer ::disable );
2321 http .sessionManagement (sessionManagement -> sessionManagement .sessionCreationPolicy (SessionCreationPolicy .STATELESS ));
2422 http .addFilterBefore (new RequestHeaderAuthenticationDecorator (), AuthorizationFilter .class );
@@ -29,6 +27,18 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2927 .permitAll ()
3028 .anyRequest ().authenticated ()
3129 );
30+
31+ // Configure exception handling to prevent Basic Auth popup
32+ // Returns JSON response instead of triggering browser Basic Auth popup
33+ http .exceptionHandling (exceptions -> exceptions
34+ .authenticationEntryPoint ((request , response , authException ) -> {
35+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
36+ response .setContentType ("application/json;charset=UTF-8" );
37+ String message = authException .getMessage () != null ? authException .getMessage ().replace ("\" " , "\\ \" " ) : "Authentication required" ;
38+ response .getWriter ().write ("{\" error\" :\" Unauthorized\" ,\" message\" :\" " + message + "\" }" );
39+ })
40+ );
41+
3242 return http .build ();
3343 }
3444}
0 commit comments