1. 程式人生 > >Ajax非同步請求,頁面不跳轉問題的解決

Ajax非同步請求,頁面不跳轉問題的解決

         背景:在進行ssm整和shiro時有一個許可權不足時跳轉到許可權不足頁面的需求。前端是easyUI的dataGrid表格傳送了一個Ajax請求,到達後端之後這個請求被perms攔截器攔截,許可權校驗未通過,於是要向/webApp/unauthorized.jsp這個頁面跳轉,結果頁面沒有變化。查詢控制檯發現,資料請求的請求302重定向到unauthorized.jsp這個頁面,這個頁面返回200,請求成功,響應也成功,就是沒有跳轉頁面。糾結了一天。

        最後發現Ajax請求是頁面不重新整理,不支援頁面的轉發和重定向。網上搜了半天的的解決方案,大概都是這個思路:後臺給前臺傳送一個前臺回撥函式需要的json資料,在json資料中新增跳轉資訊和你前臺跳轉需要的資訊。最後在前臺請求的回撥函式中進行頁面的重定向。

1.applicationContext-shrio.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<!--註冊realm-->
	<bean id="bosRealm" class="com.jujung.bos.realm.BosReam"></bean>

	<!--安全管理器-->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="bosRealm"></property>
	</bean>

	<!--註冊退出過濾器-->
	<bean id="logoutFilter" class="org.apache.shiro.web.filter.authc.LogoutFilter">
		<property name="redirectUrl" value="/unauthorized.jsp"/>
	</bean>

	<!--配置shiro的過濾器工廠(id必須和web.xml檔案中配置的filter的名字一樣)-->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!--注入安全管理器-->
		<property name="securityManager" ref="securityManager"></property>
		<!--登入頁面的url-->
		<property name="loginUrl" value="/login.jsp"></property>
		<!--成功頁面的url-->
		<property name="successUrl" value="/index.jsp"></property>
		<!--許可權不足時請求的url-->
		<property name="unauthorizedUrl" value="/unauthorized.action"></property>
		<!--注入退出過濾器-->
		<property name="filters">
			<map>
				<entry key="logout" value-ref="logoutFilter" />
			</map>
		</property>
		<!--注入url攔截規則-->
		<property name="filterChainDefinitions">
			<value>
				/css/** = anon
				/js/** = anon
				/images/** = anon
				/validatecode.jsp* = anon
				/login.jsp = anon
				/loginController/login.action = anon
				/loginController/logout.action = logout
				/staffController/list.action = perms["staff-list"]
				/* = authc
			</value>
		</property>
	</bean>
</beans>

2.在Controller層專門寫了一個handler來處理許可權不足時請求的url

  @RequestMapping("/unauthorized.action")
    @ResponseBody
    public Map<String, String> unauthorized(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //改變響應碼,為了進入datagrid的請求失敗回撥函式
        response.setStatus(302);
        //添加回調函式需要的資訊,以json形式響應
        Map<String, String> result = new HashMap<>();
        result.put("unauthorized", "unauthorized");
        return result;
    }

  3.我的這個請求來自於EasyUI的datagrid,所以我在後臺響應重定向,就會進入前臺的onLoadError()函式,在這個函式中完成跳轉。

 // 取派員資訊表格
            $('#grid').datagrid({
                iconCls: 'icon-forward',
                fit: true,
                border: false,
                rownumbers: true,
                striped: true,
                pageList: [10, 30, 50],
                pagination: true,
                toolbar: toolbar,
                url: "${pageContext.request.contextPath}/staffController/list.action",
                idField: 'id',
                columns: columns,
                onDblClickRow: doDblClickRow,
                /*許可權不足時跳轉頁面*/
                onLoadError: function(data){
                    //json串轉化為js物件
                    var result = eval("(" + data.responseText + ")");
                    //console.log(result)
                    if(result.unauthorized == "unauthorized"){
                        window.location.href = "/unauthorized.jsp"
                    }
                }
            });