【问题标题】:Swagger and springfox招摇和springfox
【发布时间】:2017-04-14 07:36:24
【问题描述】:

我正在使用 swagger 版本 2.2.2。如果我去地址,http://localhost:8080/swagger-ui.html ,我会直接拿到swagger UI。有什么办法可以加一个安全层,比如在swagger UI显示之前提示用户输入用户名和密码?

【问题讨论】:

  • 您可以使用 Spring 基本安全性,但这只会在发送 HTTP 调用后询问凭据,因此您的 Swagger 仍然对所有人可见,您也可以映射 swagger URI,但这会给您很难,因为 swagger 还需要其他一些文件来渲染。第二种选择是安装 Apache,它会在有人试图访问您 Swagger Apache Auth on specific URL 时触发。我自己从未做过选项 2,因此我不会将此作为答案发布
  • 你想保护生产环境的 url,我是对的吗?如果你使用 nginx,你可以设置基本的 http 身份验证。 digitalocean.com/community/tutorials/…
  • 你看过 jhipster 是怎么做的吗?它应该给你一个想法。

标签: spring-boot swagger swagger-ui swagger-2.0


【解决方案1】:

有可能,我是用这个安全配置做到的(假设你使用的是spring):

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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable();
        final InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        final PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        manager.createUser(User.withUsername("user").password(encoder.encode("password")).roles("ACTUATOR", "ADMIN").build());
        http.authorizeRequests().antMatchers("/swagger-ui**", "/v2/api-docs/**").hasRole("ACTUATOR").and().httpBasic()
                .and().userDetailsService(manager);
    }

}

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2019-01-12
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2018-07-20
    相关资源
    最近更新 更多