【问题标题】:Detect Authentication exception on ajax call检测 ajax 调用的身份验证异常
【发布时间】:2014-09-15 12:33:32
【问题描述】:

我正在开发一个带有 spring-mvc 和 spring security 的应用程序。

我在视图层中使用了这个小脚本,以便在我的页面中使用从我的控制器返回的 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!!!");
    }
});

但如果用户未通过身份验证(例如由于会话到期),则 div 将使用登录页面中的 html 填充。怎么避免,让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 security发送重定向状态码303,检查ajax调用中的状态码并采取适当的措施。

标签: jquery ajax spring spring-mvc spring-security


【解决方案1】:

授权错误后需要检测 401 或 404 Spring 返回 403 resource not accesible

       $.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 安全性上定义了一个表单,您可以告诉该表单以防万一或登录失败时必须转到哪个页面。

              <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"
            />

【讨论】:

  • 感谢您的回答@paul,但它不起作用。我已经用更多细节更新了我的问题..
猜你喜欢
  • 2014-12-31
  • 2018-12-10
  • 2012-01-07
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
相关资源
最近更新 更多