2

申し訳ありませんが、SpringSecurityは初めてです。私は次のapplicationContext.xmlを持っています

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />

    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package-->
    <context:component-scan base-package="org.assessme.com" />

    <!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
    <mvc:annotation-driven /> 

</beans>

私はチュートリアルに従っています...

http://static.springsource.org/spring-security/site/tutorial.html

私の質問は、既存のapplicationContext.xmlに追加する必要がありますか、それとも別のXMLファイルを作成する必要がありますか?

私のweb.xmlは次のとおりです...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <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>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

チュートリアルのように少し混乱しています。xmlのcontext-paramを指定していますが、すでに宣言されています。複数のcontext-paramを使用できますか?誰かが私にspringmvcとspringsecurityを一緒に使用する最良の方法のアイデアを教えてくれたら、それは私がxmlファイルを「マージ」するのが難しいと思っている現時点で素晴らしいでしょう。

ありがとう、

4

2 に答える 2

7

セキュリティ構成を別のファイルに入れるか、既存のアプリケーション コンテキストと組み合わせることができます。既存のアプリケーション コンテキストを使用する場合。次のように、デフォルトの名前空間を Bean として保持します。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.1.xsd">

      <security:http auto-config="true">
         <security:intercept-url pattern="/**" access="ROLE_USER" />
      </security:http>
   ...
</beans>

また、すべてのセキュリティ要素にセキュリティのプレフィックスを付ける必要があります。

ただし、別のファイルで定義する場合。利点は、セキュリティをデフォルトの名前空間として使用し、次のようにセキュリティ プレフィックスを省略できることです。

 <beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/security
       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

      <http auto-config='true'>
           <intercept-url pattern="/**" access="ROLE_USER" />
      </http>
      ...
</beans:beans>

一般的な方法は、次のようにファイル名を定義することです。

 1)applicationContext.xml
 2)applicationContext-security.xml

そしてあなたのweb.xmlで次のように:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

または、以下のようにコンマまたはスペースで区切られたリストとしても:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml /WEB-INF/applicationContext-security.xml</param-value>
</context-param>

ドキュメント: ContextLoader

ドキュメント: 名前空間の構成

于 2012-07-02T23:06:57.170 に答える
0

フォローしているチュートリアルでは、次のものも使用します。

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
       classpath:applicationContext-business.xml
       /WEB-INF/security-app-context.xml
   </param-value>
</context-param>

これapplicationContext-business.xmlはあなたのようなものroot-context.xmlです。そのため、Spring セキュリティ構成ファイルへのパスを追加する必要があります。また、 に記載されているセキュリティ フィルタも忘れずに含めてくださいweb.xml

于 2012-07-02T19:52:48.243 に答える