|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.tuoheng.constants.OidcConstant; |
|
|
import com.tuoheng.constants.OidcConstant; |
|
|
import com.tuoheng.constants.ThirdConstant; |
|
|
import com.tuoheng.constants.ThirdConstant; |
|
|
|
|
|
import com.tuoheng.exception.DiyException; |
|
|
|
|
|
import com.tuoheng.mapper.RegisteredClientMapper; |
|
|
import com.tuoheng.model.dto.ClientRoleDto; |
|
|
import com.tuoheng.model.dto.ClientRoleDto; |
|
|
|
|
|
import com.tuoheng.model.po.RegisteredClientPo; |
|
|
import com.tuoheng.model.result.ProfileResult; |
|
|
import com.tuoheng.model.result.ProfileResult; |
|
|
import com.tuoheng.model.result.TokenResult; |
|
|
import com.tuoheng.model.result.TokenResult; |
|
|
import com.tuoheng.service.ThirdService; |
|
|
import com.tuoheng.service.ThirdService; |
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
import org.springframework.util.MultiValueMap; |
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
|
|
|
|
import java.rmi.ServerException; |
|
|
import java.util.Base64; |
|
|
import java.util.Base64; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${oauth2.token.issuer}") |
|
|
@Value("${oauth2.token.issuer}") |
|
|
private String tokenIssuer; |
|
|
private String tokenIssuer; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private RegisteredClientMapper registeredClientMapper; |
|
|
|
|
|
|
|
|
|
|
|
private static final String username = "csadmin"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String password = "thjs2023"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 根据第三方授权验证token 进行获取用户信息并跳转相关地址 |
|
|
* 根据第三方授权验证token 进行获取用户信息并跳转相关地址 |
|
|
* |
|
|
* |
|
|
* @param token 第三方token |
|
|
|
|
|
|
|
|
* @param clientId 平台标识 |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
@Override |
|
|
@Override |
|
|
public JsonResult authorize(String token) { |
|
|
|
|
|
|
|
|
|
|
|
//String result = HttpUtils.sendPost(ThirdConstant.OAUTH_URL, token); |
|
|
|
|
|
//此时模拟已获取username 以及password |
|
|
|
|
|
String result = "csadmin"; |
|
|
|
|
|
|
|
|
public JsonResult authorize(String clientId) { |
|
|
|
|
|
|
|
|
//通过oidc的密码模式获取授权token 等相关信息数据 |
|
|
//通过oidc的密码模式获取授权token 等相关信息数据 |
|
|
TokenResult tokenResult = getToken(result); |
|
|
|
|
|
//封装用户名 |
|
|
|
|
|
tokenResult.setUserName(result); |
|
|
|
|
|
|
|
|
TokenResult tokenResult = getToken(clientId); |
|
|
|
|
|
tokenResult.setUserName(username); |
|
|
//此时通过token获取当前用户的相关权限信息并进行封装 |
|
|
//此时通过token获取当前用户的相关权限信息并进行封装 |
|
|
tokenResult = getClientResult(tokenResult); |
|
|
|
|
|
|
|
|
tokenResult = getClientResult(tokenResult, clientId); |
|
|
//数据封装完毕返回数据 以及相关地址 -> 是否重定向 |
|
|
//数据封装完毕返回数据 以及相关地址 -> 是否重定向 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return JsonResult.success(tokenResult); |
|
|
return JsonResult.success(tokenResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 通过账号密码获取token |
|
|
* 通过账号密码获取token |
|
|
* |
|
|
|
|
|
* @param result |
|
|
|
|
|
*/ |
|
|
*/ |
|
|
private TokenResult getToken(String result) { |
|
|
|
|
|
|
|
|
|
|
|
String url = tokenIssuer + OidcConstant.OAUTH2_TOKEN; |
|
|
|
|
|
|
|
|
private TokenResult getToken(String clientId) { |
|
|
|
|
|
|
|
|
|
|
|
RegisteredClientPo clientPo = registeredClientMapper.selectClient(clientId); |
|
|
|
|
|
if (ObjectUtils.isEmpty(clientPo)) { |
|
|
|
|
|
throw new DiyException("该clientId不存在本系统中"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String url = tokenIssuer + OidcConstant.OAUTH2_TOKEN; |
|
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
|
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
|
|
//TODO result含有参数 |
|
|
|
|
|
params.add("password", "thjs2023"); |
|
|
|
|
|
params.add("username", "csadmin"); |
|
|
|
|
|
|
|
|
params.add("password", password); |
|
|
|
|
|
//定死账号密码 |
|
|
|
|
|
params.add("username", username); |
|
|
params.add("grant_type", "password"); |
|
|
params.add("grant_type", "password"); |
|
|
params.add("scope", "openid profile"); |
|
|
params.add("scope", "openid profile"); |
|
|
//机场标识 |
|
|
//机场标识 |
|
|
String userMsg = "tuoheng-airport-admin" + ":" + "NjHifmmB41rH6bJTd4A7RA=="; |
|
|
|
|
|
|
|
|
String userMsg = clientPo.getClientId() + ":" + clientPo.getClientSecret(); |
|
|
String authorization = Base64.getEncoder().encodeToString(userMsg.getBytes()); |
|
|
String authorization = Base64.getEncoder().encodeToString(userMsg.getBytes()); |
|
|
|
|
|
|
|
|
ParameterizedTypeReference<TokenResult> parameterizedTypeReference = |
|
|
ParameterizedTypeReference<TokenResult> parameterizedTypeReference = |
|
|
new ParameterizedTypeReference<TokenResult>() { |
|
|
new ParameterizedTypeReference<TokenResult>() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
|
headers.add("Authorization", "Basic " + authorization); |
|
|
headers.add("Authorization", "Basic " + authorization); |
|
|
|
|
|
|
|
|
* @param tokenResult |
|
|
* @param tokenResult |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
private TokenResult getClientResult(TokenResult tokenResult) { |
|
|
|
|
|
|
|
|
private TokenResult getClientResult(TokenResult tokenResult, String clientId) { |
|
|
|
|
|
|
|
|
String url = tokenIssuer + OidcConstant.GET_USERINFO; |
|
|
String url = tokenIssuer + OidcConstant.GET_USERINFO; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenResult.setUserId(result.getUserId()); |
|
|
tokenResult.setUserId(result.getUserId()); |
|
|
tokenResult.setUserName(result.getUserName()); |
|
|
tokenResult.setUserName(result.getUserName()); |
|
|
ProfileResult profile = new ProfileResult(); |
|
|
ProfileResult profile = new ProfileResult(); |
|
|
BeanUtils.copyProperties(tokenResult,profile); |
|
|
|
|
|
|
|
|
BeanUtils.copyProperties(tokenResult, profile); |
|
|
|
|
|
profile.setSub(clientId); |
|
|
profile.setAuthority(authorityList); |
|
|
profile.setAuthority(authorityList); |
|
|
profile.setClientRoleList(clientRoleDtoList); |
|
|
profile.setClientRoleList(clientRoleDtoList); |
|
|
tokenResult.setProfile(profile); |
|
|
tokenResult.setProfile(profile); |