0

私はgraphqlで春のブートプロジェクトに取り組んでいます。私はgraphql-java-toolsとgraphql-spring-boot-starterを使っています。私は、以下のjava設定ファイルで確認できるように、セキュリティとセッション管理を春のセキュリティで設定することができました。私はgraphqlを使用している春のブートで認証

"/ graphql"パスが保護されています(要求のhttpヘッダーに「基本HTTP認証」またはセッショントークン(x-auth-token)のみを送信することでアクセスできます)。任意のgraphql操作で「基本的なHTTP認証」を使用して認証すると、新しいセッションが開始され、新しいセッショントークンがヘッダーに返され、そのトークンを使用してそのセッションを続行できます。

上記の動作を維持しているグラフクエリー/突然変異に対して匿名ユーザーにアクセスする方法はありますか?

匿名アクセスを許可するためにantMatchers( "/ graphql")、authenticated()をantMatchers( "/ graphql")、permitAll()に変更した場合、私のカスタム認証プロバイダはもはや呼び出されません「基本的な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(); 
} 

} 

答えて

0

代わりの.antMatchers( "/ graphql")(認証済み)我々は( "/ graphql").antMatchersを使用permitAll()。 .httpBasic()を削除し、カスタムAuthenticationProviderも削除しました。今、セキュリティのconfigsは、次のようになります。

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/graphql").permitAll() 
      .and() 
      .requestCache() 
      .requestCache(new NullRequestCache()) 
      .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(); 
} 

}

その後、我々は、ユーザーの資格情報を受け入れ、セッショントークンを返しログインの変異を作成しました。ここでgraphqlスキーマです:基本的に私たちは私たちのカスタムAuthenticationProviderに持っていたコードは、ログイン操作によって呼び出されたサービスに入った

login(credentials: CredentialsInputDto!): String 

input CredentialsInputDto { 
    username: String! 
    password: String! 
} 

public String login(CredentialsInputDto credentials) { 
    String username = credentials.getUsername(); 
    String password = credentials.getPassword(); 

    UserDetails userDetails = userDetailsService.loadUserByUsername(username); 

    ... credential checks and third party authentication ... 

    Authentication authentication = new UsernamePasswordAuthenticationToken(username, password, userDetails.getAuthorities()); 
    SecurityContextHolder.getContext().setAuthentication(authentication); 
    httpSession.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext()); 
    return httpSession.getId(); 
} 

キーは、私たちがセッションコンテキストを用意していることです認証されたユーザーの認証で認証した後、「SPRING_SECURITY_CONTEXT」というセッション属性として(再認証で)保存します。これは、ログイン処理から取得したセッショントークンの値を「x-auth-token」ヘッダに設定してリクエストを行うと、Springがコンテキストを自動的に復元できるようにするために必要なことです。 .antMatchers( "/ graphql")。permitAll()のために匿名の呼び出しが許可されています。サービス層では、次のような注釈を使用できます。@Preauthorize( "isAnonymous()OR hasRole(" USER " ) ")。

関連する問題