springboot 基于shiro 配置 springfox-swagger2

依赖项:

implementation('io.springfox:springfox-swagger2:2.9.2')
implementation('io.springfox:springfox-swagger-ui:2.9.2')

基本配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@ComponentScan(basePackageClasses = Swagger2Configuration.class)
public class Swagger2Configuration {

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                                .paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("title")// 页面标题
				.version("1.0")// 版本号
				.description("description")// 描述
				.build();
	}

}

上面配置两句关键代码:

@EnableSwagger2

开启swagger2

@ComponentScan(basePackageClasses = Swagger2Configuration.class)

这一句如果确保配置文件类属于spring能扫描到的包位置可以不加。

配置静态文件的资源路径:


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// swagger ui 主页面位置(springfox-swagger-ui-version.jar中的META-INF目录)
		registry.addResourceHandler("/swagger-ui.html")
			.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
		// swagger ui 静态资源位置(springfox-swagger-ui-version.jar中的META-INF目录)
		registry.addResourceHandler("/webjars/springfox-swagger-ui/**")
			.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
	}

}

shiro 配置:


import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomShiroConfiguration implements InitializingBean {

	@Autowired
	private ShiroFilterFactoryBean shiroFilterFactory;

	@Override
	public void afterPropertiesSet() throws Exception {
		AbstractShiroFilter shiroFilter = (AbstractShiroFilter) shiroFilterFactory.getObject();
		PathMatchingFilterChainResolver chainResolver = (PathMatchingFilterChainResolver) shiroFilter
				.getFilterChainResolver();
		DefaultFilterChainManager chainManager = (DefaultFilterChainManager) chainResolver
                                .getFilterChainManager();

		chainManager.getFilterChains().clear();
		// 一定要注意顺序问题,无需权限的在前,需要权限的在后
		Map def = new LinkedHashMap<>();
		// swagger2 api 接口地址
		def.put("/v2/api-docs", "anon");
		// swagger ui 主页面
		def.put("/swagger-ui.html", "anon");
                // swagger 资源基本路径
		def.put("/swagger-resources", "anon");
		// swagger ui 安全配置请求url前缀
		def.put("/swagger-resources/configuration/security/**", "anon");
		// swagger ui 选项配置请求url前缀
		def.put("/swagger-resources/configuration/ui/**", "anon");
		// swagger ui 静态页面资源路径
		def.put("/webjars/springfox-swagger-ui/**", "anon");

		def.putAll(shiroFilterFactory.getFilterChainDefinitionMap());
		shiroFilterFactory.setFilterChainDefinitionMap(def);

		for (Map.Entry entry : def.entrySet()) {
			String url = entry.getKey();
			String chainDefinition = entry.getValue();
			chainManager.createChain(url, chainDefinition);
		}
	}

}

因为这部分配置是在子系统进行扩充,所以没有贴出主的shiro配置了。本身其他的配置对swagger配置也没有影响。

你可能感兴趣的:(原创)