публичная аутентификация пользователя (пользователь UserBean)
LOG.info («Подготовка к загрузке accessToken»);
((ResourceOwnerPasswordResourceDetails) restTemplate.getResource ()). SetUsername (user.getUserName ());
((ResourceOwnerPasswordResourceDetails) restTemplate.getResource ()). SetPassword (user.getPassword ());
LoginResponse loginResponse = new LoginResponse();
/*
* Clearing restTemplate cache for previosly stored accessToken
*/
restTemplate.getOAuth2ClientContext().setAccessToken(null);
String accessToken = restTemplate.getAccessToken().getValue();
String refreshToken = restTemplate.getOAuth2ClientContext().getAccessToken().getRefreshToken().getValue();
Date expireDate = restTemplate.getOAuth2ClientContext().getAccessToken().getExpiration();
loginResponse.setAccessToken(accessToken);
LOG.info("AccessToken retrieved : " + accessToken);
Map<String, String> userAttributes = null;
String userInfoURI = "some URL here";
userAttributes = getUserInfo(accessToken, userAttributes, userInfoURI);
loginResponse.setUserInfo(userAttributes);
User userBean = userService.findByUserName(user.getUserName());
if (userBean == null) {
LOG.info("No existing user found with username " + user.getUserName());
LOG.info("Creating new entry in DB for username: " + user.getUserName());
userBean = new User();
userBean.setUserName(user.getUserName());
userBean.setUserType(UserType.CONTRACTOR.ordinal());
}
java.sql.Timestamp tokenExpiryDate = new java.sql.Timestamp(expireDate.getTime());
userBean.setExpiresAt(tokenExpiryDate);
userBean.setAccessToken(accessToken);
userBean.setRefreshToken(refreshToken);
userBean.setGivenName(userAttributes.get(UserInfoConstants.FIRST_NAME));
userService.saveUser(userBean);
return userBean;
}
private Map<String, String> getUserInfo(String token, Map<String, String> userAttributes, String userInfoURI) {
if (!StringUtils.isEmpty(userInfoURI)) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
HttpEntity<Map<String, String>> entity = new HttpEntity<Map<String, String>>(headers);
ResponseEntity<Map> response = restTemplate.exchange(userInfoURI, HttpMethod.GET, entity, Map.class);
userAttributes = response.getBody();
LOG.info("UserInfo details : " + userAttributes);
}
return userAttributes;
}`enter code here`