我試圖使用Spring 3.2.4和Spring Security 3.2使用@Secured註解來保護我的RESTful API。我有以下設置:Spring Security 3.2:@Secured註釋未考慮
的web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/*.xml
/WEB-INF/classes/security/security-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring 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 configuration -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/servlet/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>
的servlet-context.xml中:
<context:component-scan base-package="com.mycompany.rest.controller" />
<security:global-method-security secured-annotations="enabled" />
安全的context.xml
<beans:bean id="merchantUserDetailsService" class="com.mycompany.rest.security.CustomUserDetailsService" />
<http auto-config="false" create-session="never">
<http-basic />
</http>
<authentication-manager>
<authentication-provider user-service-ref="customUserDetailsService" />
</authentication-manager>
我編程方式分配將customUserDetailsService中的自定義角色(ROLE_GROUP,ROLE_DIVISION,ROLE_READ,ROLE_WRITE)提供給用戶,工作正常。
我的一個控制器:
@Secured("ROLE_DIVISION")
@RequestMapping(method = RequestMethod.GET)
ResponseEntity<List<CustomerResource>> getCustomer() throws ResourceDoestNotExistException {
List<Customer> providers = // retrieve providers from DAO
List<CustomerResource> resources = customerResourceAssembler.toResources(customers);
return new ResponseEntity<>(resources, HttpStatus.OK);
}
現在我的問題,@Secured註釋被忽略。我想使用@Secured註釋來避免在配置中定義多個。當我添加至少一個Spring Security時,Spring Security正常工作,但是如何避免定義它們,而是依賴於@Secured註釋?
我現在可以通過角色「ROLE_GROUP」訪問上面的方法。
對不起,這是我複製粘貼錯誤的代碼片段(正在嘗試的東西)。我更新了原始帖子。 –
沒有定義任何是否正確? –
它不應該以任何方式影響方法攔截。如果它被應用,你應該會得到一個異常。如果您確定該方法實際上被調用,請檢查它是否實際上被代理(添加一個斷點並查看堆棧或添加一個Thread.dumpStack()調用)。 –