有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何代表用户获取Oauth2令牌请求

我是spring新手,使用spring和oauth 2创建后端。到目前为止,我能够使用spring实现outh2,并且能够获得访问和刷新令牌:

localhost:8082/oauth/token

我得到的回应是:

{
  "access_token": "2b57cd84-c1fb-493e-88b0-e3da2ae66c77",
  "token_type": "bearer",
  "refresh_token": "db9e5e33-4878-4a31-8037-b7ad0107b82a",
  "expires_in": 43199,
  "scope": "read write"
} 

在用户注册方面,我试图代表用户获取访问和刷新令牌,为此我完成了如下实现(在从移动端获取用户对象请求后,我在user controller userRegistration方法中添加了此代码段):

final String clientId = PropertiesReader.getInstance().getProperty("client1");
final String clientSecret = PropertiesReader.getInstance().getProperty("client1password");
final Map<String, String> params = new HashMap<String, String>();
                        params.put("grant_type", "password");
                        params.put("client_id", clientId);
                        params.put("username", userObj.getUsername());
                        params.put("password", dummyPwd);
                        final Response response = RestAssured
                                .given()
                                .auth()
                                .preemptive()
                                .basic(clientId, clientSecret)
                                .and()
                                .with()
                                .params(params)
                                .when()
                                .post(PropertiesReader.getInstance()
                                        .getProperty("oauthurl"));
                        if (CustomValidation.checkStringIsNotEmpty(response
                                .asString())) {
                            return ClientResponse.setResponse(
                                    response.asString(), HttpStatus.OK);
                        } else {
                            return ClientResponse.setResponse(PropertiesReader
                                    .getInstance().getProperty("wentwrong"),
                                    HttpStatus.INTERNAL_SERVER_ERROR);
                        }

我得到的回应是:

  {"access_token":"00bfd552-c7eb-48ff-8f2bfd5cd24869be",
"token_type":"bearer",
"refresh_token":"f88be427-ea6e-4cad-8dc5-01d37e4cfdbc",
"expires_in":299,
"scope":"update read write",
"date":1599982652000,
"deviceDetailsSaved":false,
"firstname":"test",
"role":"USER",
"name":"test test",
"mobile":"8169280313",
"avatar":"1.svg",
"email":"test1@mail.com",
"lastname":"test"}

我想知道的是,有没有其他合适的方法可以在注册后获取oauth令牌并附加到用户对象中?。到目前为止,这个实现只针对前端客户端(安卓),所以我保持客户端细节静态。任何建议都会有帮助


共 (0) 个答案