【发布时间】: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