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

       首先shiro默认的过滤器有哪些?

      

名称类名
anonorg.apache.shiro.web.filter.authc.AnonymousFilter
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logoutorg.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreationorg.apache.shiro.web.filter.session.NoSessionCreationFilter
permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
portorg.apache.shiro.web.filter.authz.PortFilter
restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter
sslorg.apache.shiro.web.filter.authz.SslFilter
userorg.apache.shiro.web.filter.authc.UserFilter

     上面的东西是百度的。例如

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login/login.htm" />
<property name="successUrl" value="/index/index.htm" />
<property name="unauthorizedUrl" value="/index/index.htm" />
<property name="filters">
<map>


<entry key="authc">
<bean class="com.zhihai.shirorealm.ShiroAccess" />
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/ = anon
/www.htm=anon
/register/** = anon
/login/** = anon
/index/** = authc
/analysis/** = authc
</value>
</property>
</bean>

那个filterChainDefinitions后面对应的链接后面的anon还是authc都是去filters中对应的key中去找,找不到就是默认的,我们相关的权限是authc,所以我们filters中要有这个key,我们要修改返回值就要修改authc对应的默认类org.apache.shiro.web.filter.authc.FormAuthenticationFilter,我们需要重写这个类,ShiroAccess就是对这个类的重写。

我们要写返回值我们需要重写该类的    

  @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception

    我们对该方法的重写如下:

@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 {
            if(isAjax(request)){
                Map<String,Object> result=new HashMap<String,Object>();
                result.put("isLogin",false);
                response.getWriter().print(JsonUtils.Bean2Json(result));
            }else{
                this.saveRequestAndRedirectToLogin(request, response);
            }
            return false;
        }
    }


    public static boolean isAjax(ServletRequest request){
        String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
        if("XMLHttpRequest".equalsIgnoreCase(header)){
            System.out.println( "当前请求为Ajax请求");
            return Boolean.TRUE;
        }
        System.out.println(  "当前请求非Ajax请求");
        return Boolean.FALSE;
    }

这样就可以搞定了。还需努力。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值