我正在使用graphql进行spring引导项目。我正在使用graphql-java-tools和graphql-spring-boot-starter。我设法用spring安全性来配置安全性和会话管理,就像你在下面的java配置文件中看到的一样。在使用graphql的spring引导中进行身份验证
现在“/ graphql”路径是安全的(它只能在请求的http头中发送“基本http认证”或会话令牌(x-auth-token))。在任何graphql操作上使用“基本http认证”进行认证将开始一个新的会话,并将新的会话令牌发送回头,并且该令牌可用于继续该会话。
如何让匿名用户访问一些graphql查询/突变保持上述行为?如果我将antMatchers(“/ graphql”)。authenticated()更改为antMatchers(“/ graphql”)。permitAll()以允许匿名访问,那么我的自定义AuthenticationProvider不会再被调用,即使当我尝试使用“基本http认证”进行认证。
谢谢!
这里是我的CONFIGS:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationProvider authenticationProvider;
@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
authenticationManagerBuilder.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/graphql").authenticated()
.and()
.requestCache()
.requestCache(new NullRequestCache())
.and()
.httpBasic()
.and()
.headers()
.frameOptions().sameOrigin() // needed for H2 web console
.and()
.sessionManagement()
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.sessionRegistry(sessionRegistry());
}
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 180)
public class HttpSessionConfig {
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
}