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"
                    }
                }
            });
 
 

        

 

猜你喜欢

转载自blog.csdn.net/ZQQ8015/article/details/86227325
今日推荐