2017-03-15 5 views
1

私はいくつかのカスタムSpring Sceurity設定を持つJava Web Spring Bootアプリケーションを持っています。私のアプリケーションを実行すると、イメージはAuthenticationProviderを実行していて、それは私に例外を引き起こしています(ユーザーが最初にログインしたときに何らかの更新が行われるためです)。Spring AuthenticationProviderが静的リソースまたはイメージリソースに対して実行されないようにするにはどうすればよいですか?

イメージへのアクセス時にAuthenticationProviderが実行されないようにするにはどうすればよいですか?

私の設定は以下の通りです:

@Order(1) 
@Configuration 
@EnableWebSecurity 
public class BaseSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private AuthenticationEntryPoint restUnauthorizedEntryPoint; 

    @Autowired 
    private AccessDeniedHandler  restAccessDeniedHandler; 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     LOG.info("Ignoring resources in Spring Security"); 
     web.ignoring() 
       .antMatchers("/images/*") 
       .antMatchers("/app/errors/*.html"); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws SecurityConfigException { 
     try { 
       http 
       .antMatcher("/api/**") 
       .authorizeRequests() 
        .accessDecisionManager(accessDecisionManager()) 
        .antMatchers("/api/users/**").hasAnyRole("ADMIN") 
        .anyRequest().authenticated() 

       .and() 
       .exceptionHandling() 
        .authenticationEntryPoint(restUnauthorizedEntryPoint) 
        .accessDeniedHandler(restAccessDeniedHandler) 

       .and() 
       .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class) 
        .csrf().csrfTokenRepository(csrfTokenRepository()) 

       .and() 
       .headers().frameOptions().sameOrigin() 

       .and(); 
     } catch (Exception ex) { 
      throw new SecurityConfigException(ex); 
     } 
    } 

    @Bean 
    public AffirmativeBased accessDecisionManager() { 
     List<AccessDecisionVoter<? extends Object>> decisionVoters = new ArrayList<>(); 
     decisionVoters.add(new WebExpressionVoter()); 
     decisionVoters.add(new RoleHierarchyVoter(roleHierarchy())); 
     return new AffirmativeBased(decisionVoters); 
    } 

    /** 
    * Create the role hierarchy as an implementation of Spring Security {@link RoleHierarchyVoter} where the Spring 
    * roles are created from a combination of {@link Module}_{@link Role}. The AccessDecisionVoter provided with Spring 
    * Security expects the role names to begin with the prefix 'ROLE_'. The hierarchy is as follows: 
    * <p/> 
    * {@code ROLE_COMMON_ADMIN > ROLE_COMMON_USER_ADMIN } and {@code ROLE_COMMON_ADMIN > ROLE_CUSTOM_USER} 
    */ 
    @Bean 
    public RoleHierarchyImpl roleHierarchy() { 
     StringBuilder h = new StringBuilder(); 

     // Common tree, root is COMMON_ADMIN 
     appendIncludesRule(h, Module.COMMON, Role.ADMIN, Module.COMMON, Role.USER_ADMIN); 

     // Custom tree, root is COMMON_ADMIN 
     appendIncludesRule(h, Module.COMMON, Role.ADMIN, Module.CUSTOM, Role.USER); 

     RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl(); 
     roleHierarchy.setHierarchy(h.toString()); 
     return roleHierarchy; 
    } 

    private void appendIncludesRule(StringBuilder h, Module lModule, Role lRole, Module rModule, Role rRole) { 
     h.append(GrantedAuthorityFactory.ROLE_PREFIX).append(lModule).append('_').append(lRole).append('>'); 
     h.append(GrantedAuthorityFactory.ROLE_PREFIX).append(rModule).append('_').append(rRole).append(' '); 
    } 

    /** 
    * Creates a replacement CsrfTokenRepository for use by Spring CSRF protection. It looks for a header called 
    * X-XSRF-TOKEN, which AngularJS sets by default. AngularJS obtains this from the XSRF-TOKEN cookie (set in 
    * CsrfHeaderFilter). 
    * 
    * @return CsrfTokenRepository 
    */ 
    private CsrfTokenRepository csrfTokenRepository() { 
     HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
     repository.setHeaderName(CSRF_HEADER_NAME); 
     return repository; 
    } 
} 

そして、第二の構成クラス:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@Profile({ "prod" }) 
public class UidSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UserAccountService userAccountService; 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     LOG.info("Ignoring resources in Spring Security"); 
     web.ignoring() 
       .antMatchers("/images/*") 
       .antMatchers("/app/errors/*.html"); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) { 
     LOG.info("AuthenticationManagerBuilder: PreAuthenticatedAuthenticationProvider configured"); 
     auth.authenticationProvider(preauthAuthProvider()); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws SecurityConfigException { 
     try { 
      http.addFilterBefore(ssoFilter(), RequestHeaderAuthenticationFilter.class); 
      http.headers().cacheControl().disable(); 
     } catch (SecurityConfigException ex) { 
      LOG.error("SecurityConfigException:", ex); 
      throw ex; 
     } catch (Exception ex) { 
      LOG.error("SecurityConfigException:", ex); 
      throw new SecurityConfigException(ex); 
     } 
    } 

    @Bean 
    public PreAuthenticatedAuthenticationProvider preauthAuthProvider() { 
     PreAuthenticatedAuthenticationProvider preauthAuthProvider = new PreAuthenticatedAuthenticationProvider(); 
     preauthAuthProvider.setPreAuthenticatedUserDetailsService(userDetailsServiceWrapper()); 
     return preauthAuthProvider; 
    } 

    @Bean 
    public UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> userDetailsServiceWrapper() { 
     UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper = new UserDetailsByNameServiceWrapper<>(); 
     wrapper.setUserDetailsService(userAccountService); 
     return wrapper; 
    } 

    @Bean 
    public SsoHeaderFilter ssoFilter() throws SecurityConfigException { 
     SsoHeaderFilter filter = new SsoHeaderFilter(); 
     try { 
      filter.setAuthenticationManager(authenticationManager()); 
     } catch (Exception ex) { 
      LOG.error("SsoHeaderFilter Excpetion:", ex); 
      throw new SecurityConfigException(ex); 
     } 
     return filter; 
    } 
} 

AuthenticationProviderがイメージに実行されている理由は、私は本当に私はすべて無視する春を構成した後でさえも理解していません/ images/*パターン。

ホーム画面でF5を押しても例外が表示されません(ユーザーがアプリに初めてアクセスしたときに更新が行われたため)。ただし、Authプロバイダはまだ実行されているようです。私はあなたのantMatchersがオフに少しかもしれないと思う画像/ mylogo.png

答えて

0

EDIT

イメージのパス。

ここ/app/**/*.html

を試してみてください/images/**代わりの/images/*を試してみて、静的なページのためには、JHipsterからの例です。 CSSと画像コンテンツディレクトリの下で暮らす:

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring() 
     .antMatchers(HttpMethod.OPTIONS, "/**") 
     .antMatchers("/app/**/*.{js,html}") 
     .antMatchers("/bower_components/**") 
     .antMatchers("/i18n/**") 
     .antMatchers("/content/**") 
     .antMatchers("/swagger-ui/index.html") 
     .antMatchers("/test/**"); 
} 
+0

画像を変更する場合は、 'antMatcher'私はあなたではなくネストされたディレクトリ(すなわち/images/avatars/1.png)の画像を参照して推測している、あなたのためにそれを修正します画像から直接ではなく(つまり/images/logo.png) – DeezCashews

+0

画像のパスは "images/mylogo.png"なので、antMatcherはうまく見えます。 – danieldestro

+0

テスト環境で "prod"プロファイルを使用してサーバーを実行していますか?それとも、ローカルで "prod"プロファイルを実行していないため、テストでのみ起こっていますか? – DeezCashews

関連する問題