本文介绍了春天CSRF + AngularJs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过许多aswers的这个话题,没有人对我的作品。

I Already tried many of the aswers about this topic, no one works for me.

我和春天MVC 4.1.7,春季安全3.2.3在MySQL + Tomcat7工作。

I Have a basic CRUD with Spring MVC 4.1.7, Spring Security 3.2.3 working on MySQL + Tomcat7.

问题是,当我尝试后与AngularJS形式,我一直被拦截的403错误(拒绝访问)。

The problem is, when i try to POST form with AngularJS, I keep being blocked by error 403 ( access denied ).

我想通了,我需要把我的CSRF_TOKEN与POST请求,但我无法弄清楚如何!

I figured out that I need to send my CSRF_TOKEN with the POST request, but I can't figure out HOW!

我试过很多方法diferent,没有人的作品。

I Tried so many diferent ways and no one works.

我的文件

Controller.js

Controller.js

$scope.novo = function novo() {
  if($scope.id){
   alert("Update - " + $scope.id);
  }
  else{
      var Obj = {
              descricao : 'Test',
              saldo_inicial : 0.00,
              saldo : 33.45,
              aberto : false,
              usuario_id : null,
              ativo : true
      };
      $http.post(urlBase + 'caixas/adicionar', Obj).success(function(data) {
          $scope.caixas = data;
      }).error(function(data) {alert(data)});
  }
 };

弹簧security.xml文件

Spring-security.xml

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

<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/seguro**"
        access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />
    <intercept-url pattern="/seguro/financeiro**"
        access="hasAnyRole('ROLE_FINANCEIRO','ROLE_ADMIN')" />

    <!-- access denied page -->
    <access-denied-handler error-page="/negado" />
    <form-login login-page="/home/" default-target-url="/seguro/"
        authentication-failure-url="/home?error" username-parameter="inputEmail"
        password-parameter="inputPassword" />
    <logout logout-success-url="/home?logout" />
    <!-- enable csrf protection -->
    <csrf />
</http>

<!-- Select users and user_roles from database -->
<authentication-manager>
    <authentication-provider>
        <password-encoder hash="md5" />
        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="SELECT login, senha, ativo
               FROM usuarios
              WHERE login = ?"
            authorities-by-username-query="SELECT u.login, r.role
               FROM usuarios_roles r, usuarios u
              WHERE u.id = r.usuario_id
                AND u.login = ?" />
    </authentication-provider>
</authentication-manager>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Barattie ~ Soluções Integradas</display-name>

<!-- The definition of the Root Spring Container shared by all Servlets
    and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/spring-security.xml
        /WEB-INF/spring/spring-database.xml
        /WEB-INF/spring/spring-hibernate.xml
    </param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.writeStateAtFormEnd</param-name>
    <param-value>false</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/home</url-pattern>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 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>

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/erro</location>
</error-page>

更新

我尝试添加在客户端的XSRF重命名,但我不断收到访问被拒绝。

I tried to add to rename at client side the xsrf, but I keep getting access denied.

var app = angular.module('myApp', []).config(function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = '_csrf';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
});

**更新2 **

** UPDATE 2 **

我试图实现一个过滤器是这样的。

I tried to implement a filter like this.

package sys.barattie.util;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.filter.OncePerRequestFilter;

public class CsrfFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
    if (csrf != null) {
        Cookie cookie = new Cookie("XSRF-TOKEN", csrf.getToken());
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    filterChain.doFilter(request, response);
}
}

和改变了我的春天的安全吧。

And changed my spring security to it.

     <csrf token-repository-ref="csrfTokenRepository" />
<beans:bean id="csrfTokenRepository" class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository">
    <beans:property name="headerName" value="X-XSRF-TOKEN" />
</beans:bean>

在web.xml

Web.xml

<filter>
    <filter-name>csrfFilter</filter-name>
    <filter-class>sys.barattie.util.CsrfFilter</filter-class>
</filter>

但就是这样的服务器不运行过滤器,我添加了一个的System.out.println它,但不能看到调试中的消息

But it is like that the server doesn't run the filter, I've added a System.out.println to it, but can't see the messages in the debug

推荐答案

我做了一些测试,并与该结束了。

I did some tests and end up with this.

在我登录控制器,如果我的用户认证成功,我创建一个AngularJS令牌的名称的标记,就像这样。

In my login controller, if my user authentication is successful, I create a token with the name of the AngularJS Token, just like this.

CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
    Cookie cookie = new Cookie("XSRF-TOKEN", csrf.getToken());
    cookie.setPath("/");
    response.addCookie(cookie);
}

在这之后,我可以成功地管理我的$ http.post。

After that, I can manage my $http.post successfully.

我不知道这是不是最好的办法,但为我工作的方式!

I don't know if this is the best way, but is the way that worked for me!

感谢您的帮助@Joao杰利斯塔

Thanks for the help @Joao Evangelista

这篇关于春天CSRF + AngularJs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:13