Java权限管理系统之代码实现(二) 您所在的位置:网站首页 java权限管理实现 Java权限管理系统之代码实现(二)

Java权限管理系统之代码实现(二)

2023-08-16 02:11| 来源: 网络整理| 查看: 265

      一、概述

1、本Java代码的实现基于上篇Java权限管理系统之数据库设计描述的数据库设计基础之上,后端采用Springboot+shiro+mybatis整合实现用户登录认证和权限配置。

2、Shiro简介:Apache Shiro是Java的一个安全框架。Shiro可以非常容易的开发出足够好的应用,其不仅可以在JavaSE环境,也可以用在JavaEE环境。Shiro可以帮助我们完成:认证,授权,加密,会话管理,与Web集成,缓存等。基本功能如下图所示:

二、实现

      (1)添加依赖包(项目中存在其它的应用,故而存在较多的jar包)

org.springframework.boot spring-boot-starter-web mysql mysql-connector-java 5.1.45 com.fasterxml.jackson.core jackson-core 2.8.11 com.fasterxml.jackson.core jackson-annotations 2.8.11 com.fasterxml.jackson.core jackson-databind 2.8.11 net.sf.json-lib json-lib 2.4 jdk15 org.apache.commons commons-lang3 3.6 org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 com.alibaba druid 1.0.9 org.projectlombok lombok org.apache.shiro shiro-spring 1.4.0 com.github.theborakompanioni thymeleaf-extras-shiro 2.0.0 org.springframework.boot spring-boot-starter-thymeleaf taglibs standard 1.1.2 commons-beanutils commons-beanutils 1.9.3 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine com.vaadin.external.google android-json 0.0.20131108.vaadin1 compile

    (2)properties文件配置

server.port=9999 spring.datasource.url=jdbc:mysql://localhost:3306/Permission spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.type-aliases-package=com.itheima.domain spring.profiles.active=dev logging.config=classpath:logback1-core.xml spring.thymeleaf.prefix=classpath:/web/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=LEGACYHTML5

       (3)假定已经实现了登录功能,根据不同用户权限得到不同的导航菜单。

         后端实现

            menu类

@Setter @Getter public class MenuVo { private String id; private String name; private String url; private String pId; private boolean open; private boolean checked; }

       权限实体类

@Setter @Getter public class Function { private Integer func_id; private String func_name; private String func_url; private String func_code; private Integer parent_id; private Integer func_type; private Integer status; private Integer sort_num; private String create_time; private String update_time; }

     接口层

@Mapper public interface IFunctionMapper { /** * 根据用户的id查找权限菜单 * @param userId * @return */ @Select("select\n" + "\t\t\tdistinct\n" + "\t\t\tf.func_id,\n" + "\t\t\tf.func_name,\n" + "\t\t\tf.func_url,\n" + "\t\t\tf.func_code,\n" + "\t\t\tf.parent_id,\n" + "\t\t\tf.func_type,\n" + "\t\t\tf.status,\n" + "\t\t\tf.sort_num,\n" + "\t\t\tf.create_time,\n" + "\t\t\tf.update_time\n" + "\t\tfrom tb_roles r,tb_functions f,tb_role_function rf where r.role_id =\n" + "\t\trf.role_id and f.func_id = rf.func_id and r.role_id in(\n" + "\t\tselect r.role_id from tb_users u,tb_roles r,tb_user_role ur where u.user_id\n" + "\t\t= ur.user_id and r.role_id = ur.role_id and f.func_type=1 and u.user_id = #{userId});") List findMenu(@Param("userId")int userId); }

    服务层

public List findMenu(int userId) { List menus = new ArrayList(); List functions = functionMapper.findMenu(userId); if(functions != null && functions.size() > 0){ for(Function f : functions){ MenuVo mv = new MenuVo(); mv.setId(f.getFunc_id().toString()); mv.setName(f.getFunc_name()); mv.setUrl(f.getFunc_url()); if(f.getParent_id() != null) { mv.setpId(f.getParent_id().toString()); mv.setOpen(false); }else{ mv.setOpen(true); } mv.setChecked(false); menus.add(mv); } } return menus; }

        控制器

@ResponseBody @RequestMapping("/menu.do") public List findMenu(HttpServletRequest request){ List menus = new ArrayList(); //获取当前用户信息 User user = (User) SecurityUtils.getSubject().getPrincipal(); if(user != null){ menus = menuService.findMenu(user.getUser_id()); } return menus; }

        

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有