欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swagger2不被SpringSecurity框架攔截的配置及說明

 更新時(shí)間:2023年03月29日 16:58:04   作者:指尖上跳動(dòng)的旋律  
這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Swagger2不被SpringSecurity框架攔截的配置

打算在SpringSecurity框架中集成Swagger2框架進(jìn)行接口功能的運(yùn)行及測試,發(fā)現(xiàn)Swagger2會(huì)被SpringSecurity框架攔截,導(dǎo)致我們在瀏覽器中訪問不了Swagger2首頁。       

解決這個(gè)問題的主要方法只需要在SpringSecurity的配置類中添加一個(gè)方法即可,博主的SpringSecurity的配置類定義為SecurityConfig,添加以下代碼重啟項(xiàng)目再訪問即可;

/*
     * 解決Security訪問Swagger2被攔截的問題;
     * */
    @Override
    public void configure(WebSecurity web) throws Exception {
    	// allow Swagger URL to be accessed without authentication
    			web.ignoring().antMatchers(
    	                "/swagger-ui.html",
    	                "/v2/api-docs", // swagger api json
    	                "/swagger-resources/configuration/ui", // 用來獲取支持的動(dòng)作
    	                "/swagger-resources", // 用來獲取api-docs的URI
    	                "/swagger-resources/configuration/security", // 安全選項(xiàng)
    					"/swagger-resources/**",
    					//補(bǔ)充路徑,近期在搭建swagger接口文檔時(shí),通過瀏覽器控制臺(tái)發(fā)現(xiàn)該/webjars路徑下的文件被攔截,故加上此過濾條件即可。(2020-10-23)
    					"/webjars/**"
    					
    					
    			);
    }

Spring security5 集成swagger2無法訪問

主要還是spring security把 swagger需要訪問的URL被攔截,不只是swagger-ui.html這個(gè)URL

查找網(wǎng)上的解決方案沒一個(gè)好用的,然后自己在跳轉(zhuǎn)重定向的方法里打印了引發(fā)跳轉(zhuǎn)的URL,一個(gè)一個(gè)試出來的老鐵。累啊~

話不多說,放圖,配置security配置類即可

成功:

完整配置類代碼:

package com.lw.bpczy.security.config;
 
 
import com.lw.bpczy.security.authentication.MyAuthenticationFailureHandler;
import com.lw.bpczy.security.authentication.MyAuthenticationSuccessHandler;
import com.lw.bpczy.security.authorization.MyAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsUtils;
 
 
/**
* @author: Liang Shan
* @date: 2019-11-12 10:25
* @description: security安全配置
* WebSecurityConfigurerAdapter提供簡潔的方式來創(chuàng)建webSecurityConfigurer
*/
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
 
    @Autowired
    private MyAuthenticationSuccessHandler successHandler;
    @Autowired
    private MyAuthenticationFailureHandler failureHandler;
    @Autowired
    private MyAccessDeniedHandler accessDeniedHandler;
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /*配置安全項(xiàng)*/
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/needLogin")
                .loginProcessingUrl("/login").permitAll()
                .successHandler(successHandler)
                .failureHandler(failureHandler)
                .and()
                .authorizeRequests()
                // 授權(quán)不需要登錄權(quán)限的URL
                .antMatchers("/needLogin",
                             "/swagger*//**",
                             "/v2/api-docs",
                             "/webjars*//**").permitAll()
                .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
                .anyRequest().access("@rbacService.hasPermission(request,authentication)").
                and().exceptionHandling().accessDeniedHandler(accessDeniedHandler).
                and().cors().and().csrf().disable()
        ;
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論