小编Rol*_*and的帖子

使用graphql在spring boot中进行身份验证

我正在使用graphql开发一个spring boot项目.我正在使用graphql-java-tools和graphql-spring-boot-starter.我设法使用spring安全性配置安全性和会话管理,如下面的java配置文件中所示.

现在"/ graphql"路径是安全的(只能在请求的http头中发送"基本http身份验证"或会话令牌(x-auth-token)).在任何graphql操作上使用"基本http身份验证"进行身份验证将启动新会话并在标头中发回新会话令牌,并且可以进一步使用该令牌继续该会话.

如何让匿名用户访问某些graphql查询/突变保持上述行为?

如果我将antMatchers("/ graphql").authenticated()更改为antMatchers("/ graphql").permitAll()以允许匿名访问,那么即使我尝试使用"basic"进行身份验证,也不再调用自定义AuthenticationProvider http身份验证".

谢谢!

这是我的配置:

@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() …
Run Code Online (Sandbox Code Playgroud)

authentication spring-security spring-boot graphql graphql-java

11
推荐指数
2
解决办法
6987
查看次数