Requirements:
- Spring Boot application with Springfox
- Add BASIC authentication to Swagger
- Pass on all other requests
Code: implemented
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/swagger-resources/*", "*.html", "/api/v1/swagger.json")
.hasAuthority("SWAGGER")
.anyRequest().permitAll()
.and()
.httpBasic()
.and()
.csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("admin").authorities("SWAGGER");
}
}
This code however does not work - you can freely browse /swagger-ui.html#/ without any authentcation.
Question is - why BASIC auth and user do not apply to swagger ui endpoint?
.permitAll()
instead of.authenticated()
– Jellaba