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/** 等等。
同時,也設置了一些靜態(tài)頭信息,如 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注解創(chuàng)建一個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文檔和測試接口,提高開發(fā)效率。
在實際開發(fā)中,我們可以根據需要配置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權限管理
相關文章
解決feignclient調用服務,傳遞的中文數據成???問題
這篇文章主要介紹了解決feignclient調用服務,傳遞的中文數據成???問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

