如何预处理Spring中所有控制器的请求?验证CSRF令牌

时间:2012-07-05 01:44:45

标签: spring rest spring-mvc csrf

我正在开发一个Spring-MVC后端(和GWT前端),它提供了一个REST接口。出于安全原因,它应该在每个请求上验证令牌。现在的问题是:如何在不写入应该检查它的每个控制器内部的情况下检查该令牌?我想到一个类,其方法在请求到达负责的控制器之前运行并检查令牌,如果它有效,它会将数据传递给控制器​​。

通过网络发送的JSON数据如下所示:

{
    "data":
    {
        "id":1,
        "firstName":"firstExample",
        "lastName":"lastExample"
    },
    "csrf":"myCSRFToken"
}

我的Spring设置的其余部分如下所示:

的web.xml:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>myapp.server.AppConfig</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

调度-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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="myapp.server.controller" />

    <mvc:annotation-driven />

</beans>

AppConfig.java:

@Configuration
@EnableWebMvc
public class AppConfig {
    //
}

PersonController:

@Controller
@RequestMapping("/persons")
public class PersonController {
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public Collection<Person> list() {
        //
    }
}

所以基本的想法是,我的CSRFCheck.java类将首先接收每​​个请求。它将检查CSRF-Token,并将JSON的“data”部分的内容转发给负责的控制器。所以控制器只会收到:

{
    "id":1,
    "firstName":"firstExample",
    "lastName":"lastExample"
}

我是Spring的新手,我想知道在哪里修改配置,以便按预期工作。

谢谢!


现在正在使用dispatcher-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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="myapp.server.controller" />

<mvc:annotation-driven />

<mvc:interceptors>  
     <bean class="myapp.server.XSRFInterceptor" />
</mvc:interceptors>

</beans>

但为什么不使用这个AppConfig.java?:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="myapp.server.controller") 
public class AppConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new XSRFInterceptor());
    }
}

还有一个问题:如何修改传递给控制器​​的JSON?

解决问题1(xml与java):

调度-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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

</beans>

上面的AppConfig.java

CSRFInterceptor.java:

@Component
public class CSRFInterceptor extends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // Some code

        return true;
    }
}

仍然要解决:我如何处理拦截器内的JSON数据?

0 个答案:

没有答案