我正在寻找一个具有以下属性的spring安全配置WebSecurityConfigurerAdapter#configure()
:
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
中配置:
@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();
}
}
到目前为止,这一切运行得很好。不,我尝试添加第二个配置器:
@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()。如上所示,这里是做这件事的好地方吗?
或者需要这样做吗?
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?
发布于 2021-05-04 04:07:15
你可以试着调试它24小时
您几乎可以重新实现Spring-Security,因为您看不到另一种方法。
..。或者你可以用OrRequestMatcher
替换AndRequestMatcher
,然后回到你的逻辑课,第一课:-)……把头撞到墙上!GRRRRR
https://stackoverflow.com/questions/67373905
复制相似问题