All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.yanyun.auth.service.AuthUserService Maven / Gradle / Ivy
package com.yanyun.auth.service;
import com.alibaba.fastjson.JSON;
import com.yanyun.auth.dto.ResultDto;
import com.yanyun.auth.model.SystemUserModel;
import com.yanyun.auth.model.TokenModel;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
/**
* 用户操作服务
*/
public class AuthUserService {
private RestTemplate restTemplate;
private String clientId;
private String clientSecret;
private String authUrl;
public AuthUserService(RestTemplate restTemplate, String clientId, String clientSecret, String authUrl) {
this.restTemplate = restTemplate;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.authUrl = authUrl;
}
/**
* 根据用户名和密码去申请令牌
* @param username
* @param password
* @return
*/
public ResultDto applyToken(String username, String password){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
paramsMap.set("grant_type","password");
paramsMap.set("client_id",clientId);
paramsMap.set("client_secret",clientSecret);
paramsMap.set("username",username);
paramsMap.set("password",password);
paramsMap.set("scope","all");
HttpEntity httpEntity = new HttpEntity(paramsMap,headers);
ResponseEntity stringResponseEntity = restTemplate.postForEntity(authUrl+"/oauth/token", httpEntity, String.class);
TokenModel tokenModel = JSON.parseObject(stringResponseEntity.getBody(), TokenModel.class);
if(tokenModel!=null && !StringUtils.isEmpty(tokenModel.getAccess_token())){
return ResultDto.success(tokenModel);
}
return ResultDto.error(stringResponseEntity.getBody());
}
/**
* 刷新令牌服务
* @param refreshToken
* @return
*/
public ResultDto refreshToken(String refreshToken){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap paramsMap = new LinkedMultiValueMap<>();
paramsMap.set("grant_type","refresh_token");
paramsMap.set("client_id",clientId);
paramsMap.set("client_secret",clientSecret);
paramsMap.set("refresh_token",refreshToken);
HttpEntity httpEntity = new HttpEntity(paramsMap,headers);
ResponseEntity stringResponseEntity = restTemplate.postForEntity(authUrl+"/oauth/token", httpEntity, String.class);
TokenModel tokenModel = JSON.parseObject(stringResponseEntity.getBody(), TokenModel.class);
if(tokenModel!=null && !StringUtils.isEmpty(tokenModel.getAccess_token())){
return ResultDto.success(tokenModel);
}
return ResultDto.error(stringResponseEntity.getBody());
}
/**
* 注册用户API
* @param systemUserModel
* @param access_token
* @return
*/
public ResultDto registerSystemUser(SystemUserModel systemUserModel, String access_token){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization","Bearer "+access_token);
if(StringUtils.isEmpty(systemUserModel.getClientId())){
systemUserModel.setClientId(clientId);
}
HttpEntity httpEntity = new HttpEntity(systemUserModel,headers);
ResultDto resultDto = restTemplate.postForObject(authUrl + "/user", httpEntity, ResultDto.class);
//转换数据
if(resultDto!=null && resultDto.getCode()==200){
String userStr = JSON.toJSONString(resultDto.getData());
return ResultDto.success(JSON.parseObject(userStr, SystemUserModel.class));
}
return resultDto;
}
/**
* 更新用户信息
* @param systemUserModel
* @param access_token
* @return
*/
public ResultDto updateSystemUser(SystemUserModel systemUserModel, String access_token){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization","Bearer "+access_token);
if(StringUtils.isEmpty(systemUserModel.getClientId())){
systemUserModel.setClientId(clientId);
}
HttpEntity httpEntity = new HttpEntity(systemUserModel,headers);
ResponseEntity exchange = restTemplate.exchange(authUrl + "/user", HttpMethod.PUT, httpEntity, ResultDto.class);
ResultDto resultDto = exchange.getBody();
//转换数据
if(resultDto!=null && resultDto.getCode()==200){
String userStr = JSON.toJSONString(resultDto.getData());
return ResultDto.success(JSON.parseObject(userStr, SystemUserModel.class));
}
return resultDto;
}
/**
* 删除用户API 其中 clienId 和用户名必须存在
* @param systemUserModel
* @return
*/
public ResultDto deleteSystemUserByUsername(SystemUserModel systemUserModel, String access_token){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization","Bearer "+access_token);
if(StringUtils.isEmpty(systemUserModel.getClientId())){
systemUserModel.setClientId(clientId);
}
HttpEntity httpEntity = new HttpEntity(systemUserModel,headers);
ResponseEntity exchange = restTemplate.exchange(authUrl + "/user", HttpMethod.DELETE, httpEntity, ResultDto.class);
return exchange.getBody();
}
/**
* 查询该系统下所有用户
* @param clientId
* @return
*/
public ResultDto> findSystemUsers(String clientId, String access_token){
if(StringUtils.isEmpty(clientId)){
clientId=this.clientId;
}
ResultDto resultDto = restTemplate.getForObject(authUrl + "/user/" + clientId+"?access_token="+access_token, ResultDto.class);
//转换数据
if(resultDto!=null && resultDto.getCode()==200){
String users = JSON.toJSONString(resultDto.getData());
List systemUserModels = JSON.parseArray(users, SystemUserModel.class);
return ResultDto.success(systemUserModels);
}
return resultDto;
}
/**
* 根据用户名和clientId查询信息
* @param clientId
* @param username
* @param access_token
* @return
*/
public ResultDto getSystemUsersByClientAndUsername(String clientId,String username,String access_token){
if(StringUtils.isEmpty(clientId)){
clientId=this.clientId;
}
ResultDto resultDto = restTemplate.getForObject(authUrl + "/user/" + clientId+"/"+username+"?access_token="+access_token, ResultDto.class);
//转换数据
if(resultDto!=null && resultDto.getCode()==200){
String users = JSON.toJSONString(resultDto.getData());
return ResultDto.success(JSON.parseObject(users, SystemUserModel.class));
}
return resultDto;
}
}