能够打开swagger ui页面
提前致谢 。
下面是 security 配置类和 swagger 配置类。很长一段时间以来我一直在尝试整合 swagger,但失败了。
当我尝试打开 http://localhost:8080/swagger-ui.html 时,它给了我 Whitelabel 错误页面。
我的 pom.xml 文件包含以下依赖项:-
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthorizationFilter authorizationFilter;
private final UserDetailsService userDetailsService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
private static final String[] AUTH_LIST = {
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
};
@Override
protected void configure(HttpSecurity http) throws Exception { //todo-ant matcher changes
http.cors().and().csrf()
.disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(AUTH_LIST).permitAll()
.antMatchers("/api/v1/auth/**",
"/api/v1/user/create",
"/api/v1/vendor-responses/intro",
"/api/v1/timetable/**").permitAll()
.antMatchers("/api/v1/course/**").hasAnyAuthority(TEACHER.name(), SUPERUSER.name())
.antMatchers("/api/v1/**").hasAnyAuthority(SUPERUSER.name())
.antMatchers("/api/v1/**").hasAnyAuthority(TEACHER.name())
.anyRequest()
.authenticated()
.and()
.addFilterBefore(authorizationFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
/**
* Method to set paths to be included through swagger
*
* @return Docket
*/
@Bean
public Docket configApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.apnatuitionmaster.controller"))
.build();
}
/**
* Method to set swagger info
*
* @return ApiInfoBuilder
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("")
.description("")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}```
[enter image description here][1]
[1]: https://i.sstatic.net/MmfM2.png
Thanks in advance .
Below is the security config class and swagger config class. Have been trying to integrate swagger for a ver long time, but i m failing.
When I am trying to open http://localhost:8080/swagger-ui.html it gives me Whitelabel Error Page.
my pom.xml file contains the following dependencies :-
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
AuthorizationFilter authorizationFilter;
private final UserDetailsService userDetailsService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
private static final String[] AUTH_LIST = {
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
};
@Override
protected void configure(HttpSecurity http) throws Exception { //todo-ant matcher changes
http.cors().and().csrf()
.disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(AUTH_LIST).permitAll()
.antMatchers("/api/v1/auth/**",
"/api/v1/user/create",
"/api/v1/vendor-responses/intro",
"/api/v1/timetable/**").permitAll()
.antMatchers("/api/v1/course/**").hasAnyAuthority(TEACHER.name(), SUPERUSER.name())
.antMatchers("/api/v1/**").hasAnyAuthority(SUPERUSER.name())
.antMatchers("/api/v1/**").hasAnyAuthority(TEACHER.name())
.anyRequest()
.authenticated()
.and()
.addFilterBefore(authorizationFilter, UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
/**
* Method to set paths to be included through swagger
*
* @return Docket
*/
@Bean
public Docket configApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.apnatuitionmaster.controller"))
.build();
}
/**
* Method to set swagger info
*
* @return ApiInfoBuilder
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("")
.description("")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}```
[enter image description here][1]
[1]: https://i.sstatic.net/MmfM2.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论