Skip to content

Commit 9a11229

Browse files
committed
PIP-908 Tweak security config after switching to new spring-boot version 3.5.7
1 parent 413c3df commit 9a11229

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/main/java/org/ihtsdo/rvf/config/SecurityConfig.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ihtsdo.rvf.config;
22

3+
import jakarta.servlet.http.HttpServletResponse;
34
import org.ihtsdo.sso.integration.RequestHeaderAuthenticationDecorator;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
@@ -10,15 +11,12 @@
1011
import org.springframework.security.web.SecurityFilterChain;
1112
import org.springframework.security.web.access.intercept.AuthorizationFilter;
1213

13-
import static org.springframework.security.config.Customizer.withDefaults;
14-
1514
@Configuration
1615
@EnableWebSecurity
1716
public 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

Comments
 (0)