当前位置: 首页 > 知识库问答 >
问题:

keycloak CORS过滤Spring靴

巢宏富
2023-03-14

我正在使用keycloak来保护我的rest服务。我引用的是这里给出的教程。我创建了其余部分和前端。现在,当我在后端添加keycloak时,当我的前端调用api时,我会得到CORS错误。

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

    @Bean
    public WebMvcConfigurer corsConfiguration() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/*")
                        .allowedMethods(HttpMethod.GET.toString(), HttpMethod.POST.toString(),
                                HttpMethod.PUT.toString(), HttpMethod.DELETE.toString(), HttpMethod.OPTIONS.toString())
                        .allowedOrigins("*");
            }
        };
    }
} 
keycloak.realm = demo
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = tutorial-backend
keycloak.bearer-only = true
keycloak.credentials.secret = 123123-1231231-123123-1231
keycloak.cors = true
keycloak.securityConstraints[0].securityCollections[0].name = spring secured api
keycloak.securityConstraints[0].securityCollections[0].authRoles[0] = admin
keycloak.securityConstraints[0].securityCollections[0].authRoles[1] = user
keycloak.securityConstraints[0].securityCollections[0].patterns[0] = /api/*

我调用的示例REST API

@RestController
public class SampleController {    
    @RequestMapping(value ="/api/getSample",method=RequestMethod.GET)
    public string home() {
        return new string("demo");
    }        
}

前端keycloak.json属性包括

{
  "realm": "demo",
  "auth-server-url": "http://localhost:8080/auth",
  "ssl-required": "external",
  "resource": "tutorial-frontend",
  "public-client": true
}

我得到的CORS错误

XMLHttpRequest cannot load http://localhost:8090/api/getSample. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 401.

共有1个答案

赵辉
2023-03-14

尝试像我的示例一样创建CORS bean。我最近经历了同样的事情(让CORS工作),这是一场噩梦,因为SpringBoot CORS支持目前不像MVC CORS那样健壮或简单。

@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);

    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}

这就是我如何将其设置为在应用程序范围内接受任何起源,但是如果您更改了一些参数,您应该能够复制您想要的内容。也就是说,如果您只想添加您提到的方法,请将一些addAllowedMethod()链接起来。允许的起源将是相同的,然后AddMapping(“/api/*”)将变为Source.RegisterCorsConfiguration(“/api/*”,config);

编辑:

Spring数据Rest和Cors

看看这个。Sebastian是Spring工程团队的成员,所以这是你能得到的正式答案。

 类似资料:
  • 当我调用我的服务时,我的这个URL: 我有这个堆栈跟踪: 什么是平均健康有一个空的筛选器列表?

  • 问题内容: 我们添加到我们现有的项目中。 从这一刻起,我们从服务器收到401 错误。 这是因为没有标头附加到响应。为了解决这个问题,我们Filter在退出过滤器之前的链中添加了我们自己的过滤器,但是该过滤器不适用于我们的请求。 我们的错误: XMLHttpRequest无法加载。所请求的资源上不存在“ Access-Control-Allow-Origin”标头。http://localhost:

  • 我正在使用GraphQL Spring Boot库构建GraphQL APIhttps://github.com/graphql-java/graphql-spring-boot 我有一个模式 我的查询是在Spring Boot项目的查询类中实现的 我的问题是: 返回列表时如何使用过滤器和排序: 我想这是必须在Java方法中实现的,但我不确定如何在方法中注入过滤器等。

  • ExtendedService内部: 但是当我开始时,我得到无效的Bean定义,因为myService是绑定错误。如何排除原来的MyService? 例外情况: 原因:org.springframework.beans.factory.support.beandefinitionoverrideException:URL[file:svc.xml]中定义的名为“My Service”的bean定义

  • 这是因为响应中没有附加头。为了解决这个问题,我们添加了自己的筛选器,它位于注销筛选器之前的链中,但该筛选器不适用于我们的请求。 我们的错误: XMLHttpRequest无法加载。请求的资源上没有“access-control-allow-origin”标头。因此,不允许访问Origin。响应的HTTP状态代码为401。 我们的过滤器 我们的申请 我们的筛选器是从Spring-Boot注册的: 尝

  • 本文向大家介绍Spring MVC过滤器-登录过滤的代码实现,包括了Spring MVC过滤器-登录过滤的代码实现的使用技巧和注意事项,需要的朋友参考一下 一个非常简单的登录权限拦截器,具体代码如下: 以下代码是继承OncePerRequestFilter实现登录过滤的代码: 写完过滤器后,需要在web.xml中进行配置: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教