我已将 swagger 与 api 文档的 SAP Hybris commercewebservices api 扩展集成。
这是 spring-v2-spring.xml 中的条目:
<security:http pattern="/v2//api-docs" security="none"/>
<security:http pattern="/v2/*swagger*/**" security="none"/>
这是 springmvc-v2-servlet.xml 中的条目:
<mvc:resources mapping="**/swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
如何向 swagger UI 添加基本身份验证:/rest/v2/swagger-ui.html?提前致谢。
最佳答案
我已经在 kotlin 中进行了如下配置。
@Configuration
@EnableSwagger2
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
http.csrf().disable()
.antMatcher("/swagger-ui.html")
.authorizeRequests()
.anyRequest().authenticated()
.and().httpBasic()
}
@Autowired
override public fun configure(auth: AuthenticationManagerBuilder) {
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder().encode("pass123"))
.authorities("USER")
}
@Bean
public fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder()
}
}
关于java - 向 Swagger UI 添加基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62251865/