Testing HTTP Basic Authentication

虽然始终可以用 HTTP Basic 进行身份验证,但记住头名称、格式和对值进行编码会有点乏味。现在,可以使用 Spring Security 的 httpBasic RequestPostProcessor 来完成此操作。例如,下面的代码片段:

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. For example, the snippet below:

  • Java

  • Kotlin

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

将尝试使用 HTTP 基本身份验证,通过确保在 HTTP 请求上填充以下标题,对具有用户名“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==