У меня есть простое приложение REST со службой аутентификации. Я пытался добавить к нему swagger и swagger-ui, но вижу свои конечные точки только в /v2/api-docs
. В swagger-ui.html
я вижу только группы конечных точек, но не могу расширить список.
В отладке хрома я вижу:
Не удалось загрузить ресурс: сервер ответил со статусом 401 ()
Uncaught TypeError: невозможно прочитать свойство indexOf неопределенного
и на терминале с сервером:
ОШИБКА 10020 --- [nio-5001-exec-3] c.t.r.a.p.JwtAuthenticationEntryPoint: ответ с несанкционированной ошибкой. Сообщение. Для доступа к этому ресурсу требуется полная аутентификация.
Похоже, что в моих конфигурационных файлах чего-то не хватает, я попробовал несколько решений, которые нашел в Интернете, но все равно ничего не работает.
Это мой код:
помпон
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
контроллер
@RestController
@PreAuthorize("hasRole('USER')")
@RequestMapping(path = "restaurant")
@Api(value="restaurant", description="Example operations for restaurants")
public class RestaurantController {
// endpoints
}
чванливая фасоль
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.tablebooker.restaurantservice.restaurant"))
.paths(PathSelectors.any())
.build();
}
}
SecurityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
securedEnabled = true,
jsr250Enabled = true,
prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//other methods
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js")
.permitAll()
.antMatchers("/api/auth/**")
.permitAll()
.antMatchers("/restaurant/**")
.hasRole("USER")
.anyRequest()
.authenticated();
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}
}
Любые идеи, как я могу заставить мою конфигурацию работать?