SpringBoot集成Swagger使用SpringSecurity控制访问权限问题
作者:π大星的日常
1.加入swagger依赖
这是添加Swagger的Maven依赖配置。
在项目的pom.xml文件中添加以上两个依赖可以使用Swagger。
其中springfox-swagger2是Swagger API的核心依赖,springfox-swagger-ui是Swagger的UI依赖。
<dependency><!--添加Swagger依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency><!--添加Swagger-UI依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>2.编写swagger配置类
这是一个Swagger配置类,使用了Spring Boot的@Configuration注解,表示这是一个配置类,使用@EnableSwagger2注解启用Swagger2,然后定义了一个名为customDocket的Bean,返回一个Docket对象,其中设置了apiInfo和select两个属性。
- apiInfo方法返回一个ApiInfo对象,用于设置文档说明和版本说明。
- select方法返回一个ApiSelectorBuilder对象,设置了扫描的包路径。
这里设置了扫描com.hu.oneclick.controller包下的所有API接口。
@Configuration //声明该类为配置类
@EnableSwagger2 //声明启动Swagger2
public class SwaggerConfig{
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hu.oneclick.controller"))//扫描的包路径
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("oneclick")//文档说明
.version("1.0.0")//文档版本说明
.build();
}
}
3.编写SpringSecurity配置类
放开swagger访问资源界面
这段代码是使用Spring Security来配置安全性,允许Swagger访问资源界面而不需要进行认证和授权。
其中使用了 http.authorizeRequests() 来控制访问权限,设置了一些访问地址不需要进行认证,如 /swagger-ui.html, /v2/**, /swagger-resources/** 等等。
同时,也设置了一些静态头信息,如 Access-Control-Allow-Origin, Access-Control-Expose-Headers 等等。
最后,通过 permissiveRequestUrls() 方法设置了无权限接口。
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").anonymous()
.antMatchers("/user/register").anonymous()
.antMatchers("/user/sendEmailCode").anonymous()
.antMatchers("/user/sendEmailRegisterCode").anonymous()
.antMatchers("/swagger-ui.html").anonymous()
.antMatchers("/v2/**").anonymous()
.antMatchers("/swagger-resources/**").anonymous()
.antMatchers("/webjars/springfox-swagger-ui").anonymous()
.antMatchers("/webjars/springfox-swagger-ui/**").anonymous()
.anyRequest().authenticated()
.and()
.csrf().disable()
.formLogin().disable()
.sessionManagement().disable()
.cors()
.and()
.headers().addHeaderWriter(new StaticHeadersWriter(Arrays.asList(
new Header("Access-Control-Allow-Origin", "*"),
new Header("Access-Control-Expose-Headers", "Authorization"))))
.and()
.addFilterAfter(new OptionsRequestFilter(), CorsFilter.class)
.apply(new JsonLoginConfigurer<>()).loginSuccessHandler(jsonLoginSuccessHandler)
.and()
.apply(new JwtLoginConfigurer<>()).tokenValidSuccessHandler(jwtRefreshSuccessHandler)
//设置无权限接口
.permissiveRequestUrls("/login","/user/register","/user/sendEmailCode",
"/user/sendEmailRegisterCode","/swagger-ui.html","/swagger-resources/**",
"/v2/**","/webjars/springfox-swagger-ui/**","/webjars/springfox-swagger-ui")
.and()
.logout()
.logoutUrl("/logout")
.addLogoutHandler(tokenClearLogoutHandler)
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.and()
.sessionManagement().disable();
}4.启动项目访问swagger地址
访问swagger-ui.html可以跳过Spring Security的访问控制,访问Swagger文档资源。
http://localhost:8081/swagger-ui.html
即可跳过springsecurity访问swagger。
总结
本文主要介绍了如何在Spring Boot项目中使用Swagger,并且解决了使用Spring Security时访问Swagger资源被拦截的问题。
首先,我们需要在pom.xml中添加Swagger和Swagger UI的依赖。
然后,在配置类中使用@EnableSwagger2启用Swagger,并通过@Bean注解创建一个Docket对象来配置Swagger,包括文档说明和扫描的包路径等。
在使用Spring Security的项目中,由于默认情况下Spring Security会对所有资源进行保护,因此我们需要通过WebSecurityConfig类的configure方法来放开Swagger访问资源界面。
具体来说,我们需要将Swagger资源添加到Spring Security的白名单中,使其可以被匿名访问。
具体实现方式是通过http.authorizeRequests()方法进行授权配置,并添加antMatchers()方法对Swagger相关资源进行匹配,然后调用anonymous()方法将其添加到白名单中。
最后,我们需要在Spring Security的配置中添加一个JwtLoginConfigurer对象,并设置无权限接口,以确保能够访问Swagger。
通过以上步骤,我们可以成功地在Spring Boot项目中使用Swagger,并解决了使用Spring Security时访问Swagger资源被拦截的问题。
总之:
Swagger是一个非常好用的API文档生成工具,可以方便地展示API文档和测试接口,提高开发效率。
在实际开发中,我们可以根据需要配置Swagger,并通过集成Spring Security来保证接口安全。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- SpringBoot+Spring Security+JWT实现RESTful Api权限控制的方法
- Springboot+Shiro+Jwt实现权限控制的项目实践
- SpringBoot如何整合Springsecurity实现数据库登录及权限控制
- Springboot+Spring Security实现前后端分离登录认证及权限控制的示例代码
- Java SpringBoot 使用拦截器作为权限控制的实现方法
- Springboot和bootstrap实现shiro权限控制配置过程
- Springboot+Vue+shiro实现前后端分离、权限控制的示例代码
- SpringBoot整合Shiro实现权限控制的代码实现
- SpringBoot详解shiro过滤器与权限控制
- 浅谈基于SpringBoot实现一个简单的权限控制注解
- SpringSecurity实现RBAC权限管理
