Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 允许少数不进行身份验证的请求,并对剩余的所有请求进行身份验证_Java_Spring_Authentication_Spring Security_Spring Security Oauth2 - Fatal编程技术网

Java 允许少数不进行身份验证的请求,并对剩余的所有请求进行身份验证

Java 允许少数不进行身份验证的请求,并对剩余的所有请求进行身份验证,java,spring,authentication,spring-security,spring-security-oauth2,Java,Spring,Authentication,Spring Security,Spring Security Oauth2,我已使用以下http配置配置了我的资源服务器。基本上,我想设置一些配置,比如允许一些没有身份验证的请求,并且只有当用户拥有有效的访问令牌时,才允许所有其他我没有指定的请求 注意,我使用的是SpringSecurityOAuth2和SpringBoot @Override public void configure(HttpSecurity http) throws Exception { http .exceptionHandling()

我已使用以下http配置配置了我的资源服务器。基本上,我想设置一些配置,比如允许一些没有身份验证的请求,并且只有当用户拥有有效的访问令牌时,才允许所有其他我没有指定的请求

注意,我使用的是SpringSecurityOAuth2和SpringBoot

@Override
    public void configure(HttpSecurity http) throws Exception {

        http
        .exceptionHandling()
        .and()
        .csrf()
        .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
        .disable()
        .headers()
        .frameOptions().disable().disable()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and()
          .antMatcher("**")
          .authorizeRequests()
            .antMatchers("/hello/")
            .permitAll()
          .anyRequest()
            .authenticated();
    }
当我尝试访问
http://localhost:8889/hello
,即使我已将其配置为permitAll,它仍会给出401以下未经授权的响应

{
    "error": "unauthorized",
    "error_description": "An Authentication object was not found in the SecurityContext"
}
相反,我检查了以下配置

        .authorizeRequests()
        .antMatchers("/hello/").permitAll()
        .antMatchers("/hello2/").permitAll()
        .antMatchers("/secure/**").authenticated()
        .antMatchers("/secure2/**").authenticated();
它工作得很好,就像hello和hello2在没有令牌的情况下被允许一样,如果令牌未传递或传递了无效令牌,则secure和secure2会给出错误响应


有人能告诉我我遗漏了什么吗?

问题是,当您不这样访问antmatcher时,它以斜线结尾。我厌倦了“”,但面临相同的错误。我还尝试更改为.antMatchers(“/hello”)并访问“”,但再次出现相同的错误。