【问题标题】:Securing REST urls with Spring使用 Spring 保护 REST url
【发布时间】:2012-11-29 23:05:08
【问题描述】:

我有一个正常工作的 Spring REST Web 服务,并希望向它添加基本身份验证。控制器方法附有普通的 url 和 HTTP 方法注释。为了增加安全性,我做了两件事

1) 将 spring-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)在web.xml中配置spring security为:

<?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 错误页面。任何想法...

【问题讨论】:

    标签: java spring security spring-security


    【解决方案1】:

    尝试更改调度程序 servlet 的 url-mapping 以处理所有请求。

    Web.xml

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    同样在您的安全配置中更改安全过滤器intercept-url-pattern 以处理所有请求。这可以在工作后进行调整。

    spring-security.xml

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

    【讨论】:

    • 好的。通过这样做,我确实在网络浏览器中获得了一个用于 GET 请求的登录表单,当我输入用户为“spring”,密码为“spring”时,我得到 404 错误页面,当时网络浏览器中的 URL 是“localhost:8080/SpringWebServiceSecurity/getEmp/1 ”。如果我从我的应用程序中完全删除安全过滤器,则此相同的 URL 有效。是否发生了某种重定向导致此问题。
    • @Sandeep,您可以发布网址的控制器吗?另外,您在更改调度程序 servlet 后是否尝试过此操作,但没有安全过滤器?我怀疑 Spring MVC 的配置可能已关闭。如果你发布你的配置,我可以看看。
    • 嘿凯文,我已经添加了控制器代码和 applicationContext.xml 文件。事实上,我已经上传了应用程序。邮编javaexperience.com/wp-content/uploads/2012/12/…
    • @Sandeep 当您在实施安全之前点击 url,您收到了什么?指定@ResponseBody 时可以返回对象吗?
    • 是的,没有实施安全性,它可以正常工作。
    猜你喜欢
    • 2014-07-29
    • 2014-02-21
    • 2016-03-04
    • 1970-01-01
    • 2014-12-06
    • 2016-04-29
    • 2017-05-27
    • 2016-02-23
    • 1970-01-01
    相关资源
    最近更新 更多