使用oauth2提供身份验证时,从不可靠的响应中获取401错误状态

时间:2019-05-14 10:16:19

标签: spring-boot oauth-2.0 swagger-ui

我正在尝试在我的应用程序中使用spring security和oauth2.0进行设置。我已经创建了为此所需的所有文件,我能够获得access_token并在尝试与邮递员时能够使用该令牌进行身份验证。

但是现在我需要使用大摇大摆做同样的事情,并且我的状态为401 和错误:挥舞着“ Auth ErrorTypeError:无法获取”。 同样,当我点击“身份验证”按钮时,我也会获得方法类型选项。

我在我的应用程序中使用了cors过滤器,并删除了HttpMethod.options的安全性。

为什么我会收到此错误?我错过了什么?

授权服务器代码:

tf.Tensor

资源服务器:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().and().cors().and()
                .authorizeRequests()
                .antMatchers("/login","/logout.do").permitAll()
                .antMatchers(HttpMethod.OPTIONS, 
                "/oauth/token").permitAll()
              // .antMatchers("/**").authenticated()
               .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/login.do")
                .usernameParameter("username")
                .passwordParameter("password")
                .loginPage("/login")
                .and()
                .logout()
                .logoutRequestMatcher(new 
            AntPathRequestMatcher("/logout.do"))`enter code here`
           .and()
           .userDetailsService(userDetailsServiceBean());
    }

大型配置:

 @Override

public void configure(HttpSecurity http) throws Exception{

    http
    .csrf().disable().authorizeRequests().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security",
            "/swagger-ui.html", "/webjars/**", "/swagger-resources/configuration/ui", "/swagger-ui.html",
            "/swagger-resources/configuration/security")
    .permitAll();


    http.csrf().and()
            //.addFilterBefore(new CORSFilter(), ChannelProcessingFilter.class)
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
            .antMatchers(HttpMethod.GET, "/**").access("#oauth2.hasScope('read')")
            .antMatchers(HttpMethod.POST, "/**").access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.PATCH, "/**").access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.PUT, "/**").access("#oauth2.hasScope('write')")
            .antMatchers(HttpMethod.DELETE, "/**").access("#oauth2.hasScope('write')");

}

1 个答案:

答案 0 :(得分:0)

是的,我找到了错误的原因。 我在资源端而不是授权服务器上创建了cors过滤器。

这就是为什么我无法在服务器上收到选项请求的原因。

相关问题