我正在使用springboot2实现具有spring-security的oauth2。
当我直接调用oauth/token时,我能够获得结果……工作正常。
我需要让它在我的自定义登录控制器上以相同的方式工作,从那里我可以将请求重定向到oauth/token以生成令牌。这样做的原因是我需要在数据库中存储访问令牌和登录时间。
@RequestMapping(value = "/login", method = { RequestMethod.OPTIONS, RequestMethod.POST })
public Object login(HttpServletRequest request, HttpServletResponse
response, @RequestParam Map<String, String> params) {
//Here I want to make a call to oauth/token.
}
请在这方面提供帮助。提前谢谢。
发布于 2019-04-05 15:55:42
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "YOUR_BASIC");
HttpEntity<String> entity = new HttpEntity<>("", headers);
String authURL = "YOUR_URL/oauth/token?grant_type=YOUR_GRANT_TYPE&username=" + YOUR_USERNAME + "&password=YOUR_PASSWORD&scope=ui";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(authURL, entity, String.class);
JSONObject jsonObj = new JSONObject(responseEntity.getBody());
jsonObj.get("access_token").toString();
依赖关系:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
https://stackoverflow.com/questions/55371055
复制相似问题