# 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.

Canonical: https://scaplet.com/errors/http/401/spring-boot

Guide Error · HTTP status codes

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

401 4xx · Client error

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](#how-spring-security-decides-on-401)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [Fixes by cause](#fixes-by-cause)
4. [What it means for SEO and crawlers](#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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Fspring-boot%20(%22401%20Unauthorized%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Fspring-boot%20(%22401%20Unauthorized%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Fspring-boot%20(%22401%20Unauthorized%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Fspring-boot%20(%22401%20Unauthorized%20in%20Spring%20Boot%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_401 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](/errors/http/403/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()`.

_02_Check 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
```

_03_Match 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.

_04_Explained

## 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](/errors/http/401) and [4xx errors](/seo-issues/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. 01 [Spring Security: HTTP Basic Authentication](https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/basic.html) docs.spring.io · checked 2026-11-01
2. 02 [Spring Security: Architecture (ExceptionTranslationFilter)](https://docs.spring.io/spring-security/reference/servlet/architecture.html) docs.spring.io · checked 2026-11-01
3. 03 [Spring Security: Authorize HttpServletRequests](https://docs.spring.io/spring-security/reference/servlet/authorization/authorize-http-requests.html) docs.spring.io · checked 2026-11-01
4. 04 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

Questions

## Asked a lot.

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

_01_ Why 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.

_02_ What 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.

_03_ Do I have to enable httpBasic myself?

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

## Keep learning

[All errors](/errors)

[Learn 403 Forbidden in Spring Boot HTTP status codes](/errors/http/403/spring-boot) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next HTTP 401 Unauthorized HTTP status codes](/errors/http/401) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 401 errors
[401 Unauthorized in IIS](/errors/http/401/iis) [401 Unauthorized in Laravel](/errors/http/401/laravel) [401 Unauthorized in the WordPress REST API](/errors/http/401/wordpress) [HTTP 401 Unauthorized](/errors/http/401)

Nearby in errors
[402 Payment Required](/errors/http/402)

More in HTTP 401 Unauthorized
[401 Unauthorized in IIS](/errors/http/401/iis) [401 Unauthorized in Laravel](/errors/http/401/laravel) [401 Unauthorized in Spring Boot](/errors/http/401/spring-boot) [401 Unauthorized in the WordPress REST API](/errors/http/401/wordpress)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
