Raf*_*sky 1 java spring spring-security swagger swagger-ui
我有一个带有身份验证服务的简单 REST 应用程序。我试图向它添加 swagger 和 swagger-ui,但我只能在/v2/api-docs. 在swagger-ui.html我只看到端点组,但我无法扩展任何列表。
在 chrome 调试中,我看到:
加载资源失败:服务器响应状态为 401()
未捕获的类型错误:无法读取未定义的属性“indexOf”
并在带有服务器的终端上:
错误 10020 --- [nio-5001-exec-3] ctrapJwtAuthenticationEntryPoint:响应未经授权的错误。消息 - 访问此资源需要完全身份验证
看起来我的配置文件丢失了一些东西,我尝试了一些在网上找到的解决方案,但仍然没有任何效果。
这是我的代码:
绒球
<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>
Run Code Online (Sandbox Code Playgroud)
控制器
@RestController
@PreAuthorize("hasRole('USER')")
@RequestMapping(path = "restaurant")
@Api(value="restaurant", description="Example operations for restaurants")
public class RestaurantController {
// endpoints
}
Run Code Online (Sandbox Code Playgroud)
招摇豆
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
安全配置
@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/**");
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法如何使我的配置工作?
首先您应该注册 swagger 的资源。
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
}
}
Run Code Online (Sandbox Code Playgroud)
那么因为你正在使用Spring Security,也许你应该关闭权限。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
// ignore swagger
web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs");
}
Run Code Online (Sandbox Code Playgroud)
也许你最好使用2.8.0以下版本的swagger,否则你可能不得不面对很多bug。
对我来说,在传统的 Weblogic 部署中没有任何问题,没有任何提及@Override
public void configure(WebSecurity web) throws Exception......只要@Override
protected void configure(HttpSecurity http) throws Exception足够,UI 在 swagger 上可见。
但是相同的代码在 Apache Tomcat 服务器上不起作用,因此需要以下代码,
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers(HttpMethod.OPTIONS, "/**");
// ignore swagger
web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**", "/swagger-resources/**", "/v2/api-docs","/webjars/**");
}
/webjars/** AokoQin在回答中失踪。
在这里回答是因为我没有在没有上述代码的情况下在 Weblogic 上遇到任何问题,而只有 Tomcat。我已经通过ResourceHandlerRegistrymvc 配置添加了资源。
| 归档时间: |
|
| 查看次数: |
12093 次 |
| 最近记录: |