@PostMapping("getSsoToken")
@ApiOperation(value = "获取登录token")
public Object getSsoToken(HttpServletRequest request, @RequestParam Map<String, String> parameters) {
SSOUserResponse userInfo = new SSOUserResponse();
if (StringUtils.isNotBlank(parameters.get("callback"))) {
// 1,通过callback获取token
String token = getTokenByCallback(parameters);
if (StringUtils.isBlank(token)) {
return Result.failed("通过callback获取用户token失败");
}
// 2,通过token获取用户信息
userInfo = getUserInfoByToken(token);
}
if (Objects.isNull(userInfo) || StringUtils.isBlank(userInfo.getAppAccount())) {
return Result.failed("通过token获取用户信息失败");
}
// 3,组装用户信息并通过oauth2认证
try {
return getOAuth2AccessToken(request, parameters, userInfo);
} catch (HttpRequestMethodNotSupportedException e) {
e.printStackTrace();
return Result.failed("用户信息无法通过oauth认证,异常认证信息为:" + e.getMessage());
}
}
/**
* @author: Created by AWU
* @date: Created in 2023/06/13 16:53
*/
private Object getOAuth2AccessToken(HttpServletRequest request, Map<String, String> parameters,
SSOUserResponse userInfo) throws HttpRequestMethodNotSupportedException {
SysUser sysUser = userService.selectByUsername(userInfo.getAppAccount());
if (Objects.isNull(sysUser) || StringUtils.isBlank(sysUser.getUsername())) {
sysUser = createNewUserHaveDefaultRoles(userInfo);
}
if (Objects.isNull(sysUser)) {
return Result.failed("新建用户信息由于单点登录用户数据获取失败而无法成功");
}
// 4, 用户信息判空处理,密码是否需要解密等
parameters.put("username", sysUser.getUsername());
parameters.put("password", sysUser.getPassword());
parameters.put("grant_type", "password");
UsernamePasswordAuthenticationToken authenticationToken = convert(request);
if (Objects.isNull(authenticationToken)) {
return Result.failed("验证失败");
}
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(authenticationToken.getName());
if (Objects.isNull(clientDetails)) {
return Result.failed("验证失败");
}
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
return tokenEndpoint.postAccessToken(authenticationToken, parameters);
}
/**
* @author: Created by AWU
* @date: Created in 2023/06/13 15:37
*/
private UsernamePasswordAuthenticationToken convert(HttpServletRequest request) {
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (header == null) {
return null;
}
header = header.trim();
if (!org.springframework.util.StringUtils.startsWithIgnoreCase(header, AUTHENTICATION_SCHEME_BASIC)) {
return null;
}
if (header.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) {
throw new BadCredentialsException("Empty basic authentication token");
}
byte[] base64Token = header.substring(6).getBytes(StandardCharsets.UTF_8);
byte[] decoded = decode(base64Token);
String token = new String(decoded, StandardCharsets.UTF_8);
int delim = token.indexOf(":");
if (delim == -1) {
throw new BadCredentialsException("Invalid basic authentication token");
}
return new UsernamePasswordAuthenticationToken(token.substring(0, delim), token.substring(delim + 1), null);
}
/**
* @author: Created by AWU
* @date: Created in 2023/06/13 15:38
*/
private byte[] decode(byte[] base64Token) {
try {
return Base64.getDecoder().decode(base64Token);
} catch (IllegalArgumentException ex) {
throw new BadCredentialsException("Failed to decode basic authentication token");
}
}
end 转载记得标明来源