У меня есть эта конфигурация 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?