Реализация базовой аутентификации в памяти для swagger-ui html - PullRequest
0 голосов
/ 12 марта 2020

Я включил swagger в свой проект весенней загрузки и swagger-ui. html также получает доступ, но я хочу добавить аутентификацию в памяти для доступа к странице swagger-ui. html.

Для которых я добавил следующие конфигурации:

SwaggerConfig
----------------------

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.any()) // Path
                                                                                                    // to
                                                                                                    // the
                                                                                                    // package
                                                                                                    // with
                                                                                                    // controllers
                .paths(PathSelectors.any()).build();
    }
}

SecurityConfig
--------------------

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/tracer/update");
    } 




    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("password")).roles("USER")
                .and().withUser("admin").password(passwordEncoder().encode("admin")).roles("USER", "ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
    .antMatchers("/swagger-resources/*", "*.html", "/api/v1/swagger.json")
        .hasRole("SWAGGER")
    .anyRequest()
        .authenticated();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}

Но вышеприведенное не работает, кажется, потому что при обращении к swagger-ui. html выдает ошибку ниже

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth
...