2011-07-11 14 views
14

@Controllerのメソッドで@Securedが読み込まれていないように見えます。 sec:intercept-urlに基づくセキュリティフィルタリングが使用されている場合、これは正常に動作しているようです。@Securedはコントローラでは機能しませんが、intercept-urlは正常に動作しているようです。

DEBUG:: - 公共オブジェクト - 認証

ウェブをしようとしないorg.springframework.security.web.access.intercept.FilterSecurityInterceptor春のセキュリティに次のコードの結果は、私にこのログエントリを与えます。 XML

contextConfigLocation /WEB-INF/spring/root-context.xml

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/appServlet/servlet-context.xml 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<!-- Filter security --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

サーブレットのcontext.xmlはviewResolversと、すべてのマーシャリングの設定を保持します。この設定はアノテーション駆動です。

ルートのcontext.xml

<sec:global-method-security secured-annotations="enabled" /> 

<sec:http auto-config="true"> 
    <sec:http-basic/> 
</sec:http> 

<!-- Declare an authentication-manager to use a custom userDetailsService --> 
<sec:authentication-manager> 
    <sec:authentication-provider 
     user-service-ref="userDetailsService"> 
     <sec:password-encoder ref="passwordEncoder" /> 
    </sec:authentication-provider> 
</sec:authentication-manager> 

<bean 
    class="org.springframework.security.authentication.encoding.PlaintextPasswordEncoder" 
    id="passwordEncoder" /> 
<sec:user-service id="userDetailsService"> 
    <sec:user name="john" password="john" authorities="ROLE_USER, ROLE_ADMIN" /> 
    <sec:user name="jane" password="jane" authorities="ROLE_USER" /> 
</sec:user-service> 

PingController.java

@Controller 
public class PingController { 

    @Secured("ROLE_ADMIN") 
    @RequestMapping(value = "/ping", method = RequestMethod.GET) 
    public void ping() { 
    } 

} 

これは私が使用している認証方式とは関係を持っているように見える、そうしませんbasic-http-tagを見落とすことができます。

@Securedは、セキュリティが設定されているroot-context.xml以外のコンテキストで使用されているため、@ Secureが機能しないという考えがあります。この設定をservlet-context.xmlに移そうとしましたが、springSecurityFilterChainに到達していないようです。問題と私の理論の考え?

答えて

23

あなたは正しく、コンテキストごとに<global-method-security>が適用されます。ただし、セキュリティ設定全体をservlet-context.xmlに移動する必要はありません。単に<global-method-security>要素を追加するだけです。

+0

スポットオン。ありがとう、@axtavt! – kareblak

+0

素晴らしい!ありがとう:) – Athar

+1

Javaの設定方法を使用している場合は、 'WebMvcConfigurerAdapter'を拡張する設定クラスに' @ EnableGlobalMethodSecurity'アノテーションを追加することができます。 – beat

9

Spring Security FAQ(emphasis mine)を参照してください。あなたがサービス層にポイントカットを適用する場合は、アプリケーションのセキュリティコンテキストに<global-method-security>を設定するだけで済みます。春のWebアプリケーション、ディスパッチャサーブレットの スプリングMVC豆を保持するアプリケーションのコンテキストにおいて

しばしば メインアプリケーションコンテキストから分離されます。 myapp-servlet.xmlというファイルで定義されることがよく、myappはweb.xmlのSpring DispatcherServletに割り当てられた名前です。アプリケーションには複数のDispatcherServletsを持つことができ、それぞれ独自の分離されたアプリケーションコンテキストがあります。 これらの「子」コンテキストのBeanは、 アプリケーションの残りの部分には表示されません。 "親"アプリケーションコンテキストは、web.xmlで定義した ContextLoaderListenerによってロードされ、子コンテキストのすべて に表示されます。この親コンテキストは通常​​、 要素を含む のセキュリティ構成を定義する場所です。結果として、 のメソッドに適用されるセキュリティ制約は適用されません。なぜなら、BeanはDispatcherServletコンテキストの には見えないからです。 宣言をWebコンテキストに移動するか、セキュアにしたいBeanをメインアプリケーションコンテキストに移動する必要があります。

通常、個々のWebコントローラではなくサービス レイヤでメソッドセキュリティを適用することをお勧めします。

+0

これは、1つのWebアプリケーションが必要な数のインターフェイスを持つ方法です。それをサービスレイヤーに適用するのが最善の選択ですが、これを行うことの欠点も1つあります。ほとんどの場合、不要なマッピングが適用されます。 – kboom

+0

リンク先を更新してもよろしいですか? –

関連する問題