feat:接口自动注册进入x_permission表
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.xiang.xservice.auth.api.api;
|
||||
|
||||
import com.xiang.xservice.auth.api.dto.req.permission.PermissionRegisterRequest;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
public interface PermissionApi {
|
||||
|
||||
Result<Void> register(@RequestBody @NotEmpty(message = "请求参数不能为空") @Valid List<PermissionRegisterRequest> requests);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.xiang.xservice.auth.api.dto.req.permission;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PermissionRegisterRequest {
|
||||
|
||||
private String serviceName;
|
||||
/**
|
||||
* 类名+方法名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 服务名称+方法+接口地址
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* 接口地址
|
||||
*/
|
||||
private String apiPath;
|
||||
/**
|
||||
* 请求方式
|
||||
*/
|
||||
private String method;
|
||||
/**
|
||||
* 类型 接口为3
|
||||
*/
|
||||
private Integer type = 3; // 接口
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.xiang.xservice.auth.server.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.xiang.xservice.auth.api.api.PermissionApi;
|
||||
import com.xiang.xservice.auth.api.dto.req.permission.PermissionRegisterRequest;
|
||||
import com.xiang.xservice.auth.service.service.XPermissionService;
|
||||
import com.xiang.xservice.basic.common.resp.Result;
|
||||
import com.xiang.xservice.basic.exception.BusinessException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class PermissionController implements PermissionApi {
|
||||
|
||||
private final XPermissionService permissionService;
|
||||
|
||||
@PostMapping("/private/permission/register")
|
||||
public Result<Void> register(@RequestBody @NotEmpty(message = "请求参数不能为空") @Valid List<PermissionRegisterRequest> requests) {
|
||||
try {
|
||||
if (permissionService.registerAllApiPermissions(requests)) {
|
||||
return Result.success();
|
||||
}
|
||||
} catch (BusinessException e) {
|
||||
log.error("服务注册失败,请求参数:{}", JSON.toJSONString(requests), e);
|
||||
return Result.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.error("服务注册失败,请求参数:{}", JSON.toJSONString(requests), e);
|
||||
}
|
||||
return Result.error();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,9 @@ spring:
|
||||
active: local
|
||||
application:
|
||||
name: xservice-auth-center
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ant_path_matcher
|
||||
|
||||
mybatis:
|
||||
mapper-locations:
|
||||
|
||||
@@ -69,4 +69,9 @@ public class XPermission {
|
||||
* 删除标识(0:未删除 1:已删除)
|
||||
*/
|
||||
private Integer delFlag;
|
||||
|
||||
/**
|
||||
* 服务注册地址
|
||||
*/
|
||||
private String serviceName;
|
||||
}
|
||||
|
||||
@@ -25,4 +25,8 @@ public interface XPermissionMapper {
|
||||
int delBatch(@Param("list") List<Long> ids, @Param("operator") String operator);
|
||||
|
||||
List<PermissionRoleDTO> loadAllPermission();
|
||||
|
||||
int addBatch(@Param("list") List<XPermission> permissions);
|
||||
|
||||
int delByServiceName(@Param("serviceName") String serviceName);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.xiang.xservice.auth.service.service;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.xiang.xservice.auth.api.dto.req.permission.PermissionRegisterRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class ApiPermissionRegisterHandler implements ApplicationRunner {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String serviceName;
|
||||
|
||||
private final RequestMappingHandlerMapping handlerMapping;
|
||||
private final XPermissionService permissionService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<PermissionRegisterRequest> apis = Lists.newArrayList();
|
||||
|
||||
handlerMapping.getHandlerMethods().forEach((info, method) -> {
|
||||
|
||||
if (Objects.nonNull(info.getPatternsCondition())) {
|
||||
Set<String> patterns = info.getPatternsCondition().getPatterns();
|
||||
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
|
||||
|
||||
for (String url : patterns) {
|
||||
if ("/error".equals(url) || url.startsWith("/actuator")) {
|
||||
continue;
|
||||
}
|
||||
for (RequestMethod httpMethod : methods.isEmpty() ? Set.of(RequestMethod.GET) : methods) {
|
||||
PermissionRegisterRequest dto = new PermissionRegisterRequest();
|
||||
dto.setName(method.getBeanType().getName() + ":" + method.getMethod().getName());
|
||||
dto.setApiPath(url);
|
||||
dto.setMethod(httpMethod.name());
|
||||
dto.setCode(serviceName + ":" + httpMethod + ":" + url);
|
||||
dto.setType(3);
|
||||
dto.setServiceName(serviceName);
|
||||
apis.add(dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (CollectionUtils.isNotEmpty(apis)) {
|
||||
permissionService.registerAllApiPermissions(apis);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.xiang.xservice.auth.service.service;
|
||||
|
||||
import com.xiang.xservice.auth.api.dto.req.permission.PermissionRegisterRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface XPermissionService {
|
||||
|
||||
/**
|
||||
* 注册所有微服务的apiPath
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
Boolean registerAllApiPermissions(List<PermissionRegisterRequest> request);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.xiang.xservice.auth.service.service.impl;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.xiang.xservice.auth.api.dto.req.permission.PermissionRegisterRequest;
|
||||
import com.xiang.xservice.auth.service.entity.XPermission;
|
||||
import com.xiang.xservice.auth.service.repository.mapper.XPermissionMapper;
|
||||
import com.xiang.xservice.auth.service.service.XPermissionService;
|
||||
import com.xiang.xservice.basic.enums.DelStatusEnum;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class XPermissionServiceImpl implements XPermissionService {
|
||||
|
||||
private final XPermissionMapper permissionMapper;
|
||||
|
||||
@Override
|
||||
public Boolean registerAllApiPermissions(List<PermissionRegisterRequest> request) {
|
||||
Set<String> serviceNameSet = request.stream().map(PermissionRegisterRequest::getServiceName).collect(Collectors.toSet());
|
||||
for (String serviceName : serviceNameSet) {
|
||||
permissionMapper.delByServiceName(serviceName);
|
||||
}
|
||||
List<XPermission> params = Lists.newArrayList();
|
||||
for (PermissionRegisterRequest permissionRegisterRequest : request) {
|
||||
XPermission permission = new XPermission();
|
||||
permission.setName(permissionRegisterRequest.getName());
|
||||
permission.setCode(permissionRegisterRequest.getCode());
|
||||
permission.setType(3);
|
||||
permission.setApiPath(permissionRegisterRequest.getApiPath());
|
||||
permission.setMethod(permissionRegisterRequest.getMethod());
|
||||
permission.setCreatedTime(LocalDateTime.now());
|
||||
permission.setCreateBy(permissionRegisterRequest.getServiceName());
|
||||
permission.setUpdatedTime(LocalDateTime.now());
|
||||
permission.setUpdateBy(permissionRegisterRequest.getServiceName());
|
||||
permission.setDelFlag(DelStatusEnum.NOT_DELETED.getCode());
|
||||
permission.setServiceName(permissionRegisterRequest.getServiceName());
|
||||
params.add(permission);
|
||||
}
|
||||
boolean flag = true;
|
||||
if (CollectionUtils.isNotEmpty(params)) {
|
||||
if (params.size() > 200) {
|
||||
for (List<XPermission> permissions : Lists.partition(params, 200)) {
|
||||
if (permissionMapper.addBatch(permissions) <= 0) {
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return permissionMapper.addBatch(params) > 0;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
<result column="updated_time" property="updatedTime" />
|
||||
<result column="update_by" property="updateBy" />
|
||||
<result column="del_flag" property="delFlag" />
|
||||
<result column="service_name" property="serviceName"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
@@ -30,7 +31,8 @@
|
||||
create_by,
|
||||
updated_time,
|
||||
update_by,
|
||||
del_flag
|
||||
del_flag,
|
||||
service_name
|
||||
</sql>
|
||||
|
||||
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.xiang.xservice.auth.service.entity.XPermission">
|
||||
@@ -67,7 +69,10 @@
|
||||
update_by,
|
||||
</if>
|
||||
<if test="null != delFlag ">
|
||||
del_flag
|
||||
del_flag,
|
||||
</if>
|
||||
<if test="serviceName != null and serviceName != ''">
|
||||
service_name
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
@@ -102,10 +107,22 @@
|
||||
#{updateBy},
|
||||
</if>
|
||||
<if test="null != delFlag ">
|
||||
#{delFlag}
|
||||
#{delFlag},
|
||||
</if>
|
||||
<if test="serviceName != null and serviceName != ''">
|
||||
#{serviceName}
|
||||
</if>
|
||||
</trim>
|
||||
</insert>
|
||||
<insert id="addBatch">
|
||||
insert into x_permission(name, code, type, api_path, method, created_time,
|
||||
create_by, updated_time, update_by, service_name)
|
||||
values
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.name}, #{item.code}, #{item.type}, #{item.apiPath}, #{item.method}, #{item.createdTime},
|
||||
#{item.createBy}, #{item.updatedTime}, #{item.updateBy}, #{item.serviceName})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<update id="delBatch" >
|
||||
update x_permission set del_flag = 1, update_by = #{operator}, update_time = NOW()
|
||||
@@ -128,10 +145,14 @@
|
||||
<if test="null != createBy and '' != createBy">create_by = #{createBy},</if>
|
||||
<if test="null != updatedTime ">updated_time = #{updatedTime},</if>
|
||||
<if test="null != updateBy and '' != updateBy">update_by = #{updateBy},</if>
|
||||
<if test="null != delFlag ">del_flag = #{delFlag}</if>
|
||||
<if test="null != delFlag ">del_flag = #{delFlag},</if>
|
||||
<if test="null != serviceName and serviceName != ''">service_name = #{serviceName}</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
<delete id="delByServiceName">
|
||||
delete from x_permission where service_name = #{serviceName}
|
||||
</delete>
|
||||
<select id="getPermissionList" resultMap="BaseResultMap">
|
||||
select <include refid="Base_Column_List"/>
|
||||
from x_permission
|
||||
|
||||
Reference in New Issue
Block a user