转载地址:https://blog.csdn.net/u012138272/article/details/79840275
Shiro 1.4.0
在写后端的时候加入了Shiro做登录认证和会话超时管理,前端页面访问重定向没有问题,但是Ajax访问接口时,如果会话超时,则只会返回一个页面,但是前端页面无法跳转。所以需要返回状态为403的Json数据,然后让前端去跳转。
正文
代码中设置的是所有地址都要经过authc过滤器
//<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
filterChainDefinitionMap.put("/**", "authc");
- 1
- 2
所以需要自定义一下authc过滤器,很多博客都没有写不同名称所对应的过滤器,这让人看得云里雾里,这里列举一下
/**
* @Shiro内置过滤器
* anon org.apache.shiro.web.filter.authc.AnonymousFilter
* authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
* authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
* perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
* port org.apache.shiro.web.filter.authz.PortFilter
* rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
* roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
* ssl org.apache.shiro.web.filter.authz.SslFilter
* user org.apache.shiro.web.filter.authc.UserFilter
*
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
所以我们要改造过滤器就自己写一个类继承FormAuthenticationFilter,大致原理就是判断是否为Ajax,如果是Ajax就返回Json,如果不是就直接重定向。(加上import否则有些跟我一样的新手小白会比较迷茫>_<)
import com.alibaba.fastjson.JSONObject;
import com.pojo.ResultData;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ShiroLoginFilter extends FormAuthenticationFilter {
/**
* 在访问controller前判断是否登录,返回json,不进行重定向。
* @param request
* @param response
* @return true-继续往下执行,false-该filter过滤器已经处理,不继续执行其他过滤器
* @throws Exception
*/
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (isAjax(request)) {
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json");
ResultData resultData = new ResultData();
resultData.setResult(1);
resultData.setCode(403);
resultData.setMessage("登录认证失效,请重新登录!");
httpServletResponse.getWriter().write(JSONObject.toJSON(resultData).toString());
} else {
//saveRequestAndRedirectToLogin(request, response);
/**
* @Mark 非ajax请求重定向为登录页面
*/
httpServletResponse.sendRedirect("/login");
}
return false;
}
private boolean isAjax(ServletRequest request){
String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
if("XMLHttpRequest".equalsIgnoreCase(header)){
return Boolean.TRUE;
}
return Boolean.FALSE;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
这里有一个需要注意的地方,因为过滤器里已经写了重定向的方法了,所以ShiroConfig不需要设置LoginUrl,否则如果没有登录或者会话超时,返回登录页面就提示重定向多次并且无法打开登录页
/**
* @Mark shiroFilterFactoryBean.setLoginUrl("/login");
* @authc 重写了FormAuthenticationFilter过滤器,不需要再写setLoginUrl,否则会发生多次重定向
*/
// 登录成功后要跳转的链接
shiroFilterFactoryBean.setSuccessUrl("/index");