Я хочу авторизоваться с помощью безопасности JWT Spring.
Модификация Экземпляр
Retrofit.Builder()
.baseUrl(MOCK_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
API
@FormUrlEncoded
@Headers(
"Authorization: Basic VGVzdENsaWVudElkOlRlc3RDbGllbnRTZWNyZXQ=",
"Content-Type: application/x-www-form-urlencoded"
)
@POST("oauth/token/")
fun oauthToken(
@Field("username") username: String = "%2B79251234567",
@Field("password") password: String = "123",
@Field("grant_type") grant_type: String = "password"): Flowable<TokenObject>
И я получаю "status":401,"error":"Unauthorized","message":"Bad credentials"
.Лог перехватчик показывает это
D/OkHttp: --> POST http://192.168.1.28:8888/oauth/token/
D/OkHttp: Content-Type: application/x-www-form-urlencoded
D/OkHttp: Content-Length: 56
D/OkHttp: Authorization: Basic VGVzdENsaWVudElkOlRlc3RDbGllbnRTZWNyZXQ=
D/OkHttp: username=%2B79251234567&password=123&grant_type=password
D/OkHttp: --> END POST (56-byte body)
Но если я использую Unirest
val response = Unirest.post("http://192.168.1.28:8888/oauth/token")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Authorization", "Basic VGVzdENsaWVudElkOlRlc3RDbGllbnRTZWNyZXQ=")
.body("username=%2B79251234567&password=123&grant_type=password")
.asJsonAsync(object : Callback<JsonNode> {
override fun failed(e: UnirestException) {
println("The request has failed ${e.message}")
}
override fun completed(response: HttpResponse<JsonNode>) {
println("completed")
println(response.getBody())
}
override fun cancelled() {
println("The request has been cancelled")
}
})
Это закончится с успехом и токеном.Запрос журнала
Заголовки
{authorization=Basic VGVzdENsaWVudElkOlRlc3RDbGllbnRTZWNyZXQ=, content-type=application/x-www-form-urlencoded}
Тело
username=%2B79251234567&password=123&grant_type=password
OkHttp тоже нормально
val client = OkHttpClient()
val mediaType = MediaType.parse("application/x-www-form-urlencoded")
val body = RequestBody.create(mediaType, "username=%2B79251234567&password=123&grant_type=password")
val request1 = Request.Builder()
.url("http://192.168.1.28:8888/oauth/token")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", "Basic VGVzdENsaWVudElkOlRlc3RDbGllbnRTZWNyZXQ=")
.build()
val response1 = client.newCall(request1).execute()
println(response1.message())