2016-01-24 114 views
0

環境:Spring MVC的REST +彈簧安全+基本身份驗證

春4.1

春季安全4.0

問題:

我開發使用Spring一個簡單的REST服務4.1。並使用Spring安全性進行身份驗證。 我正在使用HTTP基本驗證。

問題是,即使所有配置都正確,基本身份驗證仍無法正常工作。 我正在使用郵遞員向服務器發送請求。 REST客戶端可以調用沒有授權標頭的REST控制器方法。 該方法在沒有任何驗證錯誤的情況下成功執行。

由於我使用Tomcat 6,因此我沒有使用servlet 3.0功能,所以web.xml確實存在。 已在REST控制器層上使用@Secured批註實施了方法級安全性。

任何人都可以請求幫助我去哪裏錯了嗎?

代碼:

web.xml中

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet-security.xml</param-value> 
    </context-param> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
     </init-param>  
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <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> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher>  
    </filter-mapping> 

</web-app> 

MVC-servlet的調度員security.xml文件:

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

    <http use-expressions="true" create-session="stateless"> 
     <http-basic/> 
     <csrf disabled="true"/> 
    </http> 

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

    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="XYZ" password="12345" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

MVC-調度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- Specifying base package of the Components like Controller, Service, DAO --> 
    <context:component-scan base-package="org.ngo" /> 
    <!-- Getting Database properties --> 
    <context:property-placeholder location="classpath:application.properties"/> 

    <mvc:annotation-driven/> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" /> 
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource"> 
      <ref bean="dataSource" /> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop>    
      </props> 
     </property> 
     <property name="packagesToScan" value="org.ngo.abhishek.entity"></property> 
    </bean> 

    <!-- Transaction --> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 
</beans> 

其餘控制器:

@RestController 
@RequestMapping("/abhishek") 
public class AbhishekController { 

    @Autowired 
    private AbhisheskService abhishekService; 

    @RequestMapping(method=RequestMethod.POST,consumes="application/json") 
    @Secured("ROLE_USER") 
    public ResponseEntity<Boolean> getUserById(@RequestBody List<AbhishekDTO> abhishekDtoList) { 

     boolean flag = this.abhishekService.createAbhishek(abhishekDtoList);  
     return new ResponseEntity<Boolean>(flag, HttpStatus.OK);  
    } 

} 

回答

1

從Stiletto得到線索後,我刪除了@Secured("ROLE_USER")並使用基於表達式的安全檢查。它工作(使用攔截url)。所以問題出在了@Secured的位置。

由於@Secured處於調度程序servlet上下文(按照Spring原理的子上下文),並且spring安全範圍位於applicationContext(父上下文)中,所以spring安全性被忽略。

<security:global-method-security secured-annotations="enabled"/>置於mvc-dispatcher-servlet.xml解決了這個問題。

SO上的相似問題:Spring MVC, Method level security

1

我想你的設置,它爲我工作。由於你沒有提供你所有的代碼,我最好的猜測是你的控制器的組件掃描沒有發生,或者你的瀏覽器緩存併發送基本認證憑證,但你沒有意識到它。

+0

感謝您的回覆。我會檢查這兩件事 – Atul