2017-02-20 11 views
1

私はAngular2で作業中のフロントエンドとJavaで動作するバックエンドを持っており、私のフロントエンドリソースを含んでいる静的フォルダからindex.htmlを提供しています。問題は、バックエンドにSpring Securityを追加しようとすると、@EnableWebSecurityアノテーションのためにリソースにアクセスできなくなることです。ローカルホストhttp://localhost:8080/に移動すると、index.htmlは配信されません。しかし、パスまたは手動でパスを書いている他のリソースにアクセスすると、ロードされます。 私はフロントエンドに異なったサービスを提供したくないです。静的からこれを行う方法はありますか?私は、次のことを試してみました:サービスSpring Securityの静的フォルダから角度2のプロジェクト

ここに私のセキュリティ設定:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@ComponentScan(basePackages = {"com.ramso.restapi.security"}) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class); 

public static final String REMEMBER_ME_KEY = "rememberme_key"; 

public SecurityConfig() { 
    super(); 
    logger.info("loading SecurityConfig ................................................ "); 
} 

@Autowired 
private UserDetailsService userDetailsService; 

@Autowired 
private RestUnauthorizedEntryPoint restAuthenticationEntryPoint; 


@Autowired 
private AuthenticationSuccessHandler restAuthenticationSuccessHandler; 

@Autowired 
private AuthenticationFailureHandler restAuthenticationFailureHandler; 

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(userDetailsService); 
} 


@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/front/**","/index.html"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .headers().disable() 
     .csrf().disable() 
     .authorizeRequests() 
      .antMatchers("/failure").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .exceptionHandling() 
      .authenticationEntryPoint(restAuthenticationEntryPoint) 
      .and() 
     .formLogin() 
      .loginPage("/login") 
      .loginProcessingUrl("/authenticate") 
      .successHandler(restAuthenticationSuccessHandler) 
      .failureHandler(restAuthenticationFailureHandler) 
      .usernameParameter("username") 
      .passwordParameter("password") 
      .permitAll() 
      .and() 
     .logout() 
      .logoutUrl("/logout") 
      .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()) 
      .deleteCookies("JSESSIONID") 
      .permitAll() 
      .and(); 

} 
} 

がWebMvcConfiguration:

@Configuration 
public class WebMvcConfiguration extends WebMvcConfigurerAdapter { 

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
//registry.addViewController("/").setViewName("front/index.html"); 
//registry.addViewController("/").setViewName("forward:/index.html"); 
    registry.addViewController("/").setViewName("redirect:/index.html"); 

registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
} 

} 

Application.java:

@SpringBootApplication 
public class Application { 

public static void main(String[] args) { 
    SpringApplication.run(Application.class, args); 
} 
} 

答えて

2

を追加することができWebSecurityConfigurerAdapterを拡張するクラスで以下:

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/front/**"); 
} 

web.ignoring()メソッドに入れたアリマッチャーは、春のセキュリティでは無視する必要があります。

デフォルトで静的コンテンツは(spring boot - static contentから)src/main/resourcesの下で、次のいずれかのディレクトリに配置する必要があります:

/META-INF/resources/ 
/resources/ 
/static/ 
/public/ 

次に任意のアリマッチャーはサブフォルダの前にチェックされています。

たとえば、静的コンテンツがsrc/main/resources/static/frontの場合、ant matcher /front/**は、そのサブフォルダ内のすべてのリソースを無視する必要があります。また

、あなたはsrc/main/resources/staticに置き、あなたのサイトにアクセスする際、メインのリソースとして公開するために、以下のクラスのようなものを追加する必要がありindex.html露出させるために:

@Configuration 
public class WebMvcConfiguration extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("index.html"); 
     registry.setOrder(Ordered.HIGHEST_PRECEDENCE); 
    } 

} 

そしてもちろんそれを追加web.ignoring().antMatchers("/*", "/front/**", "index.html");

/*すべてが許可されません。/**/apiなどの安全なエンドポイントや、無視されたパスの静的コンテンツにAPIを配置してください。

+0

静的にまたはフォルダの前面にsrc/main/resources/static/front? – Tom

+0

私はsrc/main/resources/static/frontの両方で試しました – Battalgazi

+0

特定のリソースにアクセスして、問題がant matcherかindex.htmlかどうかを調べることができますか? index.htmlを公開する際に問題が発生した場合は(フロントフォルダにあると仮定して)、関連するコードを追加して回答に表示することができます。 – Tom

-1

angle-cliがすべてのリソースをアプリのルートに配置するという問題が発生する可能性があります。 (私はあなたが角度cliを使用しているとは確信していませんが)

しかし、この概念を楽しませてください。

角度-CLI出力

/inline.8faab088ca0e1cca9031.bundle.js 
/main.c7b67df9d7c42383815c.bundle.js 
/polyfills.c234d2c8820869e3c698.bundle.js 
/styles.d41d8cd98f00b204e980.bundle.css 
/vendor.f0f36dacdf3fba21c295.bundle.js 

あなたは春のセキュリティantMatcherを適用するためにこれは非常に難しいことになるようなもの。

角度-CLIはすべてのスクリプトファイルは次にあなたはトムが示唆するように、パスを無視することができるはず/ui/

<script type="text/javascript" src="/ui/inline.4c30f42211c1d30d3dd3.bundle.js"></script> 
<script type="text/javascript" src="/ui/polyfills.c234d2c8820869e3c698.bundle.js"></script> 
<script type="text/javascript" src="/ui/vendor.f0f36dacdf3fba21c295.bundle.js"></script> 
<script type="text/javascript" src="/ui/main.c7b67df9d7c42383815c.bundle.js"></script> 

と呼ばれるディレクトリを指していることを意味します--deploy-url=/ui/と呼ばれる構成となっています。

+0

角度クリを使わないIm – Battalgazi

関連する問題