四时宝库

程序员的知识宝库

shrio权限脚手架(脚手架下载)

本文记录下shrio提供的前后端权限脚手架,主要用于资源(controller、method、菜单、按钮、label)等的权限控制。

后端实现

后端通过shrio提供的注解RequiresRoles、RequiresPermissions、RequiresAuthentication、RequiresUser、RequiresGuest实现controller、方法级的权限控制。

  • RequiresRoles

可以用在Controller或者方法上。可以多个roles,多个roles时默认逻辑为 AND 也就是所有具备所有role才能访问。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresRoles {
    String[] value();
    Logical logical() default Logical.AND; 
}

实例

/**
     * 重启接口脚本
     * @return
     */
    @RequestMapping(value = "restart")
    @RequiresRoles(value = "system")
    public String  restartInterface(HttpServletResponse response) {}

该方法只允许system角色的用户调用,其他用户返回403无权限调用。

  • RequiresPermissions

该注解明确要求所需要的权限描述符,可以用在Controller或者方法上。可以多个资源描述符,默认逻辑为 AND 也就是所有具备所有资源描述符才能访问。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresPermissions {

    /**
     * The permission string which will be passed to {@link org.apache.shiro.subject.Subject#isPermitted(String)}
     * to determine if the user is allowed to invoke the code protected by this annotation.
     */
    String[] value();

    /**
     * The logical operation for the permission checks in case multiple roles are specified. AND is the default
     * @since 1.1.0
     */
    Logical logical() default Logical.AND; 

}

实例

 /**
     * 运维界面
     * @param response
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = {"list"})
    @RequiresPermissions(value = {"business:operation:maintenance:list"})
    public String findList(HttpServletResponse response, HttpServletRequest request, Model model) {
        List<ProcessBean> processBeanList = queryProcessList();
        model.addAttribute("processList", processBeanList);
        return "modules/PreiousMetal/operationMaintenace/processList";
    }
  • RequiresAuthentication

该注解说明该资源需要用户登陆,可以用在Controller或者方法上,不需要参数。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresAuthentication {
}
  • RequiresGuest

该注解说明该资源需要用户登陆,并且可以是guest,可以用在Controller或者方法上,不需要参数。

  • RequiresUser

该注解说明该资源需要用户登陆,可以用在Controller或者方法上,不需要参数。

前端实现


shrio在jsp提供了 shiro:hasPermission 标签保护前端资源。
实例

<!-- 删除按钮 -->
<shiro:hasPermission name="business:contractInfo:del">
    <table:delRow url="${ctx}/business/contractInfo/deleteAll" id="contentTable"/>
</shiro:hasPermission>

如果没有business:contractInfo:del权限描述符的话,则在渲染后的前端页面不会出现该删除按钮。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接