gpt4 book ai didi

spring-boot - Swagger ui 未加载 Oauth2

转载 作者:行者123 更新时间:2023-12-04 17:59:38 28 4
gpt4 key购买 nike

我用 spring boot 和 oauth2 开发了一个演示应用程序。我有如下三个应用程序。

  1. 资源 api:这将包含我们需要保护的所有方法。
  2. 身份验证服务器:这是一个提供 token 的 oauth2 服务器
  3. UI:通过Authentication server登录成功后即可访问资源。

我几乎没有可以公开访问的资源( Controller )。但是当我尝试访问 swagger ui 时,它会要求我进行完整的身份验证。当我添加下面的代码时,swagger 页面即将出现,但不仅是整个页面中的一个下拉菜单,而且它也被破坏了。

http
.anonymous() //allow anonymous access
.and()
.authorizeRequests()
.antMatchers("/shouldbepublic/**", "/swagger-ui**/**").permitAll().and() //this should be public resources
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();

最佳答案

这是一个适合我的配置:

.antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll()'.antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll()

否则无法显示 swagger-ui.html

完整的ResourceServiceConfiguration可以在这里找到:

@Configuration
@EnableResourceServer
public static class ResourceServiceConfiguration extends ResourceServerConfigurerAdapter {

@Override
public void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http.csrf().disable().authorizeRequests()
// This is needed to enable swagger-ui interface.
.antMatchers("/swagger-ui.html","/swagger-resources/**", "/v2/api-docs/**").permitAll()
.antMatchers("/**").hasAuthority("ADMIN");
// @formatter:on
}
}

关于spring-boot - Swagger ui 未加载 Oauth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37148544/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com