servlet-test-mockmvc-http-basic.md 772 字节
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# Testing HTTP Basic Authentication

While it has always been possible to authenticate with HTTP Basic, it was a bit tedious to remember the header name, format, and encode the values.
Now this can be done using Spring Security’s `httpBasic` [`RequestPostProcessor`](request-post-processors.html).
For example, the snippet below:

Java

```
mvc
	.perform(get("/").with(httpBasic("user","password")))
```

Kotlin

```
mvc.get("/") {
    with(httpBasic("user","password"))
}
```

will attempt to use HTTP Basic to authenticate a user with the username "user" and the password "password" by ensuring the following header is populated on the HTTP Request:

```
Authorization: Basic dXNlcjpwYXNzd29yZA==
```

[Mocking Form Login](form-login.html)[Mocking OAuth2](oauth2.html)