14

@Controller のメソッドの @Secured が読み取られているようには見えません。sec:intercept-url に基づくセキュリティ フィルタリングが使用されている場合、これは問題なく機能しているようです。次のコードを実行すると、Spring Security で次のログ エントリが返されます。

DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - パブリック オブジェクト - 認証が試行されていません

web.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>

servlet-context.xml は、viewResolvers とすべてのマーシャリングの構成を保持します。この構成は注釈駆動型です。

root-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 は見落とされる可能性があります。

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

4

2 に答える 2

24

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

于 2011-07-11T15:27:23.913 に答える
9

Spring Security FAQを参照してください(強調は私のものです)。ポイントカットをサービス レイヤーに適用する場合<global-method-security>は、アプリのセキュリティ コンテキストで設定するだけで済みます。

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

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

于 2012-07-05T15:36:03.790 に答える