mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
自动扫描url资源
This commit is contained in:
@@ -1,22 +1,27 @@
|
||||
package cn.lihongjie.coal.entity;
|
||||
|
||||
import cn.lihongjie.coal.entity.base.CommonEntity;
|
||||
import io.vavr.collection.Stream;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Comment("资源")
|
||||
@Slf4j
|
||||
public class ResourceEntity extends CommonEntity {
|
||||
|
||||
@ManyToMany
|
||||
private List<PermissionEntity> permissions;
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "parent")
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
|
||||
private List<ResourceEntity> children;
|
||||
|
||||
@ManyToOne
|
||||
@@ -28,28 +33,91 @@ public class ResourceEntity extends CommonEntity {
|
||||
private String type;
|
||||
|
||||
|
||||
|
||||
@Comment("资源地址")
|
||||
private String url;
|
||||
|
||||
|
||||
|
||||
@Comment("需要机构管理员权限")
|
||||
private Boolean orgAdmin;
|
||||
|
||||
|
||||
|
||||
@Comment("需要系统管理员权限")
|
||||
private Boolean sysAdmin;
|
||||
|
||||
|
||||
|
||||
@Comment("匿名用户可以访问")
|
||||
private Boolean anonymous;
|
||||
|
||||
|
||||
public void addChildren(ResourceEntity entity) {
|
||||
|
||||
log.info("addChildren {} {}", this.getUrl(), entity.getUrl());
|
||||
if (this.children == null) {
|
||||
this.children = new ArrayList<>();
|
||||
}
|
||||
|
||||
if (entity.getUrl().equals(this.getUrl())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
for (ResourceEntity child : this.children) {
|
||||
|
||||
if (this.getUrl().startsWith(child.getUrl())) {
|
||||
|
||||
|
||||
child.addChildren(entity);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
String parent = getParent(entity.url);
|
||||
|
||||
if (isSamePath(parent, this.getUrl())) {
|
||||
|
||||
this.children.add(entity);
|
||||
entity.setParent(this);
|
||||
|
||||
} else {
|
||||
|
||||
ResourceEntity r = new ResourceEntity();
|
||||
|
||||
String tmp = entity.getUrl();
|
||||
|
||||
while (!isSamePath(this.getUrl(), getParent(tmp))) {
|
||||
tmp = getParent(tmp);
|
||||
}
|
||||
|
||||
|
||||
r.setUrl(tmp);
|
||||
|
||||
r.setName(r.getUrl());
|
||||
r.setType("3");
|
||||
r.setParent(this);
|
||||
r.setChildren(new ArrayList<>());
|
||||
r.setCode(r.getUrl());
|
||||
|
||||
|
||||
this.children.add(r);
|
||||
|
||||
|
||||
r.addChildren(entity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private boolean isSamePath(String a, String b) {
|
||||
return a.equalsIgnoreCase(b) || (a + "/").equalsIgnoreCase(b);
|
||||
}
|
||||
|
||||
private static String getParent(String path) {
|
||||
return "/" + StringUtils.join(Stream.of(StringUtils.split(path, "/")).dropRight(1), "/");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.annotations.*;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -50,12 +49,20 @@ public class BaseEntity {
|
||||
|
||||
this.createUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : "";
|
||||
}
|
||||
|
||||
if (this.createTime == null) {
|
||||
this.createTime = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void preUpdate() {
|
||||
|
||||
this.updateUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : "";
|
||||
|
||||
if (this.updateTime == null) {
|
||||
this.updateTime = LocalDateTime.now();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public class InitDataRunner implements CommandLineRunner {
|
||||
|
||||
coalParameterDefService.initDefault();
|
||||
|
||||
// resourceService.initUrlResource();
|
||||
resourceService.initUrlResource();
|
||||
|
||||
} finally {
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
@@ -5,12 +5,13 @@ import cn.lihongjie.coal.dto.*;
|
||||
import cn.lihongjie.coal.entity.ResourceEntity;
|
||||
import cn.lihongjie.coal.mapper.ResourceMapper;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -18,6 +19,7 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
@@ -27,7 +29,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -110,19 +111,58 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
|
||||
@Autowired
|
||||
RequestMappingHandlerMapping requestMappingHandlerMapping;
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager entityManager;
|
||||
|
||||
@Transactional
|
||||
public void initUrlResource(){
|
||||
|
||||
List<ResourceEntity> all = findAll().stream().filter(x -> StringUtils.equals(x.getType(), "3")).toList();
|
||||
List<ResourceEntity> all = findAll(new Specification<ResourceEntity>() {
|
||||
@Override
|
||||
public Predicate toPredicate(Root<ResourceEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
|
||||
return criteriaBuilder.and(criteriaBuilder.equal(root.get("url"), "/"), criteriaBuilder.equal(root.get("type"), "3"));
|
||||
}
|
||||
});
|
||||
|
||||
Map<String, ResourceEntity> map = all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e));
|
||||
ResourceEntity root = null;
|
||||
|
||||
if (all.isEmpty()) {
|
||||
|
||||
ResourceEntity entity = new ResourceEntity();
|
||||
entity.setCode("/");
|
||||
entity.setType("3");
|
||||
entity.setName("接口资源");
|
||||
entity.setUrl("/");
|
||||
this.save(entity);
|
||||
root = entity;
|
||||
}else {
|
||||
|
||||
root = all.get(0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
getAllUrls();
|
||||
|
||||
List<String> allUrls = getAllUrls();
|
||||
|
||||
|
||||
for (String allUrl : allUrls) {
|
||||
ResourceEntity entity = new ResourceEntity();
|
||||
entity.setCode(allUrl);
|
||||
entity.setType("3");
|
||||
entity.setName("");
|
||||
entity.setUrl(allUrl);
|
||||
root.addChildren(entity);
|
||||
|
||||
}
|
||||
|
||||
|
||||
this.save(root);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void getAllUrls() {
|
||||
private List<String> getAllUrls() {
|
||||
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
|
||||
List<String> urls = new ArrayList<>();
|
||||
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
|
||||
@@ -135,9 +175,12 @@ public class ResourceService extends BaseService<ResourceEntity, ResourceReposit
|
||||
}
|
||||
for (PathPattern pattern : info.getPathPatternsCondition().getPatterns()) {
|
||||
String ps = pattern.getPatternString();
|
||||
|
||||
urls.add(ps);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return urls;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user