Shiro - Web.Maven项目使用Shiro进行登录授权

当我们在控制器方法写了 @RequiresPermissions

Shiro在访问时, 就会判断有没有该权限,如果没有,就不会执行对应方法

 

实现过程

1.创建角色、权限表及它们之间的关系中间表

角色:

权限:

关系:

2.在配置文件当中添加Shiro注解扫描

<!--
    配置为true即使用cglib继承的方式,
    false为jdk的接口动态代理   控制器没有实现接口
-->
<aop:config proxy-target-class="true" ></aop:config>

<!-- 使用第三方去扫描shiro的注解 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor ">
    <property name="securityManager" ref="securityManager"></property>
</bean>

3.在Realm中添加授权信息

// 授权
// 1.发现访问路径的方法上面有授权的注解,就会调用这个方法
// 2.页面当中有授权的标签,也会调用这个方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    // 获取用户的身份信息
    Employee employee = (Employee) principalCollection.getPrimaryPrincipal();
    // 根据当前角色查询权限
    List<String> roles = new ArrayList<>();
    List<String> permissions = new ArrayList<>();
    // 判断当前用是不是管理员,如果是管理员,拥有所有权限
    if(employee.getAdmin()){
        // 拥有所有权限
        permissions.add("*:*");
    }else{
        // 查询角色
        roles = employeeService.getRolesById(employee.getId());
        // 查询权限
        permissions = employeeService.getPermissionById(employee.getId());
    }
    // 给授权信息
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRoles(roles);
    info.addStringPermissions(permissions);
    return info;
}

 

没有权限处理方法

1.请求

@RequestMapping("/employee")
@RequiresPermissions("employee:index")
public String employee(){
    return "employee";
}

2.如果没有权限,在Controller里添加方法

@ExceptionHandler(AuthorizationException.class)
public void handleShiroException(HandlerMethod method,HttpServletResponse response) throws Exception{ // method  发生异常的方法
    // 跳转到一个界面界面提示没有权限
    // 判断当前的请求是不是Json请求,如果是 返回json给浏览器,让它自己来做跳转
    // 因为Ajax请求没有办法跳转服务当中的请求,只能通过浏览器当中来跳转
    // 获取方法上的注解,有ResponseBody注解就是Ajax请求
    ResponseBody methodAnnotation = method.getMethodAnnotation(ResponseBody.class);
    if (methodAnnotation != null){
        //Ajax
        AjaxRes ajaxRes = new AjaxRes();
        ajaxRes.setSuccess(false);
        ajaxRes.setMsg("你没有权限操作");
        String s = new ObjectMapper().writeValueAsString(ajaxRes);
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(s);
    }else {
        // 重定向
        response.sendRedirect("nopermission.jsp");
    }
}

页面一些按钮权限处理

①在页面引入Shiro标签库(HTML页面可以通过thymeleaf来引入)

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

②在需要权限控制的地方添加对应的shiro标签

<shrio:hasPermission name="employee:add">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" id="add">添加</a>
</shrio:hasPermission>
<shrio:hasPermission name="employee:edit">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" id="edit">编辑</a>
</shrio:hasPermission>
<shrio:hasPermission name="employee:delete">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" id="delete">离职</a>
</shrio:hasPermission>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值