2016-08-17 10 views
0

私は専門職によるJavaプログラマーではありません。私は私たちの研究室のためのアプリケーションを構築するために取り組んでいます。 JSPファイルを持つSpring MVCアプリとして始まりました。スタンドアロンの認証および認可サーバーとしてOAUTH2を使用するSpring REST APIに移行しました。私はデータとユーザーを格納するためにMySQLデータベースを使用しています。 Postmanを使ってサーバーにアクセスすると、access_tokenが正常に発行されます。しかし、Angular 2クライアントを使用すると、クライアントがサーバーに送信したプリフライトOPTIONSリクエストを通過できなくなります。私はCORSフィルタを設定するいくつかの異なる方法を試しましたが、実装するためにそのフィルタを取得することができず、私はいつも飛行前の要求に対して返された401コードを取得します。私は春のブログhttps://spring.io/blog/2015/06/08/cors-support-in-spring-frameworkで提案されたソリューションを実装しようとしましたが、それはまだ動作していません。私のpom.xmlファイルでOAUTH2のプリフライトCORSリクエストに対応するSpring Securityの入手方法

は、私はそれが今であるように私も春ブーツ1.4.0 FilterRegistrationBean

ここ

私のコードがあるを使用しようとして含ま春

Spring Framework - 4.3.2.RELEASE 
Spring Security - 4.1.2.RELEASE 
Spring Security OAUTH2 - 2.0.10.RELEASE 

のこれらのバージョンを使用しています。

WebConfig Class: 
@EnableWebMvc 
@Configuration 
@ComponentScan(basePackages={"eng.lab"}) 
@Import({ApplicationContext.class}) 
@PropertySource("classpath:application.properties") 
public class WebConfig extends WebMvcConfigurerAdapter{ 

/* @Override 
    public void addCorsMappings(CorsRegistry registry) { 
     registry.addMapping("/**"); 
    }*/ 

} 

SecurityConfigクラス:

@Configuration 
@EnableWebSecurity 
@PropertySource("classpath:application.properties") 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 


    @Autowired 
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
     auth.jdbcAuthentication().dataSource(dataSource) 
     .usersByUsernameQuery(
     "select username,password, enabled from user where username=?") 
     .authoritiesByUsernameQuery(
     "select u.username, r.role from User u, Role r where r.id=u.roleId and u.userName=?"); 
    } 


    @Autowired 
    private ClientDetailsService clientDetailsService; 


      @Override 
      protected void configure(HttpSecurity http) throws Exception { 
       http 
       .csrf().disable() 
       .anonymous().disable() 
       .authorizeRequests() 
       .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll() 
       //.requestMatchers(CorsUtils::isCorsRequest).permitAll() 
       .antMatchers("/oauth/token").permitAll(); 
      } 

      @Override 
      @Bean 
      public AuthenticationManager authenticationManagerBean() throws Exception { 
       return super.authenticationManagerBean(); 
      } 


      @Bean 
      public TokenStore tokenStore() { 
       return new InMemoryTokenStore(); 
      } 

      @Bean 
      @Autowired 
      public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){ 
       TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler(); 
       handler.setTokenStore(tokenStore); 
       handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService)); 
       handler.setClientDetailsService(clientDetailsService); 
       return handler; 
      } 

      @Bean 
      @Autowired 
      public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception { 
       TokenApprovalStore store = new TokenApprovalStore(); 
       store.setTokenStore(tokenStore); 
       return store; 
      } 

     } 

MethodSecurityConfigクラス:

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) 
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { 

    @SuppressWarnings("unused") 
    @Autowired 
    private SecurityConfig securityConfig; 

    @Override 
    protected MethodSecurityExpressionHandler createExpressionHandler() { 
     return new OAuth2MethodSecurityExpressionHandler(); 
    } 
} 

WebAppInitializerクラス:

public class WebAppInitializer implements WebApplicationInitializer{ 

    @Override 
    public void onStartup(ServletContext container) throws ServletException { 

     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.register(WebConfig.class); 

     container.addListener(new ContextLoaderListener(rootContext)); 

     DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain"); 

     DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext); 

     ServletRegistration.Dynamic registration = container.addServlet("dispatcherServlet", dispatcherServlet); 

     container.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*"); 

     registration.setLoadOnStartup(1); 
     registration.addMapping("/"); 

    } 
} 

SimpleCorsFilterクラス:

@Component 
@Order(Ordered.HIGHEST_PRECEDENCE) 
public class SimpleCorsFilter implements Filter { 

    public SimpleCorsFilter() { 
    } 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     HttpServletResponse response = (HttpServletResponse) res; 
     HttpServletRequest request = (HttpServletRequest) req; 
     response.setHeader("Access-Control-Allow-Origin", "*"); 
     response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
     response.setHeader("Access-Control-Max-Age", "3600"); 
     response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization"); 

     if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { 
      response.setStatus(HttpServletResponse.SC_OK); 
     } else { 
      chain.doFilter(req, res); 
     } 
    } 

    @Override 
    public void init(FilterConfig filterConfig) { 
    } 

    @Override 
    public void destroy() { 
    } 
} 

私が2番目のWebSecurityConfigクラスを組み込むと、代わりに403コードが取得されます。

MyWebSecurityクラス:

@Configuration 
@EnableWebSecurity 
@Order(-1) 
public class MyWebSecurity extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
      .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll(); 
    } 
} 

任意の提案DATAREST-573のバグが修正されるのを待っているを超えて飛行前の問題を乗り越える方法に関しては?

更新:私はBenjaminの提案を試みましたが、解決策を適切に実装しているかどうかはわかりません。私はWebConfigクラスをフォローとして編集しましたが、プレフライトはまだ失敗します。フィルタはまだ適用されていません。

package eng.lab.config; 


import javax.servlet.Filter; 

import org.springframework.boot.web.servlet.FilterRegistrationBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@EnableWebMvc 
@Configuration 
@ComponentScan(basePackages={"eng.lab"}) 
@Import({ApplicationContext.class}) 
@PropertySource("classpath:application.properties") 
public class WebConfig extends WebMvcConfigurerAdapter{ 

    //more custome rule beans 

    @Bean 
    public FilterRegistrationBean simpleCORSFilterRegistration() { 

     FilterRegistrationBean registration = new FilterRegistrationBean(); 
     registration.setFilter(new SimpleCorsFilter()); 
     registration.addUrlPatterns("/*"); 
     //registration.addInitParameter("paramName", "paramValue"); 
     registration.setName("simpleCorsFilter"); 
     registration.setOrder(0); 
     return registration; 
    } 

    @Bean(name = "simpleCorsFilter") 
    public Filter simpleCorsFilter() { 
     return new SimpleCorsFilter(); 
    } 

/* @Override 
    public void addCorsMappings(CorsRegistry registry) { 
     registry.addMapping("/**"); 
    }*/ 

} 
+0

があなたのSimpleCorsFilterは、javax.servlet.Filterのですか? –

+0

はい。私はこのimport import javax.servlet.Filterを使いました。 –

+0

このクラスのコードは実行されていないと思います。あなたは確認できますか? –

答えて

0

あなたのCORSフィルタは登録されていません。これは標準のjavax.servlet.Filterインスタンスなので、FilterRegistrationBeanに登録するか、web.xmlファイルで宣言する必要があります。

あなたが詳細については、このSO質問を参照することができます。How to add a filter class in Spring Boot?

関連する問題