Free auditStart free
GuideError · HTTP status codes

401 Unauthorized in Spring Boot

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.

3 min
read
4
sources
Oct 31, 2026
updated
Scaplet Editorial Team
author
The short answer

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.

  1. How Spring Security decides on 401
  2. Reproduce and check it in 2 minutes
  3. Fixes by cause
  4. What it means for SEO and crawlers
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01401 versus 403 in Spring Security

How Spring Security decides on 401

When a request reaches a protected resource without valid authentication, the filter chain throws an exception that 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.
State of the request

Not authenticated

Handler that runs
AuthenticationEntryPoint
Result
401 for Basic, or a redirect for form login
State of the request

Authenticated, authority missing

Handler that runs
AccessDeniedHandler
Result
403, see 403 in Spring Boot
State of the request

No exception thrown

Handler that runs
None
Result
The filter does nothing
Unauthenticated request path
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 header

The authorization docs' test examples confirm the outcome: an unauthenticated request gets status().isUnauthorized() and an authenticated user without the authority gets status().isForbidden().

02Check it

Reproduce and check it in 2 minutes

Spring's Basic entry point sends a challenge header, which httpbin imitates, so you can see what a 401 challenge looks like:

Run the same pair against your own app. This needs it running (replace the URL, user and password):

Your own app: with and without credentials
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.

Public endpoint: a 401 with a challenge header
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: 200
03Match the symptom to the change

Fixes by cause

The Basic docs say HTTP Basic must be enabled explicitly once any servlet-based configuration is provided. If your own SecurityFilterChain bean omits it, curl with -u is ignored:
Symptom

401 even with correct curl -u

Likely cause
httpBasic not in your SecurityFilterChain
Change
Add .httpBasic(withDefaults())
Symptom

401 on a page meant to be public

Likely cause
A later rule such as anyRequest().authenticated() matched it, or no permitAll was declared before it
Change
Add the path with permitAll() above the catch-all
Symptom

Browser shows a Basic dialog for an API call

Likely cause
The default Basic entry point sends WWW-Authenticate
Change
Send X-Requested-With: XMLHttpRequest or use a custom entry point
Symptom

Redirect to login, not 401

Likely cause
Form login is the configured entry point
Change
Expected for browsers; use httpBasic or a custom entry point for APIs
Enable HTTP Basic
@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:

Custom entry point
@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.

04Explained

What it means for SEO and crawlers

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.

Sources · 4

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • spring.io
  • google.com
  1. 01Spring Security: HTTP Basic Authenticationdocs.spring.io · checked 2026-11-01
  2. 02Spring Security: Architecture (ExceptionTranslationFilter)docs.spring.io · checked 2026-11-01
  3. 03Spring Security: Authorize HttpServletRequestsdocs.spring.io · checked 2026-11-01
  4. 04How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Why does Spring Security return 401 instead of redirecting to login?

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.

02What is the difference between 401 and 403 in Spring Security?

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.

03Do I have to enable httpBasic myself?

The Spring Security docs say HTTP Basic must be enabled explicitly once any servlet-based configuration is provided.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane