使用io.jsonwebtoken获取JWT

时间:2019-04-24 18:45:15

标签: spring spring-boot jwt restful-authentication

我想获得带有库io.jsonwebtoken的JWT,我想知道是否有类似的类或方法,例如OAuth2

OAuth2AuthorizedClient client = authorizedClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());

或者最好是通过发送用户名和密码的POST方法使用户RestTemplate restTemplate = new RestTemplate();

1 个答案:

答案 0 :(得分:0)

使用RestTemplate就可以了

// POST Login
    // @return "Authorization string".
    private static String postLogin(String username, String password) {

        // Request Header
        HttpHeaders headers = new HttpHeaders();

        // Request Body
        MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<String, String>();
        parametersMap.add("username", username);
        parametersMap.add("password", password);

        // Request Entity
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(parametersMap, headers);

        // RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // POST Login
        ResponseEntity<String> response = restTemplate.exchange(URL_LOGIN, //
                HttpMethod.POST, requestEntity, String.class);

        HttpHeaders responseHeaders = response.getHeaders();

        List<String> list = responseHeaders.get("Authorization");
        return list == null || list.isEmpty() ? null : list.get(0);
    }