SpringBoot整合Swagger页面如何禁止访问swagger-ui.html

在Spring Boot中禁止访问Swagger UI页面并在拦截器中进行拦截可以通过配置Spring Security来实现。下面是一个简单的示例,演示如何实现这一点:

在Spring Boot项目中创建一个Spring Security配置类,如下所示:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/swagger-ui.html").denyAll()
            .antMatchers("/swagger-resources/**").permitAll() // 如果需要访问Swagger的其他资源,可以放行
            .and()
            .csrf().disable();
    }
}

在这个配置中,我们使用HttpSecurity对象配置了访问规则。.antMatchers("/swagger-ui.html").denyAll() 表示禁止访问 swagger-ui.html 页面,而 .antMatchers("/swagger-resources/**").permitAll() 则允许访问Swagger的其他资源。

接下来,创建一个拦截器(Interceptor)类,用于拦截对 swagger-ui.html 的访问:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getRequestURI().equals("/swagger-ui.html")) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return false;  // 拦截访问
        }
        return true;  // 放行其他请求
    }
    
    // 可以实现 postHandle 和 afterCompletion 方法进行相应处理
}

配置这个拦截器类并使其生效:

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}

这样配置后,即可通过Spring Security和拦截器实现禁止访问Swagger UI页面 swagger-ui.html

如果你想完全禁用 Swagger UI 和 Swagger 资源,你可以在 Spring Boot 项目的 application.yml 或 application.properties 文件中添加以下配置来实现: 

在 application.yml 文件中的配置:

spring:
  profiles: 
  swagger:
    enabled: false

 在 application.properties 文件中的配置:

spring.profiles.swagger.enabled=false

通过将这些配置设置为 false,你可以完全禁用 Spring Boot 中关于 Swagger UI 和 Swagger 资源的自动配置和展示。这样就可以确保这些端点和页面对外部用户不可见或无法访问。

Spring Boot中,如果你想只禁止对`swagger-ui.html`的访问,可以通过配置Spring Security来实现。以下是一个示例,展示了如何在Spring Boot应用中配置Spring Security来禁止对`swagger-ui.html`的访问: 1. 首先,确保你的Spring Boot项目中已经引入了Spring Security和Swagger的相关依赖。 2. 创建一个配置类,继承自`WebSecurityConfigurerAdapter`,并重写`configure`方法: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // 禁止访问swagger-ui.html .antMatchers("/swagger-ui.html").denyAll() // 其他请求允许访问 .anyRequest().permitAll() .and() // 关闭csrf .csrf().disable(); } } ``` 3. 如果你使用的是Spring Boot 2及以上版本,并且使用的是Spring Security 5及以上版本,可以按照以下方式配置: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() // 禁止访问swagger-ui.html .antMatchers("/swagger-ui.html").denyAll() // 其他请求允许访问 .anyRequest().permitAll() .and() // 关闭csrf .csrf().disable(); } } ``` 这样配置后,访问`/swagger-ui.html`将会被拒绝,而其他请求则可以正常访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值