2013-06-04 58 views
7

Tôi đang cố gắng để có bối cảnh an ninh mùa xuân trong bối cảnh con, vì vậy tôi có thể có bảo mật url trên tập tin ngữ cảnh servlet.Làm thế nào để có bối cảnh an ninh mùa xuân trong bối cảnh con

tôi có:

<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-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:/spring-security.xml 
    </param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>myapp-soap</servlet-name> 
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>transformWsdlLocations</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    </servlet> 

trên web.xml, cấu hình bảo mật chung về mùa xuân-security.xml và

<!-- Authorization configurations --> 
<security:http auto-config="false" use-expressions="true" 
    create-session="never" 
    authentication-manager-ref="authenticationManager" 
    entry-point-ref="authenticationEntryPoint"> 

    <security:custom-filter 
     position="PRE_AUTH_FILTER" ref="serviceAuthenticationFilter"/> 

    <security:intercept-url 
     pattern="/GetForbiddenUrl" access="hasRole('roleThatDoesntExist')" /> 
    <security:intercept-url pattern="/**" access="permitAll" /> 
</security:http> 
<!-- annotation security --> 
<security:global-method-security pre-post-annotations="enabled"/> 

trên myapp-xà-servlet.xml. Nó không làm việc nhưng không thành công với

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/my-app/v1/soap]] (ServerService Thread Pool -- 192) JBWEB000284: Exception starting filter springSecurityFilterChain: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined 

Tuy nhiên, nếu tôi di chuyển <security:http> phần để cấu hình bối cảnh gốc xuân-an ninh, mọi thứ hoạt động. Nó có nên hoạt động theo cách tôi thử không? Làm cách nào để có được bảo mật dựa trên url trong ngữ cảnh con của tôi?

Tôi cũng đã thử kết hợp các tệp ngữ cảnh thành một, nhưng vấn đề tương tự dường như xảy ra.

+0

Bạn có chắc chắn spring-security.xml của bạn được chọn vào mùa xuân không? –

+0

@MaksymDemidas vâng, khi di chuyển phần 'http' dẫn đến tất cả các chỉ thị được sử dụng, trong cả ngữ cảnh parnt và con – eis

Trả lời

17

Các DelegatingFilterProxy sẽ bởi giao diện mặc định trong ApplicationContext gốc có nghĩa là theo mặc định bạn cần phải đặt cấu hình <http> của bạn ở đó (đó là những gì tạo ra springSecurityFilterChain).

Tuy nhiên, bạn có thể chỉ định sử dụng DelegatingFilterProxy một khác nhau ApplicationContext bằng cách chỉ định contextAttribute để sử dụng. Để làm được bản cập nhật này web.xml của bạn như hình dưới đây

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <init-param> 
     <param-name>contextAttribute</param-name> 
     <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.myapp-soap</param-value> 
    </init-param> 
</filter> 

Một ví dụ tương tự như sử dụng Xuân An 3.2 + 's AbstractSecurityWebApplicationInitializer có thể được nhìn thấy dưới đây:

public class SecurityApplicationInitializer extends 
     AbstractSecurityWebApplicationInitializer { 

    @Override 
    protected String getDispatcherWebApplicationContextSuffix() { 
     // NOTE: if you are using AbstractDispatcherServletInitializer or 
     // AbstractAnnotationConfigDispatcherServletInitializer You probably 
     // want this value to be "dispatcher" 
     return "myapp-soap"; 
    } 

} 

này hoạt động bởi vì nó sẽ thay đổi tên của Thuộc tính ServletContext mà DelegatingFilterProxy sử dụng để tra cứu ApplicationContext. Thay vì sử dụng giá trị mặc định phát hiện ra gốc ApplicationContext, nó bây giờ sử dụng thuộc tính mà bạn đang sử dụng MessageDispatcherServlet (do đó trỏ đến ngữ cảnh con).

Lưu ý rằng MessageDispatcherServlet 's (hoặc bất kỳ lớp con của FrameworkServlet như DispatcherServlet) lưu trữ các ApplicationContext trong ServletContext sử dụng tên thuộc tính "org.springframework.web.servlet.FrameworkServlet.CONTEXT." + <servlet-name> nơi <servlet-name> là tên của servlet. Vì vậy, trong trường hợp này, thuộc tính phải được cấu hình là org.springframework.web.servlet.FrameworkServlet.CONTEXT.myapp-soap. Nếu bạn đã thay đổi tên servlet-name from myapp-soap thành spring-servlet, thì bạn sẽ sử dụng org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring-servlet thay thế.

PS Tôi nghĩ rằng chủ đề nên đọc "Làm thế nào để bối cảnh an ninh mùa xuân như bối cảnh con"

+0

wow, nghe chính xác những gì tôi cần. Tôi sẽ thử điều này và trở lại với kết quả. – eis

+0

hoạt động như một sự quyến rũ. – eis

2

<security:http> sẽ phải chuyển đến ngữ cảnh ứng dụng chính thay vì ngữ cảnh con (servlet), vì yếu tố không gian tên bảo mật này tạo ra hạt springSecurityFilterChain, được tra cứu bởi DelegatingFilterProxy trong ngữ cảnh chính. Như javadoc của nó nêu rõ:

web.xml thường sẽ chứa một định nghĩa DelegatingFilterProxy, với quy định filter-name tương ứng với một tên đậu trong ứng dụng bối cảnh gốc Spring.

Các vấn đề liên quan