springboot shiro 自定义配置swagger地址 配合权限

参考了三篇文章
SpringBoot2.0配置swagger2并统一加入认证参数Authorize
https://blog.csdn.net/weixin_30409927/article/details/99826685

SpringMVC + swagger修改swagger-ui.html的访问路径
https://blog.csdn.net/u014207158/article/details/89947199

springfox & swagger-ui 和 springmvc的整合(以及展示路径修改)
https://www.jianshu.com/p/ce7e247515f5?utm_source=oschina-app

思路:
因为在启动项目的时候SWAGGER打包的JAR会在RESOURCE里进行访问
所以先要从MVC配置入手,将访问的地址指向RESOURCE,参考MVC配置
然后将正常访问swagger地址http://localhost:8080/swagger-ui.html 和controller匹配返回相应数据就可以做成无法访问的效果
配合shiro的话就要需要用到权限才能访问到http://localhost:8080/api/swagger/swagger-ui.html,如果直接访问时不可能带token访问到的,所以必须要嵌套在前段网页中进行token访问才能正常打开SWAGGER网页

swagger依赖

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.8.0</version>
		</dependency>

SWAGGER配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).
                useDefaultResponseMessages(false)
                .select()
                //此配置指定扫描哪个位置的CONTROLLER
                .apis(RequestHandlerSelectors.basePackage("com.andy.testdemo.controller"))
                .paths(PathSelectors.regex("^(?!auth).*$"))
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                ;
    }

    private List<ApiKey> securitySchemes() {

        return new ArrayList(
                Collections.singleton(new ApiKey("Authorization", "token", "header")));
    }
    private List<SecurityContext> securityContexts() {
        return new ArrayList(
                Collections.singleton(SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build())
        );
    }
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return new ArrayList(
                Collections.singleton(new SecurityReference("Authorization", authorizationScopes)));
    }	

}

MVC配置

@Configuration
public class CorsConfig implements WebMvcConfigurer {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// api/swagger/为新的访问地址, 就是在MVChandler里添加映射关系,访问这个地址会找到META-INF/RESOURCES下的静态资源
		registry.addResourceHandler("/api/swagger/**").addResourceLocations("classpath:/META-INF/resources/");
	}
	//配置跨域访问
	@Override
	public void addCorsMappings(CorsRegistry registry) {
				        registry.addMapping("/**")	// 允许跨域访问的路径
				        .allowedOrigins("*")	// 允许跨域访问的源
				        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")	// 允许请求方法
				        .maxAge(168000)	// 预检间隔时间
				        .allowedHeaders("*")  // 允许头部设置
				        .allowCredentials(true);	// 是否发送cookie
	}
}

controller

@Controller
public class SwaggerController {
 //因为SWAGGER需要JS和静态文件进行打开所以也必须要做相对应的指向
    @GetMapping("/api/swagger/swagger-resources")
    public String resource() {
        return "forward:/swagger-resources";
    }
 
    @GetMapping("/api/swagger/swagger-resources/configuration/ui")
    public String ui() {
        return "forward:/swagger-resources/configuration/ui";
    }
 
    @GetMapping("/api/swagger/v2/api-docs")
    public String doc() {
        return "forward:/v2/api-docs";
    }
    @GetMapping("/api/swagger/swagger-resources/configuration/security")
    public String security() {
        return "forward:/swagger-resources/configuration/security";
    }
    //屏蔽原有的SWAGGER-UI.HTML地址,此只做简单处理,可以利用JSON返回和谐错误提示。
    @ResponseBody
    @GetMapping("swagger-ui.html")
    public String swagger() {
    	return "Wrong Page Please try later!";
    }
    
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Spring Boot中配置Shiro网关,可以使用Shiro的Filter来实现。具体步骤如下: 1. 在pom.xml文件中添加Shiro和Spring Boot Starter Web依赖。 2. 创建一个ShiroFilter类,继承自org.apache.shiro.web.servlet.AbstractShiroFilter,并实现其抽象方法。 3. 在Spring Boot的配置类中,创建一个ShiroFilterFactoryBean对象,并设置其属性。 4. 在配置类中,创建一个DelegatingFilterProxyRegistrationBean对象,并设置其属性。 5. 在配置类中,创建一个DefaultWebSecurityManager对象,并设置其Realm属性。 6. 在配置类中,创建一个ShiroRealm对象,并实现其认证和授权方法。 7. 在配置类中,创建一个ShiroDialect对象,并注册到Thymeleaf模板引擎中。 8. 在配置类中,创建一个ShiroAnnotationProcessor对象,并注册到Spring容器中。 9. 在配置类中,创建一个ShiroFilterChainDefinition对象,并实现其抽象方法。 10. 在配置类中,创建一个ShiroSessionManager对象,并设置其属性。 11. 在配置类中,创建一个ShiroSessionListener对象,并注册到ShiroSessionManager中。 12. 在配置类中,创建一个ShiroCacheManager对象,并设置其属性。 13. 在配置类中,创建一个ShiroCookie对象,并设置其属性。 14. 在配置类中,创建一个ShiroRememberMeManager对象,并设置其属性。 15. 在配置类中,创建一个ShiroSubjectFactory对象,并设置其属性。 16. 在配置类中,创建一个ShiroFilterFactoryBeanConfigurer对象,并注册到Spring容器中。 17. 在配置类中,创建一个ShiroFilterChainDefinitionConfigurer对象,并注册到Spring容器中。 18. 在配置类中,创建一个ShiroWebSecurityConfigurerAdapter对象,并设置其属性。 19. 在配置类中,创建一个ShiroWebMvcConfigurer对象,并注册到Spring容器中。 20. 在配置类中,创建一个ShiroWebMvcConfigurerAdapter对象,并设置其属性。 21. 在配置类中,创建一个ShiroWebMvcConfigurationSupport对象,并设置其属性。 22. 在配置类中,创建一个ShiroWebMvcConfigurationAdapter对象,并设置其属性。 23. 在配置类中,创建一个ShiroWebMvcAutoConfiguration对象,并设置其属性。 24. 在配置类中,创建一个ShiroWebMvcProperties对象,并设置其属性。 25. 在配置类中,创建一个ShiroWebMvcPropertiesCustomizer对象,并注册到Spring容器中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值