Browse Source

新增平台表,更新添加租户接口

tags/v2.4.0
xiaoying 1 year ago
parent
commit
499376ebbb
20 changed files with 777 additions and 25 deletions
  1. +11
    -0
      tuoheng_oidc_admin/pom.xml
  2. +23
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ExceptionInterface.java
  3. +45
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ServiceException.java
  4. +32
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/HeadClientHttpRequestInterceptor.java
  5. +64
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/RestProperties.java
  6. +176
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/RestTemplateConfig.java
  7. +15
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/constant/HhzUrlConstant.java
  8. +29
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/PlatformController.java
  9. +10
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java
  10. +20
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/PlatformMapper.java
  11. +4
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/TenantMapper.java
  12. +32
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/OidcTenantDto.java
  13. +66
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/Platform.java
  14. +67
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/TTenant.java
  15. +18
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/service/PlatformService.java
  16. +7
    -1
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/ClientUserSevice.java
  17. +84
    -23
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/ClientUserServiceImpl.java
  18. +43
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/PlatformServiceImpl.java
  19. +24
    -0
      tuoheng_oidc_admin/src/main/resources/mapper/PlatformMapper.xml
  20. +7
    -1
      tuoheng_oidc_admin/src/main/resources/mapper/TenantMapper.xml

+ 11
- 0
tuoheng_oidc_admin/pom.xml View File

@@ -32,6 +32,17 @@
<scope>test</scope>
</dependency>

<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.4.4</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>org.springframework.boot</groupId>

+ 23
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ExceptionInterface.java View File

@@ -0,0 +1,23 @@
package com.tuoheng.common;

/**
* 枚举类 封装
*
* @author zhu_zishuang
* @date 2021-03-12
*/
public interface ExceptionInterface {
/**
* 获取错误码
*
* @return
*/
int getCode();

/**
* 获取异常信息
*
* @return
*/
String getMessage();
}

+ 45
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ServiceException.java View File

@@ -0,0 +1,45 @@
package com.tuoheng.common;

/**
* 业务异常类(业务处理时手动抛出异常)
*
* @author zhu_zishuang
* @date 2021-03-12
*/
public class ServiceException extends RuntimeException {

/**
* 异常码
*/
public final int code;

/**
* 异常描述,兼容JsonResult
*/
private String msg;

/**
* 构造器
*
* @param exceptionInfo
*/
public ServiceException(ExceptionInterface exceptionInfo) {
super(exceptionInfo.getMessage());
this.msg = exceptionInfo.getMessage();
this.code = exceptionInfo.getCode();
}

/**
* 构造器
*
* @param code
* @param message
*/
public ServiceException(int code, String message) {
super(message);
this.msg = message;
this.code = code;
}

}


+ 32
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/HeadClientHttpRequestInterceptor.java View File

@@ -0,0 +1,32 @@
package com.tuoheng.config.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
* 默认拦截器
*/
public class HeadClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(HeadClientHttpRequestInterceptor.class);

@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
LOGGER.info("#####head handle########");
HttpHeaders headers = httpRequest.getHeaders();
headers.add("Accept", "application/json");
headers.add("Accept-Encoding", "gzip");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
ClientHttpResponse response = clientHttpRequestExecution.execute(httpRequest, bytes);
HttpHeaders headersResponse = response.getHeaders();
headersResponse.add("Accept", "application/json");
return response;
}
}

+ 64
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/RestProperties.java View File

@@ -0,0 +1,64 @@
package com.tuoheng.config.http;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
@Setter
public class RestProperties {

public final static String HTTP = "http";

public final static String HTTPS = "https";

/**
* 连接超时, 默认10秒
*/
@Value("${spring.http.restTemplate.connect.timeout: 10000}")
private Integer connectTimeout;

/**
* 响应超时, 默认2分钟
*/
@Value("${spring.http.restTemplate.read.timeout: 120000}")
private Integer readTimeout;

/**
* 请求超时时间, 默认10秒
*/
@Value("${spring.http.restTemplate.connection.request.timeout: 10000}")
private Integer connectionRequestTimeout;

/**
* 请求失败重试次数, 默认重试3次
*/
@Value("${spring.http.restTemplate.retryCount: 3}")
private Integer retryCount;

/**
* 请求失败重试开关,默认开启
*/
@Value("${spring.http.restTemplate.requestSentRetryEnabled: true}")
private Boolean requestSentRetryEnabled;

/**
* 线程池最大连接数,默认1000
*/
@Value("${spring.http.restTemplate.pool.maxTotal: 1000}")
private Integer maxTotal;

/**
* 线程池主机最大并发数,默认100
*/
@Value("${spring.http.restTemplate.pool.maxPerRoute: 100}")
private Integer maxPerRoute;

/**
* 线程池空闲连接过期时间,默认60秒
*/
@Value("${spring.http.restTemplate.pool.validateAfterInactivity: 60000}")
private Integer validateAfterInactivity;
}

+ 176
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/http/RestTemplateConfig.java View File

@@ -0,0 +1,176 @@
package com.tuoheng.config.http;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class RestTemplateConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateConfig.class);

@Bean
public RestTemplate restTemplate(RestProperties restProperties) {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory(restProperties));
// 获取restTemplate中的转换器集合
List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
// 遍历转换器集合,找到对应的StringHttpMessageConverter转换器
for (HttpMessageConverter<?> converter : converterList) {
if(converter.getClass() == StringHttpMessageConverter.class){
converterList.remove(converter);
break;
}
}
// 添加新的StringHttpMessageConverter转换器,并设置字符集为UTF-8
converterList.add(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
// 添加拦截器
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
// interceptors.add(new DefaultClientHttpRequestInterceptor());
interceptors.add(new HeadClientHttpRequestInterceptor());
restTemplate.setInterceptors(interceptors);
return restTemplate;
}

private ClientHttpRequestFactory clientHttpRequestFactory(RestProperties restProperties) {
// httpClientBuilder配置构架器
HttpClientBuilder httpClientBuilder = HttpClients.custom();
// 设置重试次数,此处注意,如果使用无参构造,重试次数为3。this(3, false);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(restProperties.getRetryCount(), restProperties.getRequestSentRetryEnabled()));
// 设置保持长连接
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
// 使用连接池
httpClientBuilder.setConnectionManager(poolingConnectionManager(restProperties));
// 获取httpClient
CloseableHttpClient httpClient = httpClientBuilder.build();
// 配置HttpClient的对应工厂HttpComponentsClientHttpRequestFactory
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(restProperties.getConnectTimeout());
factory.setReadTimeout(restProperties.getReadTimeout());
factory.setConnectionRequestTimeout(restProperties.getConnectionRequestTimeout());
return factory;
}

private HttpClientConnectionManager poolingConnectionManager(RestProperties restProperties) {
// 注册http和https请求
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(RestProperties.HTTP, PlainConnectionSocketFactory.getSocketFactory())
.register(RestProperties.HTTPS, createSSLConn())
.build();
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(registry);
// 最大连接数
poolingConnectionManager.setMaxTotal(restProperties.getMaxTotal());
// 每个主机的并发
poolingConnectionManager.setDefaultMaxPerRoute(restProperties.getMaxPerRoute());
// 空闲连接过期时间
poolingConnectionManager.setValidateAfterInactivity(restProperties.getValidateAfterInactivity());
return poolingConnectionManager;
}

private SSLConnectionSocketFactory createSSLConn() {
SSLConnectionSocketFactory sslsf = null;
try
{
SSLContext sslContext = new SSLContextBuilder()
// 不检查证书
.loadTrustMaterial(null, (X509Certificate[] chain, String authType) -> true)
.build();
sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); // 不检查hostname
} catch (GeneralSecurityException e){
LOGGER.error("restTemplate开启SSL校验失败, error:{}", e);
}
return sslsf;
}


/**
* 自定义的重试策略,需要的时候使用
*/
private void customHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled){
// 请求失败时,进行请求重试
HttpRequestRetryHandler handler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException e, int currentRetryCount, HttpContext httpContext) {
if(!requestSentRetryEnabled){
return false;
}
if (currentRetryCount > retryCount){
// 重试超过3次,放弃请求
LOGGER.error("retry has more than 3 time, give up request");
return false;
}
if (e instanceof NoHttpResponseException){
// 服务器没有响应,可能是服务器断开了连接,应该重试
LOGGER.error("receive no response from server, retry");
return true;
}
if (e instanceof SSLHandshakeException){
// SSL握手异常
LOGGER.error("SSL hand shake exception");
return false;
}
if (e instanceof InterruptedIOException){
// 超时
LOGGER.error("InterruptedIOException");
return false;
}
if (e instanceof UnknownHostException){
// 服务器不可达
LOGGER.error("server host unknown");
return false;
}
if (e instanceof ConnectTimeoutException){
// 连接超时
LOGGER.error("Connection Time out");
return false;
}
if (e instanceof SSLException){
LOGGER.error("SSLException");
return false;
}
HttpClientContext context = HttpClientContext.adapt(httpContext);
HttpRequest request = context.getRequest();
if (!(request instanceof HttpEntityEnclosingRequest)){
// 如果请求不是关闭连接的请求
return true;

}
return false;
}
};
}
}

+ 15
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/constant/HhzUrlConstant.java View File

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

/**
* hhz平台接口常量url
* @Author xiaoying
* @Date 2022/10/18 10:52
*/
public class HhzUrlConstant {

/**
* 创建租户
*/
public static String CREATE_TENANT = "/oidcTenant/add";

}

+ 29
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/PlatformController.java View File

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

import com.tuoheng.model.service.PlatformService;
import com.tuoheng.until.JsonResult;
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;

/**
* @Author xiaoying
* @Date 2023/1/9 14:30
*/
@RestController
@RequestMapping("/platform")
public class PlatformController {

@Autowired
private PlatformService platformService;

/**
* 查询各平台名称
* @return
*/
@GetMapping("/list")
public JsonResult list(){
return platformService.findAll();
}
}

+ 10
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/TenantController.java View File

@@ -1,6 +1,7 @@
package com.tuoheng.controller;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.dto.OidcTenantDto;
import com.tuoheng.model.param.CreateClientTenantDto;
import com.tuoheng.model.param.CreateClientUserDto;
import com.tuoheng.service.ClientUserSevice;
@@ -32,4 +33,13 @@ public class TenantController {
return clientUserSevice.createClientTenant(createClientTenantDto, loginUser);
}

/**
* 新增业务平台的租户基本数据
* @param dto
* @return
*/
@PostMapping("/add")
public JsonResult add(@RequestBody OidcTenantDto dto ){
return clientUserSevice.addTenant(dto);
}
}

+ 20
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/PlatformMapper.java View File

@@ -0,0 +1,20 @@
package com.tuoheng.mapper;

import com.tuoheng.model.dto.Platform;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
* @description 针对表【platform(平台表)】的数据库操作Mapper
* @createDate 2023-01-09 11:09:18
* @Entity com.tuoheng.model.dto.Platform
*/
@Mapper
public interface PlatformMapper extends BaseMapper<Platform> {

}





+ 4
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/TenantMapper.java View File

@@ -1,7 +1,10 @@
package com.tuoheng.mapper;

import com.tuoheng.model.dto.TTenant;
import com.tuoheng.model.po.TenantPo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
* @author chenjiandong
@@ -13,4 +16,5 @@ public interface TenantMapper {

int insertTenant(TenantPo tenantPo);

TTenant getByCode(@Param("code") String code);
}

+ 32
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/OidcTenantDto.java View File

@@ -0,0 +1,32 @@
package com.tuoheng.model.dto;

import lombok.Data;

/**
* oidc-新增租户第三方
* @Author xiaoying
* @Date 2023/1/9 10:08
*/
@Data
public class OidcTenantDto {
/**
* 用户名
*/
private String username;
/**
* 租户名称
*/
private String name;
/**
* 平台
*/
private String clientId;
/**
* 密码
*/
private String password;
/**
* 租户code
*/
private String code;
}

+ 66
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/Platform.java View File

@@ -0,0 +1,66 @@
package com.tuoheng.model.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
* 平台表
* @TableName platform
*/
@TableName(value ="platform")
@Data
public class Platform implements Serializable {
/**
* 主键
*/
@TableId
private String id;

/**
* 平台编码
*/
private String platformCode;

/**
* 平台名称
*/
private String platformName;

/**
* 平台地址
*/
private String platformUrl;

/**
* 添加人
*/
private String createUser;

/**
* 创建时间
*/
private Date createTime;

/**
* 更新人
*/
private String updateUser;

/**
* 更新时间
*/
private Date updateTime;

/**
* 有效标识
*/
private Integer mark;

@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

+ 67
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/TTenant.java View File

@@ -0,0 +1,67 @@
package com.tuoheng.model.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
*
* @TableName t_tenant
*/
@TableName(value ="t_tenant")
@Data
public class TTenant implements Serializable {
/**
*
*/
@TableId(type = IdType.AUTO)
private Long id;

/**
* 用户名
*/
@TableId
private String userId;

/**
*
*/
private Date createTime;

/**
*
*/
private Date updateTime;

/**
*
*/
private Long createUser;

/**
*
*/
private Long updateUser;

/**
* 租户备注信息
*/
private String remark;

/**
* 租户code
*/
private String code;

/**
* 1:正常租户;0:不可使用
*/
private Integer enabled;

@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

+ 18
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/service/PlatformService.java View File

@@ -0,0 +1,18 @@
package com.tuoheng.model.service;

import com.tuoheng.model.dto.Platform;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tuoheng.until.JsonResult;

/**
* @author 小影
* @description 针对表【platform(平台表)】的数据库操作Service
* @createDate 2023-01-09 11:09:18
*/
public interface PlatformService extends IService<Platform> {
/**
* 查询各平台名称
* @return
*/
JsonResult findAll();
}

+ 7
- 1
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/ClientUserSevice.java View File

@@ -1,6 +1,7 @@
package com.tuoheng.service;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.dto.OidcTenantDto;
import com.tuoheng.model.param.CreateClientTenantDto;
import com.tuoheng.model.param.CreateClientUserDto;
import com.tuoheng.model.param.UpdateUserClientRoleDto;
@@ -24,5 +25,10 @@ public interface ClientUserSevice {

JsonResult updateUserClientRole(UpdateUserClientRoleDto updateUserClientRoleDto, LoginUser loginUser);


/**
* 新增业务平台的租户基本数据
* @param dto
* @return
*/
JsonResult addTenant(OidcTenantDto dto);
}

+ 84
- 23
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/ClientUserServiceImpl.java View File

@@ -1,10 +1,14 @@
package com.tuoheng.service.impl;

import com.tuoheng.mapper.AuthoritiesMapper;
import com.tuoheng.mapper.ClientUserMapper;
import com.tuoheng.mapper.ClientUserRoleMapper;
import com.tuoheng.mapper.TenantMapper;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.common.ServiceException;
import com.tuoheng.constant.HhzUrlConstant;
import com.tuoheng.mapper.*;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.dto.OidcTenantDto;
import com.tuoheng.model.dto.Platform;
import com.tuoheng.model.dto.TTenant;
import com.tuoheng.model.param.*;
import com.tuoheng.model.po.AuthoritiesPo;
import com.tuoheng.model.po.ClientUserRolePo;
@@ -12,10 +16,13 @@ import com.tuoheng.model.po.TenantPo;
import com.tuoheng.model.po.UserPo;
import com.tuoheng.service.ClientUserSevice;
import com.tuoheng.until.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
@@ -26,6 +33,7 @@ import java.util.List;
* @date 2022/10/8 11:35
*/
@Service
@Slf4j
public class ClientUserServiceImpl implements ClientUserSevice {

@Autowired
@@ -40,10 +48,16 @@ public class ClientUserServiceImpl implements ClientUserSevice {
@Autowired
private ClientUserRoleMapper clientUserRoleMapper;

@Autowired
private RestTemplate restTemplate;

@Autowired
private PlatformMapper platformMapper;

@Override
@Transactional(readOnly = true)
public JsonResult judgeCreate(String username){
if(clientUserMapper.judgeCreateByUserName(username) > 0){
public JsonResult judgeCreate(String username) {
if (clientUserMapper.judgeCreateByUserName(username) > 0) {
return JsonResult.success(false);
}
return JsonResult.success(true);
@@ -51,9 +65,9 @@ public class ClientUserServiceImpl implements ClientUserSevice {

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult createClientUser(CreateClientUserDto createClientUserDto, LoginUser loginUser){
public JsonResult createClientUser(CreateClientUserDto createClientUserDto, LoginUser loginUser) {

if(clientUserMapper.judgeCreateByUserName(createClientUserDto.getUsername()) > 0){
if (clientUserMapper.judgeCreateByUserName(createClientUserDto.getUsername()) > 0) {
return JsonResult.error("该用户名称已存在!");
}

@@ -62,21 +76,21 @@ public class ClientUserServiceImpl implements ClientUserSevice {
.setPassword("{bcrypt}" + new BCryptPasswordEncoder().encode(createClientUserDto.getPassword()));
userPo.setCreateUser(loginUser.getUserId());
//租户逻辑新增 start
if(createClientUserDto.getTenantFlag() != null){
if(createClientUserDto.getTenantFlag() == 1){
if (createClientUserDto.getTenantFlag() != null) {
if (createClientUserDto.getTenantFlag() == 1) {
userPo.setIsTenant(1);
}else {
} else {
userPo.setIsTenant(0);
UserPo po = clientUserMapper.getUserByUserName(createClientUserDto.getTenantName());
if(po != null){
if (po != null) {
userPo.setTenantId(po.getId());
}
}
}
//租户逻辑新增 end
clientUserMapper.insertClientUser(userPo);
if(createClientUserDto.getTenantFlag() != null){
if(createClientUserDto.getTenantFlag() == 1){
if (createClientUserDto.getTenantFlag() != null) {
if (createClientUserDto.getTenantFlag() == 1) {
TenantPo tenantPo = new TenantPo()
.setUserId(userPo.getId());
tenantMapper.insertTenant(tenantPo);
@@ -84,7 +98,7 @@ public class ClientUserServiceImpl implements ClientUserSevice {
}
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
for(ClientRoleDto clientRoleDto : createClientUserDto.getClientRoleDtoList()){
for (ClientRoleDto clientRoleDto : createClientUserDto.getClientRoleDtoList()) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientUserDto.getUsername())
@@ -108,9 +122,9 @@ public class ClientUserServiceImpl implements ClientUserSevice {

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser){
public JsonResult createClientTenant(CreateClientTenantDto createClientTenantDto, LoginUser loginUser) {

if(clientUserMapper.judgeCreateByUserName(createClientTenantDto.getUsername()) > 0){
if (clientUserMapper.judgeCreateByUserName(createClientTenantDto.getUsername()) > 0) {
return JsonResult.error("该用户名称已存在!");
}
UserPo userPo = new UserPo()
@@ -125,7 +139,7 @@ public class ClientUserServiceImpl implements ClientUserSevice {
tenantMapper.insertTenant(tenantPo);
List<AuthoritiesPo> authoritiesPos = new ArrayList<>();
List<ClientUserRolePo> clientUserRolePoArrayList = new ArrayList<>();
for(ClientRoleDto clientRoleDto : createClientTenantDto.getClientRoleDtoList()){
for (ClientRoleDto clientRoleDto : createClientTenantDto.getClientRoleDtoList()) {
AuthoritiesPo authoritiesPo = new AuthoritiesPo()
.setUserId(userPo.getId())
.setUsername(createClientTenantDto.getUsername())
@@ -148,9 +162,9 @@ public class ClientUserServiceImpl implements ClientUserSevice {

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult updateUserPassword(UpdateUserPassDto updateUserPassDto, LoginUser loginUser){
public JsonResult updateUserPassword(UpdateUserPassDto updateUserPassDto, LoginUser loginUser) {

if(clientUserMapper.getUserByUserName(updateUserPassDto.getUsername()) == null){
if (clientUserMapper.getUserByUserName(updateUserPassDto.getUsername()) == null) {
return JsonResult.error("该用户不存在!");
}

@@ -164,13 +178,13 @@ public class ClientUserServiceImpl implements ClientUserSevice {

@Override
@Transactional(rollbackFor = Exception.class)
public JsonResult updateUserClientRole(UpdateUserClientRoleDto updateUserClientRoleDto, LoginUser loginUser){
public JsonResult updateUserClientRole(UpdateUserClientRoleDto updateUserClientRoleDto, LoginUser loginUser) {
UserPo userPo = clientUserMapper.getUserByUserName(updateUserClientRoleDto.getUsername());
if(userPo == null){
if (userPo == null) {
return JsonResult.error("该用户不存在!");
}
List<ClientRoleDto> clientRoleDtoList = updateUserClientRoleDto.getClientRoleDtoList();
for(ClientRoleDto dto : clientRoleDtoList){
for (ClientRoleDto dto : clientRoleDtoList) {
ClientUserRolePo clientUserRolePo = new ClientUserRolePo()
.setUserId(userPo.getId())
.setClientId(dto.getClientId())
@@ -181,4 +195,51 @@ public class ClientUserServiceImpl implements ClientUserSevice {
return JsonResult.success(true);
}

/**
* 新增业务平台的租户基本数据
*
* @param dto
* @return
*/
@Override
public JsonResult addTenant(OidcTenantDto dto) {

if (ObjectUtil.isEmpty(dto.getCode())) {
return JsonResult.error("租户code不能为空");
}
TTenant tTenant = tenantMapper.getByCode(dto.getCode());
if (ObjectUtil.isNotNull(tTenant)) {
return JsonResult.error("该租户code已存在,请重新输入");
}

Platform platform = platformMapper.selectOne(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getPlatformCode, dto.getClientId())
.eq(Platform::getMark, 1));
if (ObjectUtil.isNull(platform)) {
return JsonResult.error("该业务平台不存在");
}
//设置请求头
HttpHeaders resultRequestHeader = new HttpHeaders();
HttpEntity httpEntity = new HttpEntity(dto, resultRequestHeader);
//设置地址(hhz平台)
String url = platform.getPlatformUrl() + HhzUrlConstant.CREATE_TENANT;
ResponseEntity<JsonResult> response;
try {
response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JsonResult.class);
} catch (Exception e) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (null == response || !response.hasBody()) {
log.error("业务平台新增租户响应失败");
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "业务平台新增租户失败");
}
if (response.getBody().getCode() != JsonResult.SUCCESS) {
log.error("业务平台新增租户响应失败" + response.getBody());
return JsonResult.error(response.getBody().getMsg());
}

return JsonResult.success();
}


}

+ 43
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/service/impl/PlatformServiceImpl.java View File

@@ -0,0 +1,43 @@
package com.tuoheng.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.model.dto.Platform;
import com.tuoheng.model.service.PlatformService;
import com.tuoheng.mapper.PlatformMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 小影
* @description 针对表【platform(平台表)】的数据库操作Service实现
* @createDate 2023-01-09 11:09:18
*/
@Service
public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform>
implements PlatformService{

@Autowired
private PlatformMapper platformMapper;

/**
* 查询
*
* @return
*/
@Override
public JsonResult findAll() {

List<Platform> platforms = platformMapper.selectList(Wrappers.<Platform>lambdaQuery()
.eq(Platform::getMark, 1));

return JsonResult.success(platforms);
}
}





+ 24
- 0
tuoheng_oidc_admin/src/main/resources/mapper/PlatformMapper.xml View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tuoheng.mapper.PlatformMapper">

<resultMap id="BaseResultMap" type="com.tuoheng.model.dto.Platform">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="platformCode" column="platform_code" jdbcType="VARCHAR"/>
<result property="platformName" column="platform_name" jdbcType="VARCHAR"/>
<result property="platformUrl" column="platform_url" jdbcType="VARCHAR"/>
<result property="createUser" column="create_user" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updateUser" column="update_user" jdbcType="VARCHAR"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="mark" column="mark" jdbcType="TINYINT"/>
</resultMap>

<sql id="Base_Column_List">
id,platform_code,platform_name,
platform_url,create_user,create_time,
update_user,update_time,mark
</sql>
</mapper>

+ 7
- 1
tuoheng_oidc_admin/src/main/resources/mapper/TenantMapper.xml View File

@@ -3,7 +3,13 @@
<mapper namespace="com.tuoheng.mapper.TenantMapper">

<insert id="insertTenant" parameterType="com.tuoheng.model.po.TenantPo" keyProperty="id" useGeneratedKeys="true">
insert into t_tenant (user_id, remark) values (#{userId}, #{remark})
insert into t_tenant (user_id, remark)
values (#{userId}, #{remark})
</insert>

<select id="getByCode" resultType="com.tuoheng.model.dto.TTenant">
select *
from tuoheng_oidc.t_tenant
where code = #{code}
</select>
</mapper>

Loading…
Cancel
Save