2016-09-27 3 views
2

私は春にはかなり新しくなっています。スプリング4セキュリティコンフィグレーションは実行されますが、アクティブではありません

SpringベースのREST APIに基本的なHTTP認証を追加しようとしています。

私はquite a fewtutorialsとの文書を今のところ通り過ぎていますが、それらはすべて同じことを示しているようですので、何か不足しているはずです。私が欲しいもの

すべての要求は、デフォルトでHTTP認証の背後にある簡単な構成を定義することですが、@PreAuthorizeを使用して、またはHttpSecurity設定を変更することで、必要に応じて、我々は、メソッドレベルのセキュリティを定義することができます。私のような非常に簡単な構成を定義している

は:

@Configuration 
@EnableWebMvc 
@EnableWebSecurity 
@ComponentScan(basePackages = "com.app.rest") 
public class RESTConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .anyRequest().hasAnyRole("USER") 
       .and() 
       .httpBasic(); 
    } 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); 
    } 
} 

私は、非常に単純なコントローラ(なしサービス、空想何を)持っています。ここで

は非常に基本的な概要です:

@RestController 
@RequestMapping("/images") 
public class ImagesController { 

    @JsonView(View.Basic.class) 
    @RequestMapping(value = "/{id}", method = RequestMethod.GET) 
    public ObjectXvo getImageByID(@PathVariable String id) throws IOException { 

     ObjectXvo result = doThings(); 
     return result; 
    } 
} 

すべてのオンラインチュートリアルに比べて唯一の顕著な違いは、我々は、このAPIをロードする方法です。 Jettyコンテナを既に使用しているため、ほとんどの場合と同様にWebAppInitializerを使用する代わりに、再利用することにしました。ここで

は、RESTのAPIが定義されているかの抽出物である:

// Creating REST Servlet 
ServletContextHandler restHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); 
restHandler.setErrorHandler(null); 
restHandler.setContextPath("/rest"); 

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
context.setConfigLocation("com.app.test"); 
WebApplicationContext webAppContext = context; 

DispatcherServlet dispatcherServlet = new DispatcherServlet(webAppContext); 
ServletHolder springServletHolder = new ServletHolder("REST dispatcher", dispatcherServlet); 
restHandler.addServlet(springServletHolder, "/"); 
restHandler.addEventListener(new ContextLoaderListener(webAppContext)); 

// Creating handler collection 
ContextHandlerCollection handlerCollection = new ContextHandlerCollection(); 
handlerCollection.addHandler(anotherHandler); 
handlerCollection.addHandler(restHandler); 
handlerCollection.addHandler(yetAnotherHandler); 

// Setting server context 
server.setHandler(handlerCollection); 

ものがされてアプリを実行しているとき、私はセットアップ任意のセキュリティスキームをしていなかった場合、私はまだのようにすべてのURLにアクセスできることです。 アプリケーションをデバッグすると、私のconfigureメソッド内のブレークポイントとしてRESTConfigurationが処理されているのを確認できます。

XMLファイルの使用を避けようと思っています。注釈がこれまでより優れていると思います。

このセキュリティ設定が有効化されていない理由を誰かが正しい方向で指摘できますか?

EDIT:それはどのように関連する

わからないが、私はREST方式に@PreAuthorizeを追加しようとした場合、私は次のエラーを取得:

HTTP ERROR: 500 

Problem accessing /rest/images/I147. Reason: 

    An Authentication object was not found in the SecurityContext 

の情報がたくさんありますインターネット上でこれを修正する方法については、これは関連していない場合は私は思っています。

答えて

1

あなたはSpring Security Reference参照、springSecurityFilterChainを登録する必要があります:あなたは手動でspringSecurityFilterChainを登録する必要がAbstractSecurityWebApplicationInitializer使用しない場合は

The next step is to register the springSecurityFilterChain with the war. This can be done in Java Configuration with Spring’s WebApplicationInitializer support in a Servlet 3.0+ environment. Not suprisingly, Spring Security provides a base class AbstractSecurityWebApplicationInitializer that will ensure the springSecurityFilterChain gets registered for you. The way in which we use AbstractSecurityWebApplicationInitializer differs depending on if we are already using Spring or if Spring Security is the only Spring component in our application.

を:

webAppContext.getServletContext() 
    .addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")) 
    .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.ASYNC), false, "/*"); 

も参照してください:

+0

どうもありがとう!以前に提供されたサンプルに照らして、restHandlerにフィルターを追加する必要がありました。 – jlengrand

関連する問題