When using basic auth, the thing, that triggers the login dialog of the
browser is the existence of a WWW-Authenticate
header in the 401 response. So your goal here is to remove that.
With a default spring security setup the reason the headers gets sent is
BasicAuthenticationEntryPoint.
Since this also shows the realm to the user, here are hooks in the basic auth
configurer to replace the entrypoint (where BasicAuthenticationEntryPoint is
the default if not set).
So here is an example with plain spring security to set this up:
@Configuration
@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and()
.formLogin()
.disable()
.httpBasic()
.authenticationEntryPoint(new BasicAuthenticationEntryPointWithoutWWWAuthenticate())
}
}
// See org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint
class BasicAuthenticationEntryPointWithoutWWWAuthenticate implements AuthenticationEntryPoint {
void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
// XXX don't set the header, that triggers the browser to show the login form
// response.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\"");
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
}
}
spring-security-rest? - Jeff Scott Brown