Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 检测ajax调用上的身份验证异常_Jquery_Ajax_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Jquery 检测ajax调用上的身份验证异常

Jquery 检测ajax调用上的身份验证异常,jquery,ajax,spring,spring-mvc,spring-security,Jquery,Ajax,Spring,Spring Mvc,Spring Security,我正在用SpringMVC和SpringSecurity开发一个应用程序 我在视图层中使用这个小脚本,以便用从控制器返回的html数据填充页面中的div: $.ajax({ type : 'GET', url : $("#"+ event.args.element.id+ "Url").val(), dataType : 'html', success : function(data){ $("#ContentPanel").html(data);

我正在用SpringMVC和SpringSecurity开发一个应用程序

我在视图层中使用这个小脚本,以便用从控制器返回的html数据填充页面中的
div

$.ajax({
    type : 'GET',
    url : $("#"+ event.args.element.id+ "Url").val(),
    dataType : 'html',
    success : function(data){
        $("#ContentPanel").html(data);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert("Error!!!");
    }
});
但是,如果中的用户未经过身份验证(例如,由于会话过期),则会从登录页面用html填充div。如何避免它,并让spring将此类错误信息提供给视图中的脚本

以下是我的spring安全配置:

<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http auto-config="true" use-expressions="true">
    <session-management>
        <concurrency-control max-sessions="1" />
    </session-management>

    <form-login 
        login-page="/login" 
        login-processing-url="/resources/j_spring_security_check"  
        authentication-failure-url="/login?login_error=t"/>
    <logout logout-url="/resources/j_spring_security_logout"/>

    <intercept-url pattern="/resources/**" access="permitAll" />
    <intercept-url pattern="/login" access="permitAll" />

    <intercept-url pattern="/url1**" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/url2/**" access="hasRole('ROLE_ADMIN')" />

    <intercept-url pattern="/**" access="isAuthenticated()" />
</http>

<beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

<beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

<beans:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <beans:constructor-arg>
        <beans:ref bean="authenticationProvider"/>
    </beans:constructor-arg>
</beans:bean>

<beans:bean id="userDetailsService" class="it.cpmapave.fgas.aziende.service.jpa.UserDetailsServiceImpl" />

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder hash="sha-256"/>
    </authentication-provider>
</authentication-manager>



<global-method-security pre-post-annotations="enabled" />


谢谢你的提示

在错误授权Spring返回403资源不可访问后,您需要检测401或404

       $.ajax({
       type : "GET",
       url : url,
       statusCode : {

       200 : function() {
          alert("Successfull access.");
       },
       401 : function() {
          alert("Unauthorised access.");
       },

       403 : function() {
          alert("Forbidden resouce can't be accessed");
       }
       });

       };
在任何情况下,如果您在Spring security上定义了一个表单,那么您可以告诉表单在发生登录失败时必须转到哪个页面

              <form-login
                login-processing-url="/doLogin.do"
                login-page="/login.do"
                username-parameter="username"
                password-parameter="password"
                default-target-url="/foo.do"
                authentication-failure-handler-ref="simpleUrlAuthenticationFailureHandler"
            />


将spring security配置为发送重定向状态代码303,检查ajax调用中的状态代码并采取适当的操作。感谢您的回答@paul,但它不起作用。我已经用更多的细节更新了我的问题。。