我试着在“localhost:8080/v2/api”和"localhost:8080/ swagger -ui.html“上看到swagger文档,但仍然失败。该项目已经在“localhost:8080/v2/apis/**”上成功构建和运行。
在本地运行应用程序后,活动配置文件是本地的。
SwaggerConfig
@Profile({"local", "local_kuh", "develop"})
@Configuration
@EnableSwagger2
public class SwaggerConfig {
private ApiInfo apiInfo() {
String description = "[DESCRIPTION]";
return new ApiInfoBuilder()
.title("Rest API Server")
.description(description)
.build();
}
private ApiKey apiKey() {
return new ApiKey("apiKey", "Authorization", "header");
}
@Bean
public SecurityConfiguration security() {
return SecurityConfigurationBuilder.builder().scopeSeparator(",")
.additionalQueryStringParams(null)
.useBasicAuthenticationWithAccessCodeGrant(false).build();
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth())
.forPaths(PathSelectors.any()).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(new SecurityReference("apiKey", authorizationScopes));
}
@Bean
public Docket commonApi() {
ParameterBuilder aParameterBuilder = new ParameterBuilder();
aParameterBuilder.name("Authorization")
.description("Access Tocken")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
return new Docket(DocumentationType.SWAGGER_2)
.groupName("v2")
.apiInfo(this.apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("net.infobank.moyamo.controllers"))
.build()
.securitySchemes(Collections.singletonList(apiKey()))
.securityContexts(Collections.singletonList(securityContext()))
.useDefaultResponseMessages(false);
}
}
...
<!-- springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- springfox-swager-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
...
dependencies {
...
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
...
}
WebSecurityConfig
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final DataSource datasource;
private final RestAuthenticationEntryPoint restAuthenticationEntryPoint;
private final TokenAuthorizationFilter tokenAuthorizationFilter;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(tokenAuthorizationFilter, UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/v2/auth/**").permitAll()
.antMatchers("/v2/notices/**").permitAll()
.antMatchers("/v2/policies/**").permitAll()
.antMatchers(HttpMethod.OPTIONS, "/v1/gambles", "/v1/gambles/**", "/v2/rankings").permitAll()
.antMatchers(HttpMethod.GET, "/v2/rankings").permitAll()
.antMatchers("/postings").permitAll()
.antMatchers("/static/**").permitAll()
.antMatchers("/docs/**").permitAll()
.antMatchers("/actuator/**").permitAll()
.antMatchers("/api/**").permitAll()
.antMatchers("/v2/shops").permitAll()
.antMatchers("/v2/api-docs", "/v2/logs", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**", "/swagger/**").permitAll() //swagger
.antMatchers("/**").authenticated()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint);
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(datasource).passwordEncoder(bCryptPasswordEncoder);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
我以为一切都准备好了。但仍然得到404错误。
我也看不见swagger-ui.html。
我认为swagger应该在本地配置文件上没有身份验证的情况下工作。但无论授权与否,都不可行。
我怎样才能解决这个问题?谢谢大家,提前。
发布于 2022-08-26 10:23:41
实际上现在我正在使用openapi,但是我还记得springfox的配置bean
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket apiDefault() {
return docketGeneric("default", "/api/.*");
}
private Docket docketGeneric(String groupName, String pathsRegex) {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.protocols(Collections.singleton("http"))
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex(pathsRegex))
.build()
.forCodeGeneration(true);
}
https://stackoverflow.com/questions/73496744
复制相似问题