Конфигурация муравейников - PullRequest
0 голосов
/ 28 мая 2019

У меня есть эта конфигурация Spring и OAuth2:

public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    private static final String RESOURCE_ID = "resource-server-rest-api";
    private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('read')";
    private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('write')";
    private static final String SECURED_PATTERN = "/api/**";
    private static final String PUBLIC_PATTERN = "/api/*/public/**";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                    .anonymous()
                .and()
                    .requestMatchers()
                        .antMatchers(SECURED_PATTERN)
                .and().authorizeRequests()
                        .antMatchers(PUBLIC_PATTERN).permitAll()
                        .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
                .anyRequest().access(SECURED_READ_SCOPE);
    }
}

Теперь я хочу добавить Swagger в свой проект.Я настроил SwaggerController:

@Controller //note - this is a spring-boot controller, not @RestController
public class SwaggerController {

    public static final String SWAGGER_URL = "/api/v1/public/swagger/docs";
    public static final String SWAGGER_HTML = "/swagger-ui.html";

    @RequestMapping(SWAGGER_URL)
    public String home() {
        return "redirect:" + SWAGGER_HTML;
    }
}

Проблема в том, что я не могу указать путь "/swagger-ui.html", чтобы не запускать Spring Login.Я попробовал это (обратите внимание, что я добавил antMatchers для Swagger HTML):

@Override
    public void configure(HttpSecurity http) throws Exception {
        http
                ...
                .and().authorizeRequests()
                        .antMatchers(PUBLIC_PATTERN).permitAll()
                        .antMatchers("/swagger-ui.htm").permitAll()// No authentication

...}

Но это не сработало.Как мне настроить HttpSecurity?

Ответы [ 2 ]

0 голосов
/ 28 мая 2019

Добавить это переопределение:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
            .antMatchers(SWAGGER_URL)
            .antMatchers(SWAGGER_HTML);
}
0 голосов
/ 28 мая 2019

То есть домашняя страница чванства должна перенаправить на /swagger-ui.html?

Если да, у вас есть опечатка при настройке HttpSecurity, которую вы вводите только htm .Поэтому измените на:

 .antMatchers("/swagger-ui.html").permitAll()

С другой стороны, если домашняя страница чванства - /swagger-ui.htm.Затем вы изменяете SWAGGER_HTML в вашем контроллере на:

 public static final String SWAGGER_HTML = "/swagger-ui.htm";
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...