# Basic Authentication

This section provides details on how Spring Security provides support for [Basic HTTP Authentication](https://tools.ietf.org/html/rfc7617) for servlet based applications.

Let’s take a look at how HTTP Basic Authentication works within Spring Security.
First, we see the [WWW-Authenticate](https://tools.ietf.org/html/rfc7235#section-4.1) header is sent back to an unauthenticated client.

![basicauthenticationentrypoint](https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/basicauthenticationentrypoint.png)

Figure 1. Sending WWW-Authenticate Header

The figure builds off our [`SecurityFilterChain`](../../architecture.html#servlet-securityfilterchain) diagram.

![number 1](https://docs.spring.io/spring-security/reference/_images/icons/number_1.png) First, a user makes an unauthenticated request to the resource `/private` for which it is not authorized.

![number 2](https://docs.spring.io/spring-security/reference/_images/icons/number_2.png) Spring Security’s [`FilterSecurityInterceptor`](../../authorization/authorize-requests.html#servlet-authorization-filtersecurityinterceptor) indicates that the unauthenticated request is *Denied* by throwing an `AccessDeniedException`.

![number 3](https://docs.spring.io/spring-security/reference/_images/icons/number_3.png) Since the user is not authenticated, [`ExceptionTranslationFilter`](../../architecture.html#servlet-exceptiontranslationfilter) initiates *Start Authentication*.
The configured [`AuthenticationEntryPoint`](../architecture.html#servlet-authentication-authenticationentrypoint) is an instance of [`BasicAuthenticationEntryPoint`](https://docs.spring.io/spring-security/site/docs/5.6.2/api/org/springframework/security/web/authentication/www/BasicAuthenticationEntryPoint.html) which sends a WWW-Authenticate header.
The `RequestCache` is typically a `NullRequestCache` that does not save the request since the client is capable of replaying the requests it originally requested.

When a client receives the WWW-Authenticate header it knows it should retry with a username and password.
Below is the flow for the username and password being processed.

![basicauthenticationfilter](https://docs.spring.io/spring-security/reference/_images/servlet/authentication/unpwd/basicauthenticationfilter.png)

Figure 2. Authenticating Username and Password

The figure builds off our [`SecurityFilterChain`](../../architecture.html#servlet-securityfilterchain) diagram.

![number 1](https://docs.spring.io/spring-security/reference/_images/icons/number_1.png) When the user submits their username and password, the `BasicAuthenticationFilter` creates a `UsernamePasswordAuthenticationToken` which is a type of [`Authentication`](../architecture.html#servlet-authentication-authentication) by extracting the username and password from the `HttpServletRequest`.

![number 2](https://docs.spring.io/spring-security/reference/_images/icons/number_2.png) Next, the `UsernamePasswordAuthenticationToken` is passed into the `AuthenticationManager` to be authenticated.
The details of what `AuthenticationManager` looks like depend on how the [user information is stored](index.html#servlet-authentication-unpwd-storage).

![number 3](https://docs.spring.io/spring-security/reference/_images/icons/number_3.png) If authentication fails, then *Failure*

* The [SecurityContextHolder](../architecture.html#servlet-authentication-securitycontextholder) is cleared out.

* `RememberMeServices.loginFail` is invoked.
  If remember me is not configured, this is a no-op.

* `AuthenticationEntryPoint` is invoked to trigger the WWW-Authenticate to be sent again.

![number 4](https://docs.spring.io/spring-security/reference/_images/icons/number_4.png) If authentication is successful, then *Success*.

* The [Authentication](../architecture.html#servlet-authentication-authentication) is set on the [SecurityContextHolder](../architecture.html#servlet-authentication-securitycontextholder).

* `RememberMeServices.loginSuccess` is invoked.
  If remember me is not configured, this is a no-op.

* The `BasicAuthenticationFilter` invokes `FilterChain.doFilter(request,response)` to continue with the rest of the application logic.

Spring Security’s HTTP Basic Authentication support in is enabled by default.
However, as soon as any servlet based configuration is provided, HTTP Basic must be explicitly provided.

A minimal, explicit configuration can be found below:

Example 1. Explicit HTTP Basic Configuration

Java

```
protected void configure(HttpSecurity http) {
	http
		// ...
		.httpBasic(withDefaults());
}
```

XML

```
<http>
	<!-- ... -->
	<http-basic />
</http>
```

Kotlin

```
fun configure(http: HttpSecurity) {
	http {
		// ...
		httpBasic { }
	}
}
```

[Form](form.html)[Digest](digest.html)

