shiro修改没有登录或者session失效,根据ajax返回json

        当使用shiro框架,进行权限控制时,没有登录或者session失效,进行ajax请求时,不会跳转会登录页面,仅仅不返回正确结果。

因此,需要解决对ajax请求进行特殊处理。

        1、重新FormAuthenticationFilter类onAccessDenied方法。让其根据不同的请假方式,返回不同的结果。

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;

public class ShiroFormAuthenticationFilter extends FormAuthenticationFilter {
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        if(this.isLoginRequest(request, response)) {
            if(this.isLoginSubmission(request, response)) {
                return this.executeLogin(request, response);
            } else {
                return true;
            }
        } else {
            String header = ((HttpServletRequest) request).getHeader("Content-Type");
            if(header != null && header.equals("application/x-www-form-urlencoded")){
                response.setCharacterEncoding("UTF-8");
                PrintWriter out = response.getWriter();
                out.print("{\"isOver\" : true }");
                out.flush();
                out.close();
            }else{
                this.saveRequestAndRedirectToLogin(request, response);
            }
            return false;
        }
    }

}
       2、修改shiro配置文件,添加拦截器。

 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"></property>
        <property name="loginUrl" value="/"></property>
        <property name="unauthorizedUrl" value="/"></property>
        <property name="filters">
            <map>
                <entry key="authc">
                    <bean class="zteict.qinhuangdao.framework.common.shiro.shiro.ShiroFormAuthenticationFilter"></bean>
                </entry>
            </map>
        </property>
        <!-- 授权配置 -->
        <property name="filterChainDefinitions">
            <value>
                <!--与用户登录有关的权限-->
                /components/** = anon
                /css/** = anon
                /images/** = anon
                /lib/** = anon
                /LoginController.js = anon
                /loginController/login.do = anon
                <!--权限授权-->
                /** = authc
            </value>
        </property>
    </bean>
        3、前台页面统一处理ajax请求,这里系统中使用的是angularjs,分装post请求service,前台使用其他js框架的,请自行修改。

        /**
         * 封装post请求
         * @param url
         * @param data
         * @param callback
         */
        var commPost = function (url,data,callback) {
            $http({
                method:'POST',
                url:baseUrl+url,
                data : data
            }).then(function(result) {
                    if(result.data.isOver != undefined && result.data.isOver){
                        //跳转页面
                        window.location.href = baseUrl+"pages/common/login/login.html";
                    }else{
                        callback(result.data);
                    }
            });
        };




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值