java - spring 中对/oauth/token 的选项请求未授权 401

标签 java angularjs spring spring-mvc oauth

我正在使用 Spring Boot 来制作 REST API 并使用 Spring Security oauth2 进行登录身份验证。当我从与 Spring Boot 集成的 Angular 客户端向 localhost:8080/oauth/token 发送请求时,它工作正常。(运行在同一个本地主机:8080)。 但是如果我想从在 localhost(xampp) 上运行的客户端登录,那么它会给出错误

enter image description here

更新:我还在 application.java 文件中添加了此代码

@SpringBootApplication
public class Application {
private static final String[] REQUEST_METHOD_SUPPORTED = { "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD" };

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init() {
    return (args) -> {
        FileSystemUtils.deleteRecursively(new File(FileUploadController.ROOT));

        Files.createDirectory(Paths.get(FileUploadController.ROOT));
    };
}
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
            .allowedOrigins("localhost:80")
            .allowedMethods("POST", "GET", "PUT", "DELETE")
            .allowedHeaders("header1", "header2", "header3")
            .exposedHeaders("header1", "header2");

        }
    };
 }


}

OAuth2ServerConfiguration.java:

@Configuration
@EnableWebSecurity
public class OAuth2ServerConfiguration {

private static final String RESOURCE_ID = "restservice";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
        ResourceServerConfigurerAdapter {
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        // @formatter:off
        resources
            .resourceId(RESOURCE_ID);
        // @formatter:on
    }
    @Order(-1)
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http

            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS).permitAll()

        // @formatter:on
    }

}

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends
        AuthorizationServerConfigurerAdapter {

    private TokenStore tokenStore = new InMemoryTokenStore();

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        // @formatter:off
        endpoints
            .tokenStore(this.tokenStore)
            .authenticationManager(this.authenticationManager)
            .userDetailsService(userDetailsService);                            

        // @formatter:on
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        // @formatter:off
        clients
            .inMemory()
                .withClient("clientapp")
                    .authorizedGrantTypes("password", "refresh_token")
                    .authorities("USER")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .secret("LMS");
        // @formatter:on
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setSupportRefreshToken(true);
        tokenServices.setTokenStore(this.tokenStore);
        return tokenServices;
    }

}

}

我已经检查并尝试了很多解决方案,但无法弄清楚。请提供帮助!!!!

最佳答案

选项请求会自动发送以确保服务器处于 Activity 状态,因此如果您使用“WebSecurityConfgurerAdapter”,您需要在安全配置中允许该选项方法

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {

         httpSecurity.// your autherization configuration
          .antMatchers(HttpMethod.OPTIONS).permitAll()


 } 

关于java - spring 中对/oauth/token 的选项请求未授权 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115004/

相关文章:

java - 如何上传其中包含图像文件的表单,AngularJS spring

java - 如果 IOS 按钮出现在屏幕上,如何使用 if/else 来单击它

java - reflect.Field.annotations 始终为空

java - 为什么 Java Processbuilder 运行命令比 Python Subprocess.check_output 慢 4000 倍

javascript - 我怎样才能让 View 在返回时保留其嵌套的子状态,而不是使用 ui-router 转到其父级?

java - 每次运行或调试 Web 应用程序后显示的 IntelliJ 更新框架

java - 从子对象映射 json 字段

java - Wicket 中有静态树组件吗?

angularjs - 使Elasticsearch 2上的CORS可以被AngularJS应用访问

javascript - 重新编译 Angular 指令以编程方式添加 ng-module 和 ng-change