首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何配置GraphQL接口的spring-security?

如何配置GraphQL接口的spring-security?
EN

Stack Overflow用户
提问于 2021-05-04 02:17:12
回答 1查看 54关注 0票数 0

我正在寻找一个具有以下属性的spring安全配置WebSecurityConfigurerAdapter#configure()

代码语言:javascript
运行
复制
  GET  /api/v3/         <= REST endpoints only authenticated (k
  POST /api/v4/graphql  <= My new GraphQL endpoint, allow anonymous but only for POST
  POST /api/v4/subscriptions    <= GraphQL subscriptions, allow anonymous
  GET  /playground      <= GraphQL playground web app
  GET  /vendor          <= JS and CSS resources for GraphQL playground 

“旧的”/api/v3在它自己的WebSecurityConfigurerAdapter中配置:

代码语言:javascript
运行
复制
    @Configuration
    public class OldApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        String basePath = "/api/v3"  // old REST api

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            log.info("Configuring HttpWebSecurity for "+ basePath);
            http
                .authorizeRequests()
                    .antMatchers(basePath+"/_ping").permitAll()   // allow is alive ping
                    .anyRequest().authenticated()                                   // everything else must be authenticated
                .and()
                    .csrf().disable()  
                    .formLogin().disable() 
                    .httpBasic().disable()
                    .logout().disable();

        }
    }

到目前为止,这一切运行得很好。不,我尝试添加第二个配置器:

代码语言:javascript
运行
复制
    @Configuration
    @Order(30)   // MUST be smaller than 100 to be first!
    public class GraphQlSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        String basePath = "/api/v4";   // new GraphQL API


        protected void configure(HttpSecurity http) throws Exception {
            AndRequestMatcher allowedGraphQlRequests = new AndRequestMatcher(
                new RegexRequestMatcher(basePath + "/graphql", HttpMethod.POST.name()),
                new RegexRequestMatcher(basePath + "/subscriptions", HttpMethod.GET.name()),
                new RegexRequestMatcher(basePath + "/playground", HttpMethod.GET.name()),
                new RegexRequestMatcher(basePath + "/vendor", HttpMethod.GET.name())
            );

            http.requestMatcher(allowedGraphQlRequests)  // MUST limit this  to only these URLs !
                .authorizeRequests()
                    .antMatchers(basePath + "/graphql").permitAll()
                    .antMatchers(basePath + "/subscriptions").permitAll()
                    .antMatchers(basePath + "/playground").permitAll()
                    .antMatchers(basePath + "/vendor/**").permitAll().and().csrf().disable();

        }

    }

但现在只允许经过身份验证的请求。到目前为止,我的调试表明,RegexRequestMatchers被正确地评估并与给定的路径相匹配。

我需要为我的GraphQL post请求禁用CSRF()。如上所示,这里是做这件事的好地方吗?

或者需要这样做吗?

代码语言:javascript
运行
复制
http.requestMatcher(allowedGraphQlRequests)  // MUST limit this  to only these URLs !
    .authorizeRequests()
        .antMatchers(basePath + LiquidoUrlPaths.GRAPHQL).permitAll().and().csrf().disable()
    .authorizeRequests()
        .antMatchers(basePath + LiquidoUrlPaths.SUBSCRIPTIONS).permitAll()
        .antMatchers(basePath + LiquidoUrlPaths.PLAYGROUND).permitAll()
        .antMatchers(basePath + LiquidoUrlPaths.VENDOR + "/**").permitAll();

我很困惑?:-(我必须如何配置我的GraphQL,以便在没有graphql的情况下允许匿名POST请求/ API /v4/graphql?

EN

回答 1

Stack Overflow用户

发布于 2021-05-04 04:07:15

你可以试着调试它24小时

您几乎可以重新实现Spring-Security,因为您看不到另一种方法。

..。或者你可以用OrRequestMatcher替换AndRequestMatcher,然后回到你的逻辑课,第一课:-)……把头撞到墙上!GRRRRR

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67373905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档