配合自定义注解和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;
    }

}

 

收藏 打印