我试图将 Spring 安全配置为仅阻止对 swagger 的请求,但是它阻止了所有 url。有谁知道如何只锁定 swagger 的 url 而其余的都不安全?
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests().anyRequest().permitAll()
.and()
.authorizeRequests()
.antMatchers("/swagger*/**").authenticated();
http.httpBasic();
}
Run Code Online (Sandbox Code Playgroud) spring spring-mvc documentation-generation swagger-ui springfox
我正在尝试使用 Springfox 并遵循https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api文档从 springboot 项目生成 Swagger 文档。
最初,我收到错误“访问资源需要完全授权”,因为我在应用程序中使用 OAuth2。我更改了配置以允许所有以 /swagger-ui.html 结尾的请求。
现在,我在尝试访问本地的 /swagger-ui.html 时收到“WhiteLabel 错误页面 - 此应用程序没有 /error 的显式映射”。
我浏览了各种帖子,但没有一个解决方案对我有用 - 我没有使用可能干扰的@webmvcconfiguration。有人可以帮忙吗?
我有以下 swagger 的弹簧引导配置,当服务启动时,我收到以下错误,我不确定为什么会这样。我遵循了一个教程,它对他们有用。
java.lang.IllegalStateException: Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered. default
@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("test.rest"))
.paths(PathSelectors.ant("/test/**"))
.build()
.apiInfo(apiInfo());
}
// Describe the apis
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("test")
.description("Test")
.version("1.0.0")
.license("vvv")
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
我还有另一个配置
@OurApp
@EnableSwagger2
public class CoreApp extends OurApp {
}
Run Code Online (Sandbox Code Playgroud) 如何获取过滤器以应用于根路径之外的每个请求(我想忽略的请求除外)?这是我的例子:
我有一个 Spring Security 过滤器,如下所示:
private static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.antMatcher("/**")
.addFilterBefore(new AuthenticationFilter(), basicAuthenticationFilter.class);
}
@Override
public void configure(WebSecurity web) {
web
.ignoring()
.requestMatchers(SecurityServletRequestMatchers.servletIgnoreAuthMatcher());
}
}
Run Code Online (Sandbox Code Playgroud)
该过滤器填充一个CustomApiToken包含所有标头信息的对象,并将其放入 Spring Security 上下文中SecurityContextHolder.getContext().setAuthentication(token),以便轻松访问请求控制器上的令牌。
我正在尝试将 Springfox 添加到项目中,这意味着我想禁用 UI 和 API 文档页面的过滤器。
我最初的尝试是在该方法中添加一个子句:
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.antMatcher("/**")
.addFilterBefore(new AuthenticationFilter(), BasicAuthenticationFilter.class);
http
.requestMatcher(SecurityServletRequestMatchers.servletIgnoreAuthMatcher())
.headers() //.servletIgnoreAuthMatchers has all the swagger urls also
.defaultsDisabled()
.disable() …Run Code Online (Sandbox Code Playgroud) 调查Springfox和Swagger UI,但我遇到了一个问题.我使用Spring Boot REST示例项目作为我的PoC的基础.我正在运行JDK 8,该项目利用了Gradle.
首先,这是项目的文件内容:
的build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
jcenter()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("io.springfox:springfox-swagger2:2.2.2")
compile("io.springfox:springfox-swagger-ui:2.2.2")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Run Code Online (Sandbox Code Playgroud)
GreetingController.java
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController …Run Code Online (Sandbox Code Playgroud)