加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Shiro Ajax请求没有权限返回JSON,没有登录返回JSON

发布时间:2020-12-16 03:10:09 所属栏目:百科 来源:网络整理
导读:转自:https://blog.csdn.net/catoop/article/details/69210140 本文基于Shiro权限注解方式来控制Controller方法是否能够访问。 例如使用到注解: @RequiresPermissions 来控制是否有对应权限才可以访问 @RequiresUser 来控制是否存在用户登录状态才可以访问

转自:https://blog.csdn.net/catoop/article/details/69210140

本文基于Shiro权限注解方式来控制Controller方法是否能够访问。
例如使用到注解:
@RequiresPermissions来控制是否有对应权限才可以访问
@RequiresUser来控制是否存在用户登录状态才可以访问

想了解Shiro是如何通过注解来控制权限的,可以查看源码AopAllianceAnnotationsAuthorizingMethodInterceptor,其构造方法中添加了几个对应的权限注解方法拦截器(这里不做详细阐述)。

用户在请求使用这些注解方式控制的方法时,如果没有通过权限校验。Shiro 会抛出如下两组类型的异常。

登录认证类异常 UnauthenticatedException.class,AuthenticationException.class
权限认证类异常 UnauthorizedException.class,AuthorizationException.class
(每个具体的异常对应哪个注解,大家查看源码了解一下)

言归正传,直接上代码,通过代码来说明本文目的 “做Ajax请求的时候,如果请求的URL是被注解权限控制的,在没有权限或者登陆失效的情况下,如果获得JSON方式的返回结果(如果用户没有登录,大多数都是直接跳转到登录页面了)”。

通过一个 BaseController 来统一处理,然后被其他 Controller 继承即可,对于JSON和页面跳转,我们只需要做一个Ajax判断处理即可。

代码如下:

/** * BaseController * * @author 单红宇(365384722) * @myblog http://blog.csdn.net/catoop/ * @create 2017年4月4日 */
public abstract class BaseController {

    /** * 登录认证异常 */
    @ExceptionHandler({ UnauthenticatedException.class,AuthenticationException.class })
    public String authenticationException(HttpServletRequest request,HttpServletResponse response) {
        if (WebUtilsPro.isAjaxRequest(request)) {
            // 输出JSON
            Map<String,Object> map = new HashMap<>();
            map.put("code","-999");
            map.put("message","未登录");
            writeJson(map,response);
            return null;
        } else {
            return "redirect:/system/login";
        }
    }

    /** * 权限异常 */
    @ExceptionHandler({ UnauthorizedException.class,AuthorizationException.class })
    public String authorizationException(HttpServletRequest request,"-998");
            map.put("message","无权限");
            writeJson(map,response);
            return null;
        } else {
            return "redirect:/system/403";
        }
    }

    /** * 输出JSON * * @param response * @author SHANHY * @create 2017年4月4日 */
    private void writeJson(Map<String,Object> map,HttpServletResponse response) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            out = response.getWriter();
            out.write(JsonUtil.mapToJson(map));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
  • 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
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
public class WebUtilsPro {

    /** * 是否是Ajax请求 * * @param request * @return * @author SHANHY * @create 2017年4月4日 */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
            return true;
        } else {
            return false;
        }
    }
}
  • 19

下面是一个普通的 Controller

@Controller
@RequestMapping
public class PageController extends BaseController{

    @RequiresUser
    @RequestMapping(value="/main",method=RequestMethod.GET)
    public String main(Model model){
        return "main";
    }

    @RequiresUser
    @RequestMapping(value="/getData",method=RequestMethod.POST)
    @ResponseBody
    public List<String> getData(Model model){
        List<String> list = new ArrayList<>();
        list.add("data1");
        list.add("data2");
        return list;
    }

}
  • 21

当我们使用 ajax 方式去请求 /getData 时,如果用户没有登录。则会返回对应没有登录的JSON结果。页面在做ajax请求时候,发现用户没有登录,可能需要根据响应结果做对用的页面交互处理,而不是暴力的直接重定向到登录页面了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读