java - Android Retrofit 2 使用 Oauth 2.0 登录 - Spring Boot 后端
问题描述
我正在尝试从使用 Spring Boot 2 的 API 登录并使用改造 2 获取令牌。
我遇到的问题是它可以在 Postman 中使用,但我在 Android 中获得了 403 状态码。
这是我在 Postman 中使用的代码:
我一直在尝试这两种方法,但我得到了相同的 403 状态码:
令牌响应类:
public class TokenResponse {
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("access_token")
@Expose
private String accessToken;
public String getTokenType() {
return tokenType;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
}
服务(我尝试过的两种方式):
@POST("/oauth/token/")
@FormUrlEncoded
Call<TokenResponse> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("username") String username,
@Field("password") String password);
@FormUrlEncoded
@POST("/oauth/token/")
Call<TokenResponse> getToken2(@Field("grant_type") String grantType,
@Field("username") String username,
@Field("password") String password,
@Header("Authorization") String authorization);
以及我提出请求的班级(也有我尝试过的两种方式):
private void oauth2Example() {
Retrofit.Builder builder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://ip:8080");
Retrofit retrofit = builder.build();
UserService service = retrofit.create(UserService.class);
//way number one
byte[] credentials = "androidapp:12345".getBytes();
String basicAuth = "";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
basicAuth = "Basic " + Base64.getEncoder().encodeToString(credentials).trim();
}
Call<TokenResponse> call = service.getToken2("password", "j@h.com", "12345", basicAuth);
//FINISH way number 1
//way number two
Call<TokenResponse> call = service.getToken("androidapp", "12345", "password", "j@h.com", "12345");
//FINISH way number 2
//RESULT
call.enqueue(new Callback<TokenResponse>() {
@Override
public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) {
if (response.isSuccessful()){
Toast.makeText(PostActivity.this, response.body().getAccessToken(), Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(PostActivity.this,"error " + response.code(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<TokenResponse> call, Throwable t) {
Toast.makeText(PostActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
如您所见,我评论了我正在尝试的两种方式(当然,当时我没有使用相同的“调用”变量),但是正如我所说,在这两种方式中,我都收到了 403 错误。
为了能够从 Postman 获得不记名令牌,我应该修改什么,但我在 Android 中只获得了 403?
解决方案
我为了避免 403 禁止所做的就是改变这个
@POST("/oauth/token/")
@FormUrlEncoded
Call<TokenResponse> getToken(@Field("client_id") String clientId,
@Field("client_secret") String clientSecret,
@Field("grant_type") String grantType,
@Field("username") String username,
对此:
@FormUrlEncoded
@Headers(
{"Authorization: Basic yourAuthHere",
"Content-Type: application/x-www-form-urlencoded"}
)
@POST("oauth/token")
Call<JWT> oauthToken(
@Field("username") String username,
@Field("password") String password,
@Field("grant_type") String grant_type);
这样我就可以使用 oauth 2.0 通过 Spring Boot 后端登录
推荐阅读
- python - 为什么我不能使用方法更改类中列表的值?
- python - Python dll函数返回错误值
- android - 从 Android Studio 构建时不会增量执行 Kotlin 编译
- python - 无法在 Python 中将字符串与模式匹配
- python - 格式化数据框的 JSON 输出
- javascript - 更改部分 HTML 文档的相对路径基础,尤其是对于注入的 SVG?
- spring-boot - 弹簧数据 aerospike | 如何实现交易行为
- python - 使用 k-means 可视化集群
- c - 如何实现终端滚动?
- python - InvalidArgumentError:不兼容的形状:[10,224,224,3] 与 [10,224,224]