Not authenticated
- Handler that runs
- AuthenticationEntryPoint
- Result
- 401 for Basic, or a redirect for form login
Why Spring Security returns 401: no credentials arrived, httpBasic is not enabled, or the entry point is not the one you expect. Includes curl checks and config.
A Spring Boot 401 means Spring Security found no valid authentication for a protected path and started authentication through the configured entry point. With HTTP Basic, that entry point sends a WWW-Authenticate header. Check that httpBasic is enabled, the client sends credentials, and the path is not matched by a rule that requires authentication unexpectedly.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
ExceptionTranslationFilter catches. The architecture docs say it then starts authentication: the AuthenticationEntryPoint requests credentials from the client, and for example by redirecting to a login page or sending a WWW-Authenticate header.client -> filter chain -> AuthorizationFilter (rule requires authentication)
-> AccessDeniedException, user not authenticated
-> ExceptionTranslationFilter -> startAuthentication()
-> AuthenticationEntryPoint
|-- form login configured: redirect to the login page
`-- httpBasic configured: 401 + WWW-Authenticate headerThe authorization docs' test examples confirm the outcome: an unauthenticated request gets status().isUnauthorized() and an authenticated user without the authority gets status().isForbidden().
Run the same pair against your own app. This needs it running (replace the URL, user and password):
curl -i http://localhost:8080/api/orders
curl -i -u user:password http://localhost:8080/api/orders
# Expect: the first returns 401 (or a 302 redirect to a login page when form login is
# the entry point). The second returns 200 with valid credentials.A 302 to a login page instead of a 401 tells you the entry point is the form login one. A 403 on the second call means authentication worked and an authority rule refused you.
curl -i https://httpbin.org/basic-auth/user/pass
# Expect: HTTP/2 401 and a www-authenticate: Basic realm="Fake Realm" header
curl -s -o /dev/null -w "%{http_code}\n" -u user:pass https://httpbin.org/basic-auth/user/pass
# Expect: 200SecurityFilterChain bean omits it, curl with -u is ignored:@Bean
public SecurityFilterChain filterChain(HttpSecurity http) {
http
// ...
.httpBasic(withDefaults());
return http.build();
}If the browser pops up a login dialog you do not want for a JavaScript client, the docs describe a built-in switch: when a request carries X-Requested-With: XMLHttpRequest, the default entry point leaves out the body and the WWW-Authenticate header of the 401. To change the behavior entirely, supply your own BasicAuthenticationEntryPoint:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName("My Realm");
http
// ...
.httpBasic(basic -> basic.authenticationEntryPoint(entryPoint));
return http.build();
}Rule order matters, as the authorize-requests docs state: the filter applies only the first match. A specific permitAll() path placed after anyRequest().authenticated() never gets used.
Google ignores content from URLs that return 4xx, apart from 429, and it says 401 and 403 have no effect on crawl rate. A 401 on an API is fine. A 401 or login redirect on a public page you want found keeps it out of Search, so check marketing paths against your rule list. See 401 Unauthorized and 4xx errors.
3 questions people ask next, answered from the sources on this page.
The entry point decides. With HTTP Basic it sends a WWW-Authenticate header with a 401. With form login it redirects to a login page.
An unauthenticated request starts authentication through the AuthenticationEntryPoint, usually a 401. An authenticated request that lacks the authority is handled by the AccessDeniedHandler and returns 403.
The Spring Security docs say HTTP Basic must be enabled explicitly once any servlet-based configuration is provided.
Run the free audit, then approve the fixes. Every change can be undone.