|
16 | 16 | package software.xdev.sse.web.sidecar.actuator; |
17 | 17 |
|
18 | 18 | import java.lang.reflect.Field; |
| 19 | +import java.lang.reflect.Method; |
19 | 20 |
|
20 | 21 | import jakarta.servlet.http.HttpServletRequest; |
21 | 22 | import jakarta.servlet.http.HttpServletResponse; |
22 | 23 |
|
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
23 | 26 | import org.springframework.http.HttpStatus; |
24 | 27 | import org.springframework.security.config.annotation.web.HttpSecurityBuilder; |
25 | 28 | import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer; |
26 | 29 | import org.springframework.security.core.AuthenticationException; |
27 | 30 | import org.springframework.security.web.AuthenticationEntryPoint; |
28 | 31 | import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint; |
| 32 | +import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
29 | 33 | import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint; |
| 34 | +import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher; |
30 | 35 |
|
31 | 36 |
|
32 | 37 | public class NoErrorBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint |
33 | 38 | { |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(NoErrorBasicAuthenticationEntryPoint.class); |
| 40 | + |
34 | 41 | protected NoErrorBasicAuthenticationEntryPoint() |
35 | 42 | { |
36 | 43 | } |
@@ -76,7 +83,27 @@ public static <B extends HttpSecurityBuilder<B>> HttpBasicConfigurer<B> install( |
76 | 83 |
|
77 | 84 | if(authenticationEntryPoint instanceof final DelegatingAuthenticationEntryPoint delegatingAuthEntryPoint) |
78 | 85 | { |
79 | | - delegatingAuthEntryPoint.setDefaultEntryPoint(entryPoint); |
| 86 | + try |
| 87 | + { |
| 88 | + final Method mSetDefaultEntryPoint = DelegatingAuthenticationEntryPoint.class.getDeclaredMethod( |
| 89 | + "setDefaultEntryPoint", |
| 90 | + AuthenticationEntryPoint.class); |
| 91 | + mSetDefaultEntryPoint.setAccessible(true); |
| 92 | + mSetDefaultEntryPoint.invoke(delegatingAuthEntryPoint, entryPoint); |
| 93 | + } |
| 94 | + catch(final Exception ex) |
| 95 | + { |
| 96 | + LOG.debug("setDefaultEntryPoint failed (Spring Boot API changed?) - Trying to use fallback", ex); |
| 97 | + |
| 98 | + // Replace entire implementation |
| 99 | + httpBasicConfigurer.authenticationEntryPoint( |
| 100 | + DelegatingAuthenticationEntryPoint.builder() |
| 101 | + .addEntryPointFor( |
| 102 | + new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), |
| 103 | + new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest")) |
| 104 | + .defaultEntryPoint(entryPoint) |
| 105 | + .build()); |
| 106 | + } |
80 | 107 | } |
81 | 108 | } |
82 | 109 | catch(final NoSuchFieldException | IllegalAccessException e) |
|
0 commit comments