Mus*_*eel 8 ajax jquery spring spring-mvc spring-security
我所有的$.ajax,都POST和GET都工作正常,但只要我整合Spring security 3.2.6到我的项目的POSTAjax请求停止,而洛任何问题的工作.
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://www.springframework.org/schema/security"
             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-3.2.xsd">
    <!--Permit all Web resources to bypass proxy-->
    <http pattern="/js/**" security="none"/>
    <http pattern="/css/**" security="none"/>
    <http pattern="/fonts/**" security="none"/>
    <http pattern="/images/**" security="none"/>
    <http auto-config="true" use-expressions="true" >
        <intercept-url pattern="/login" access="isAnonymous()"/>
        <intercept-url pattern="/workflow**" access="hasRole('ROLE_WORKFLOW_ADMIN')"/>
        <intercept-url pattern="/**" access="hasAnyRole('ROLE_ADMIN','ROLE_WORKFLOW_ADMIN','ROLE_DMS_ADMIN')"/>
        <access-denied-handler error-page="/403"/>
        <form-login
                login-page="/login"
                default-target-url="/dashboard"
                authentication-failure-url="/login?error"
                username-parameter="username"
                password-parameter="password"/>
        <logout invalidate-session="true" logout-success-url="/login?logout"/>
        <csrf/>
    </http>
    <!-- Select users and user_roles from database -->
    <authentication-manager>
        <authentication-provider ref="daoAuthenticationProvider"/>
    </authentication-manager>
    <beans:bean id="daoAuthenticationProvider"
                class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="authService"/>
    </beans:bean>
</beans:beans>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <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>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error</location>
    </error-page>
</web-app>
编辑
我试图访问的URL是
春天的安全可能会有所帮助吗?
Mus*_*eel 13
经过三天痛苦的日子,我发现了问题,男孩是愚蠢的.
问题是我csrf在弹簧安全方面启用了保护.这导致我的帖子请求被禁止触发access-denied-handler错误页面,因为我没有将我access-denied-handler的映射到"/403"错误页面,如下所示,我http 403/401被屏蔽了http 404  
<access-denied-handler error-page="/403"/>
所以简而言之
access-denied-handler错误页面映射到有效的URL$ .ajax({method:'POST',url:'/ ajax',data:{"$ {_ csrf.parameterName}":"$ {_ csrf.token}"}});
在实现spring security 4时,还必须特别参考一种情况,一种情况需要表单按钮(而不是“ a href”)。
A>已发送表单发布请求:两个登录/注销按钮中都隐藏了输入类型
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
B>对于AJAX发布请求,在taglib声明后的JSP页面中添加以下内容。
<meta name="_csrf" content="${_csrf.token}" />
<meta name="_csrf_header" content="${_csrf.headerName}" />
还有一些在jsp的“ body”上方,在“准备就绪”的任何地方
<script type="text/javascript">
$(function() {
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");
    $(document).ajaxSend(function(e, xhr, options) {
        xhr.setRequestHeader(header, token);
    });
});
 </script>
它应该工作!如果仍然存在CORS问题,请参阅下面的文章。 http://www.html5rocks.com/zh-CN/tutorials/cors/
| 归档时间: | 
 | 
| 查看次数: | 7321 次 | 
| 最近记录: |