0
我有一個可以運行的Spring REST網絡服務,並且想爲它添加基本的身份驗證。控制器方法具有正常的URL和附加到它們的HTTP方法註釋。爲了增加安全性,我做兩件事情用Spring來保護REST網址
1)彈簧security.xml文件添加到它看起來像WEB-INF文件夾:
<?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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http auto-config="true">
<security:http-basic />
<security:intercept-url pattern="/*" access="ROLE_USER" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="spring" password="spring" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
2)配置Spring Security在web.xml爲:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3)彈簧控制器:
package com.sample.main;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sample.employee.Employee;
@Controller
public class EmpController
{
List<Employee> list = new ArrayList<Employee>();
@RequestMapping(value = "/getEmp/{emp}", method = RequestMethod.GET)
public @ResponseBody Employee getEmployee(@PathVariable("emp") int empid) {
System.out.println("meet getEmp");
for (Iterator<Employee> iterator = list.iterator(); iterator.hasNext();) {
Employee emp = (Employee) iterator.next();
if(emp.getEmpId()==empid) {
return emp;
}
}
return new Employee();
}
}
4)的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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.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="com.sample" />
<!-- 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>
5)爲spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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-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">
</beans>
我已經上傳完整申請: http://www.javaexperience.com/wp-content/uploads/2012/12/SpringWebServiceSecurity.zip
配置springSecurityFilterChain後,我開始變得對REST的URL 404個錯誤頁面中的應用。任何想法...
確定。通過這樣做,我在GET瀏覽器中獲得了一個登錄表單,當我將用戶輸入爲「spring」,密碼爲「spring」時,我得到了404錯誤頁面,當時Web瀏覽器中的URL是「http:/ /本地主機:8080/SpringWebServiceSecurity/getEmp/1 /」。如果我從我的應用程序中完全刪除安全篩選器,則此工作原理也適用。發生了一些導致此問題的重定向。 – Sandeep
@Sandeep,你可以發佈url的控制器嗎?另外,您是否在更改調度程序Servlet後嘗試執行此操作,而沒有安全過濾器?我懷疑Spring MVC的配置可能關閉。如果你發佈你的配置,我可以看看。 –
嘿凱文,我已經添加了控制器代碼和applicationContext.xml文件。事實上,我已經上傳了該應用程序。 zip在http://www.javaexperience.com/wp-content/uploads/2012/12/SpringWebServiceSecurity.zip – Sandeep