Browse Source

增加获取hlcgadmin用户token接口

tags/V1.3.3^2
wanghaoran 5 months ago
parent
commit
1621c632d4
6 changed files with 232 additions and 0 deletions
  1. +27
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/constant/OidcConstant.java
  2. +28
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/OidcLoginController.java
  3. +21
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/ProfileResultDto.java
  4. +25
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/TokenResultDto.java
  5. +8
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/oidc/OidcLoginService.java
  6. +123
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/oidc/impl/OidcLoginServiceImpl.java

+ 27
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/constant/OidcConstant.java View File

@@ -0,0 +1,27 @@
package com.tuoheng.admin.constant;

import lombok.Data;

/**
* oidc相关授权接口
* @Author xiaoying
* @Date 2023/11/10 13:55
*/
@Data
public class OidcConstant {

public static final String OIDC_DOMAIN = "https://oidc.t-aaron.com";

public static final String OAUTH2_TOKEN = "/oauth2/token";

public static final String GET_USERINFO = "/user/getInfo";

public static final String HLCG_USERNAME = "hlcgadmin";

public static final String HLCG_PASSWORD = "thjs2023";

public static final String CLIENT_ID = "tuoheng-telecomumale-admin";

public static final String CLIENT_SECRET = "{noop}TiPUwzVk5H4qgjwXrMynMg==";

}

+ 28
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/OidcLoginController.java View File

@@ -0,0 +1,28 @@
package com.tuoheng.admin.controller;

import com.tuoheng.admin.service.oidc.OidcLoginService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/OidcLogin")
@Slf4j
public class OidcLoginController {

@Autowired
private OidcLoginService loginService;

/**
*
*登录
* @return
*/
@GetMapping("/authorize")
public JsonResult authorize() {
return loginService.authorize();
}
}

+ 21
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/ProfileResultDto.java View File

@@ -0,0 +1,21 @@
package com.tuoheng.admin.dto;

import lombok.Data;

import java.util.List;

/**
* @Author xiaoying
* @Date 2023/11/11 13:20
*/
@Data
public class ProfileResultDto {

private String sub;
private List<String> authority;
private String userId;
private String userName;
private List<ClientRoleDto> clientRoleList;
private String azp;

}

+ 25
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/TokenResultDto.java View File

@@ -0,0 +1,25 @@
package com.tuoheng.admin.dto;

import lombok.Data;

import java.util.List;

/**
* @Author xiaoying
* @Date 2023/11/10 14:11
*/
@Data
public class TokenResultDto {

private String access_token;
private String refresh_token;
private String scope;
private String token_type;
private String expires_in;
private String userId;
private String userName;
private String sub;
private List<String> authorityList;
private List<ClientRoleDto> clientRoleDtoList;
private ProfileResultDto profile;
}

+ 8
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/oidc/OidcLoginService.java View File

@@ -0,0 +1,8 @@
package com.tuoheng.admin.service.oidc;

import com.tuoheng.common.core.utils.JsonResult;

public interface OidcLoginService {

JsonResult authorize();
}

+ 123
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/oidc/impl/OidcLoginServiceImpl.java View File

@@ -0,0 +1,123 @@
package com.tuoheng.admin.service.oidc.impl;

import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.constant.OidcConstant;
import com.tuoheng.admin.dto.ClientRoleDto;
import com.tuoheng.admin.dto.ProfileResultDto;
import com.tuoheng.admin.dto.TokenResultDto;
import com.tuoheng.admin.service.oidc.OidcLoginService;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;
import java.util.List;

@Service
@Slf4j
public class OidcLoginServiceImpl implements OidcLoginService {

@Autowired
private RestTemplate restTemplate;

/**
* 根据第三方授权验证token 进行获取用户信息并跳转相关地址
*
* @return
*/
@Override
public JsonResult authorize() {
String username = OidcConstant.HLCG_USERNAME;
String password = OidcConstant.HLCG_PASSWORD;

//通过oidc的密码模式获取授权token 等相关信息数据
TokenResultDto tokenResult = getToken(username,password);
tokenResult.setUserName(username);
//此时通过token获取当前用户的相关权限信息并进行封装
tokenResult = getClientResult(tokenResult, username);
//数据封装完毕返回数据 以及相关地址 -> 是否重定向
return JsonResult.success(tokenResult);
}

/**
* 通过账号密码获取token
*/
private TokenResultDto getToken(String username, String password) {


String url = OidcConstant.OIDC_DOMAIN + OidcConstant.OAUTH2_TOKEN;
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("password", password);
//定死账号密码
params.add("username", username);
params.add("grant_type", "password");
params.add("scope", "openid profile");
//机场标识
String clientSecret = OidcConstant.CLIENT_SECRET.split("}")[1];
String userMsg = OidcConstant.CLIENT_ID + ":" + clientSecret;
String authorization = Base64.getEncoder().encodeToString(userMsg.getBytes());

ParameterizedTypeReference<TokenResultDto> parameterizedTypeReference =
new ParameterizedTypeReference<TokenResultDto>() {
};

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Authorization", "Basic " + authorization);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(params, headers);

ResponseEntity<TokenResultDto> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, parameterizedTypeReference);
return response.getBody();
}

/**
* 获取对应用户的相关信息
*
* @param tokenResult
* @return
*/
private TokenResultDto getClientResult(TokenResultDto tokenResult, String username) {

String url = OidcConstant.OIDC_DOMAIN + OidcConstant.GET_USERINFO;

ParameterizedTypeReference<JsonResult<TokenResultDto>> parameterizedTypeReference =
new ParameterizedTypeReference<JsonResult<TokenResultDto>>() {
};

HttpHeaders header = new HttpHeaders();
header.add("Authorization", "Bearer " + tokenResult.getAccess_token());
JSONObject object = new JSONObject();
object.put("username", tokenResult.getUserName());
HttpEntity httpEntity = new HttpEntity(object, header);

ResponseEntity<JsonResult<TokenResultDto>> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, parameterizedTypeReference);

TokenResultDto result = exchange.getBody().getData();
//封装数据
List<String> authorityList = result.getAuthorityList();
List<ClientRoleDto> clientRoleDtoList = result.getClientRoleDtoList();
tokenResult.setAuthorityList(authorityList);
tokenResult.setClientRoleDtoList(clientRoleDtoList);
tokenResult.setSub(result.getUserName());
tokenResult.setUserId(result.getUserId());
tokenResult.setUserName(result.getUserName());
ProfileResultDto profile = new ProfileResultDto();
BeanUtils.copyProperties(tokenResult, profile);
profile.setSub(username);
profile.setAzp(OidcConstant.CLIENT_ID);
profile.setAuthority(authorityList);
profile.setClientRoleList(clientRoleDtoList);
tokenResult.setProfile(profile);

return tokenResult;

}

}

Loading…
Cancel
Save