Shiro Ajax请求没有权限返回JSON,没有登录返回JSON
2017-08-17 14:32
330 查看
http://blog.csdn.net/catoop/article/details/69210140?locationNum=14&fps=1
本文基于Shiro权限注解方式来控制Controller方法是否能够访问。
例如使用到注解:
想了解Shiro是如何通过注解来控制权限的,可以查看源码
用户在请求使用这些注解方式控制的方法时,如果没有通过权限校验。Shiro 会抛出如下两组类型的异常。
登录认证类异常 UnauthenticatedException.class, AuthenticationException.class
权限认证类异常 UnauthorizedException.class, AuthorizationException.class
(每个具体的异常对应哪个注解,大家查看源码了解一下)
言归正传,直接上代码,通过代码来说明本文目的 “做Ajax请求的时候,如果请求的URL是被注解权限控制的,在没有权限或者登陆失效的情况下,如果获得JSON方式的返回结果(如果用户没有登录,大多数都是直接跳转到登录页面了)”。
通过一个 BaseController 来统一处理,然后被其他 Controller 继承即可,对于JSON和页面跳转,我们只需要做一个Ajax判断处理即可。
代码如下:
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
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
下面是一个普通的 Controller
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
当我们使用 ajax 方式去请求 /getData 时,如果用户没有登录。则会返回对应没有登录的JSON结果。
页面在做ajax请求时候,发现用户没有登录,可能需要根据响应结果做对用的页面交互处理,而不是暴力的直接重定向到登录页面了
本文基于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, HttpServletResponse response) { if (WebUtilsPro.isAjaxRequest(request)) { // 输出JSON Map<String,Object> map = new HashMap<>(); map.put("code", "-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
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; } } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
当我们使用 ajax 方式去请求 /getData 时,如果用户没有登录。则会返回对应没有登录的JSON结果。
页面在做ajax请求时候,发现用户没有登录,可能需要根据响应结果做对用的页面交互处理,而不是暴力的直接重定向到登录页面了
相关文章推荐
- Shiro Ajax请求没有权限返回JSON,没有登录返回JSON
- shiro修改没有登录或者session失效,根据ajax返回json
- shiro修改没有登录或者session失效,根据ajax返回json
- 解决MOSS2007启动单一登录服务时出现“您没有执行此操作的权限”
- 【原创】属性 IsLocked 不可用于 登录“[sa]”。该对象可能没有此属性,也可能是访问权限不足而无法检索。
- java web 使用fliter定义权限拦截(例如:提交订单,我的订单 没有登录前不允许访问)
- 使用okhttp时没有返回json数据
- windows下登录samba服务器后无法访问linux下共享目录,提示没有权限。
- “你没有权限登录JIRA”的解决办法
- passwd命令读取的shadow文件没有任何权限,为什么普通用户可以正常登录
- web中定义过滤器验证登录(未登录没有权限访问页面)
- controller类中使用json返回错误码,实现用户登录
- 【已解决】接应用宝sdk:在没有装QQ的情况下QQ登录会打开页面提示下载QQ,此时返回就会报错误
- 基于所提供的凭据,您没有权限查看此目录或网页;HTTP 错误 401.2 - 未授权:服务器配置导致登录失败
- java权限拦截,控制当前登录用户访问方法,访问路径,并json提示,驳回请求
- net.sf.json.JSONObject在参数为空时,返回json中没有这个key
- CTP 下单返回错误: 没有报单权限 和字段错误需要注意的问题
- jsp中判断权限,若没有登录,则不能访问
- 远程桌面无法登录:由于帐户限制,无法让你登(访问局域网某台电脑时提示:无法访问,你可能没有权限使用网络资源.的解决办法 !!)
- shiro 登录 过滤器 返回json