权限控制-Shiro2

首先看先自定义的Realm:

  1. /**

  2. * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.

  3. */

  4. @Override

  5. protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

  6. SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

  7. //获取用的id

  8. String userId = ((User)principals.getPrimaryPrincipal()).getUserId();

  9. //查询对应用户的角色集合

    扫描二维码关注公众号,回复: 2729010 查看本文章
  10. List<RoleR> roleList=userService.getRoleList(userId);

  11. List<Menu> menuList=null;

  12. List<String> roleAllList = new ArrayList<String>();

  13. List<String> resourceList = new ArrayList<String>();

  14. for (RoleR role : roleList) {

  15. roleAllList.add(role.getRoleId()+"");

  16. //查询对应角色的对应权限集合

  17. menuList=userService.getMenuList(role.getRoleId());

  18. for (Menu menu : menuList) {

  19. if(StringUtils.isNotBlank(menu.getPermission())){

  20. resourceList.add(menu.getPermission());

  21. }

  22. }

  23. }

  24. //赋角色

  25. info.addRoles(roleAllList);

  26. //赋权限

  27. info.addStringPermissions(resourceList);

  28. return info;

  29. }

可能都见过这个方法,但是你们知道什么时候调用吗?

我来揭秘:

  1. @RequestMapping("getProductList")

  2. @ResponseBody

  3. @RequiresPermissions("product:list")//这是是核心

  4. public String getProductList(Integer offset,Integer limit,Product product){

  5. page.setStrat(offset);

  6. page.setPagecount(limit);

  7. page.setObj(product);

  8. productService.getProductList(page);

  9. Map map=new HashMap();

  10. map.put("total", page.getPagesumcount());

  11. map.put("rows", page.getList());

  12. Gson gson=new Gson();

  13. String str=gson.toJson(map);

  14. return str;

  15. }

只要你访问后台的方法上面有

@RequiresPermissions

这个注解,那么此时shiro回去访问

doGetAuthorizationInfo

这个方法,然后回去验证当前用户是否有次权限,如果没有就回抛会授权异常

但是用户是看不到的,怎么办?这时候就要用另外一个方法,进行全局异常捕获。

没错就是用
@ControllerAdvice和@ExceptionHandler(value={UnauthorizedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)这些注解结合使用。用来捕获所有控制层抛来的制定异常

然后在转到制定的错误提示页面提示用户相关错误信息,如果你们用了aop拦截了controller并且是环绕通知,这时候有个坑,是捕获不到错误的。

  1. /**

  2. * 没有权限 异常

  3. * <p/>

  4. * 后续根据不同的需求定制即可

  5. */

  6. @ExceptionHandler(value={UnauthorizedException.class})

  7. @ResponseStatus(HttpStatus.UNAUTHORIZED)

  8. public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e){

  9. System.out.println("-----------------------------------------------------");

  10. ModelAndView mv = new ModelAndView();

  11. mv.addObject("errorInfo", e);

  12. mv.setViewName("unauthorized");

  13. return mv;

  14. }


 

原因因为aop拦截后抛出了更大的异常,而你捕获的是未授权,所以要重新抛出未授权

不仅仅是权限控制,也可以角色控制,一样的用法

接下来就是前台js的权限控制

  1.  
  2. <div id="toolbar" class="btn-group">

  3. <shiro:hasPermission name="product:insert">

  4. <button id="btn_add" type="button" class="btn btn-primary" onclick="addProduct()">

  5. <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增

  6. </button>

  7. </shiro:hasPermission>

  8. <shiro:hasPermission name="product:deletes">

  9. <button id="btn_delete" type="button" class="btn btn-warning" onclick="delProductAll()">

  10. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除

  11. </button>

  12. </shiro:hasPermission>

  13. <shiro:hasPermission name="product:insert">

  14. <button id="btn_delete" type="button" class="btn btn-success" onclick="updateAllProduct()">

  15. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量修改图片

  16. </button>

  17. </shiro:hasPermission>

  18. <shiro:hasPermission name="product:excel">

  19. <button id="btn_delete" type="button" class="btn btn-success" onclick="exportExcel()">

  20. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>导出Excel

  21. </button>

  22. </shiro:hasPermission>

  23. <shiro:hasPermission name="product:xml">

  24. <button id="btn_delete" type="button" class="btn btn-success" onclick="exportXml()">

  25. <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>导出Xml

  26. </button>

  27. </shiro:hasPermission>

  28. </div>


如果当前用户符合这些权限,按钮就可以显示,前天要引入shiro标签库

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> 

下面是我的权限表的大致结构:

           下载地址:http://download.csdn.net/download/qq_38665235/9999509

这是Realm的下载地址:

           下载地址:http://download.csdn.net/download/qq_38665235/9999496

转自https://blog.csdn.net/qq_38665235/article/details/78122298

猜你喜欢

转载自blog.csdn.net/sanyaoxu_2/article/details/81583258