【问题标题】:Spring security OAuth2: permitAll() doesn't work for me (HTTP 401 for unprotected endpoins)Spring security OAuth2: permitAll() 对我不起作用(HTTP 401 for unprotected endpoints)
【发布时间】:2019-12-12 20:28:12
【问题描述】:

我必须在一堆未回答的帖子中添加另一个帖子,表明同样的问题。我正在使用 Spring Boot 2.1.6.RELEASE 和 spring-security-oauth2 2.1.5.RELEASE。我有以下资源配置:

@Configuration
public class RdfResourceServerConfig extends ResourceServerConfigurerAdapter
{
  @Override
  public void configure(HttpSecurity httpSecurity) throws Exception
  {
    httpSecurity
      .csrf().disable()
      .authorizeRequests().antMatchers("/, /**/test/**").permitAll()
      .and().authorizeRequests().anyRequest().authenticated();
  }
}

基于此配置,我希望http://localhost:8080/.../test/ 的 GET 是允许的,但它返回 HTTP 401:

{
  "error": "unauthorized",
  "error_description": "Full authentication is required to access this resource"
}

当然,我的 Application 类声明了所有必需的注解:

@SpringBootApplication
@EnableResourceServer
public class ReferenceDataFactoryApplication extends SpringBootServletInitializer
{
  public static void main(String[] args)
  {
    SpringApplication.run(ReferenceDataFactoryApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
  {
    return builder.sources(ReferenceDataFactoryApplication.class);
  }
  ...
}

非常感谢您让我知道我做错了什么。

亲切的问候,

尼古拉斯

【问题讨论】:

  • 您的网址可能与/, /**/test/** 模式不匹配。你最好分成两个不同的模式
  • 就是这样,非常感谢。但是我见过类似的东西: .antMatchers("/", "/favicon.ico", "/**/*.png", "/**/*.gif", "/**/*.svg ", "/**/*.jpg", "/**/*.html", "/**/*.css", "/**/*.js") .permitAll() 不知道怎么做投票反对这个解决方案...

标签: java spring-boot spring-security spring-security-oauth2


【解决方案1】:

如 cmets 中所述,”/, /**/test/**” 不是有效模式。 AntMatcher 每次方法调用采用 1 个字符串模式。

.antMatcher(”/”)
.antMatcher(”/**/test/**”)

【讨论】:

  • 是的,它做到了,谢谢。我投票支持你的答案,但我认为因为尼古拉舍甫琴科只发表了评论。不知道如何处理。
  • 没关系,如果他写了答案就给他吧:) 很高兴它成功了
猜你喜欢
  • 2016-02-16
  • 2018-01-31
  • 2017-08-20
  • 2014-08-31
  • 2019-03-25
  • 2020-07-24
  • 2021-05-25
  • 2019-02-21
  • 2019-09-27
相关资源
最近更新 更多