转自: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@H_301_33@ 单红宇(365384722) * @myblog@H_301_33@ http://blog.csdn.net/catoop/ * @create@H_301_33@ 2017年4月4日 */@H_301_33@
public@H_301_33@ abstract@H_301_33@ class@H_301_33@ BaseController@H_301_33@ {@H_301_33@
/** * 登录认证异常 */@H_301_33@
@ExceptionHandler@H_301_33@({ UnauthenticatedException.class,AuthenticationException.class })
public@H_301_33@ String authenticationException@H_301_33@(HttpServletRequest request,HttpServletResponse response) {
if@H_301_33@ (WebUtilsPro.isAjaxRequest(request)) {
// 输出JSON@H_301_33@
Map<String,Object> map = new@H_301_33@ HashMap<>();
map.put("code"@H_301_33@,"-999"@H_301_33@);
map.put("message"@H_301_33@,"未登录"@H_301_33@);
writeJson(map,response);
return@H_301_33@ null@H_301_33@;
} else@H_301_33@ {
return@H_301_33@ "redirect:/system/login"@H_301_33@;
}
}
/** * 权限异常 */@H_301_33@
@ExceptionHandler@H_301_33@({ UnauthorizedException.class,AuthorizationException.class })
public@H_301_33@ String authorizationException@H_301_33@(HttpServletRequest request,"-998"@H_301_33@);
map.put("message"@H_301_33@,"无权限"@H_301_33@);
writeJson(map,response);
return@H_301_33@ null@H_301_33@;
} else@H_301_33@ {
return@H_301_33@ "redirect:/system/403"@H_301_33@;
}
}
/** * 输出JSON * * @param@H_301_33@ response * @author@H_301_33@ SHANHY * @create@H_301_33@ 2017年4月4日 */@H_301_33@
private@H_301_33@ void@H_301_33@ writeJson@H_301_33@(Map<String,Object> map,HttpServletResponse response) {
PrintWriter out = null@H_301_33@;
try@H_301_33@ {
response.setCharacterEncoding("UTF-8"@H_301_33@);
response.setContentType("application/json; charset=utf-8"@H_301_33@);
out = response.getWriter();
out.write(JsonUtil.mapToJson(map));
} catch@H_301_33@ (IOException e) {
e.printStackTrace();
} finally@H_301_33@ {
if@H_301_33@ (out != null@H_301_33@) {
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@H_301_33@ class@H_301_33@ WebUtilsPro@H_301_33@ {@H_301_33@
/** * 是否是Ajax请求 * * @param@H_301_33@ request * @return@H_301_33@ * @author@H_301_33@ SHANHY * @create@H_301_33@ 2017年4月4日 */@H_301_33@
public@H_301_33@ static@H_301_33@ boolean@H_301_33@ isAjaxRequest@H_301_33@(HttpServletRequest request) {
String requestedWith = request.getHeader("x-requested-with"@H_301_33@);
if@H_301_33@ (requestedWith != null@H_301_33@ && requestedWith.equalsIgnoreCase("XMLHttpRequest"@H_301_33@)) {
return@H_301_33@ true@H_301_33@;
} else@H_301_33@ {
return@H_301_33@ false@H_301_33@;
}
}
}
- 19
下面是一个普通的 Controller
@Controller@H_301_33@
@RequestMapping@H_301_33@
public@H_301_33@ class@H_301_33@ PageController@H_301_33@ extends@H_301_33@ BaseController@H_301_33@{@H_301_33@
@RequiresUser@H_301_33@
@RequestMapping@H_301_33@(value="/main"@H_301_33@,method=RequestMethod.GET)
public@H_301_33@ String main@H_301_33@(Model model){
return@H_301_33@ "main"@H_301_33@;
}
@RequiresUser@H_301_33@
@RequestMapping@H_301_33@(value="/getData"@H_301_33@,method=RequestMethod.POST)
@ResponseBody@H_301_33@
public@H_301_33@ List<String> getData@H_301_33@(Model model){
List<String> list = new@H_301_33@ ArrayList<>();
list.add("data1"@H_301_33@);
list.add("data2"@H_301_33@);
return@H_301_33@ list;
}
}
- 21
当我们使用 ajax 方式去请求 /getData 时,如果用户没有登录。则会返回对应没有登录的JSON结果。页面在做ajax请求时候,发现用户没有登录,可能需要根据响应结果做对用的页面交互处理,而不是暴力的直接重定向到登录页面了。