配合自定义注解和Swagger2注解进行权限资源初始化。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util. s;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.crown.common.annotations.Resources;
import org.crown.model.entity.Resource;
import org.spring work.beans.factory.annotation.Autowired;
import org.spring work.stereotype.Service;
import org.spring work.web.bind.annotation.RequestMapping;
import org.spring work.web.bind.annotation.RequestMethod;
import org.spring work.web.method.HandlerMethod;
import org.spring work.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import io.swagger.annotations.ApiOperation;
import liqui .util.MD5Util;
/**
* <p>
* 服务mapping扫描
* </p>
*
* @author Caratacus
*/
@Service
public class ScanMappings {
@Autowired
private IResourceService resourceService;
@Autowired
private RequestMappingHandlerMapping handlerMapping;
private String[] emptyArray = new String[]{\"\"};
/**
* 扫描资源插入数据库
*/
@PostConstruct
public void doScan() {
resourceService.saveOrUpdateBatch(
handlerMapping.getHandlerMethods()
.values()
.stream()
.map(this::getResources)
.flatMap(Collection::stream)
.collect(Collectors.toList())
);
}
/**
* 获取Resource
*
* @param handlerMethod
* @return
*/
public List<Resource> getResources(HandlerMethod handlerMethod) {
Resources res = handlerMethod.getMethodAnnotation(Resources.class);
if ( s.isNull(res)) {
return Collections.emptyList();
}
RequestMapping requestMappingAnnotation = handlerMethod.getBeanType().getAnnotation(RequestMapping.class);
RequestMapping methodMappingAnnotation = handlerMethod.getMethodAnnotation(RequestMapping.class);
if ( s.isNull(requestMappingAnnotation) && s.isNull(methodMappingAnnotation)) {
return Collections.emptyList();
}
ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
String[] requestMappings = s.nonNull(requestMappingAnnotation) ? requestMappingAnnotation.value() : emptyArray;
String[] methodMappings = s.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.path() : emptyArray;
RequestMethod[] method = s.nonNull(methodMappingAnnotation) ? methodMappingAnnotation.method() : new RequestMethod[0];
requestMappings = ArrayUtils.isEmpty(requestMappings) ? emptyArray : requestMappings;
methodMappings = ArrayUtils.isEmpty(methodMappings) ? emptyArray : methodMappings;
Set<String> mappings = new HashSet<>(1);
for (String reqMapping : requestMappings) {
for (String methodMapping : methodMappings) {
mappings.add(reqMapping + methodMapping);
}
}
List<Resource> resources = new ArrayList<>(1);
for (RequestMethod requestMethod : method) {
for (String mapping : mappings) {
//接口描述
Resource resource = new Resource();
resource.setResourceName( s.nonNull(apiOperation) ? apiOperation.value() : \"未命名资源路径\");
resource.setMapping(mapping);
resource.setMethod(requestMethod.name());
resource.setAuthType(res.auth());
resource.setPerm(resourceService.getResourcePermTag(requestMethod.name(), mapping));
resource.setId(MD5Util.computeMD5(resource.getPerm()));
resources.add(resource);
}
}
return resources;
}
}
继续阅读与本文标签相同的文章
下一篇 :
怎么使用 Origin 把多张图合成一张图?
-
最新115道华为、京东、滴滴、美团精选Java面试题整理
2026-05-18栏目: 教程
-
吴伯凡:谁在重新定义我们的城市
2026-05-18栏目: 教程
-
阿里巴巴集团副总裁郭继军:智慧城市建设如何不变成房地产项目
2026-05-18栏目: 教程
-
阿里云RDS for SQL Server购买使用流程
2026-05-18栏目: 教程
-
阿里云智能战略与合作部刘湘雯:阿里关于创新创业服务的思考
2026-05-18栏目: 教程
