| | |
| | | |
| | | @PostMapping("logout") |
| | | @ApiOperation(value ="退出登录") |
| | | public void logout(){ |
| | | service.logout(); |
| | | public void logout(HttpServletRequest request){ |
| | | service.logout(request); |
| | | } |
| | | |
| | | /**拦截登录*//* |
| | |
| | | @RequestMapping("permission") |
| | | @Api(tags = "权限") |
| | | @Slf4j |
| | | @Deprecated |
| | | /** |
| | | * 不独立存在,依附于角色菜单 |
| | | * 预留单纯的权限接口 |
| | | */ |
| | | public class PermissionController { |
| | | |
| | | @Resource |
| | | private PermissionService permissionService; |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加") |
| | | public boolean add(@RequestBody Permission permission){ |
| | | return permissionService.add(permission); |
| | | } |
| | | |
| | | @PostMapping("/batch") |
| | | @ApiOperation(value = "添加批量") |
| | |
| | | return permissionService.getAll(); |
| | | } |
| | | |
| | | @GetMapping("/page") |
| | | @ApiOperation(value = "查询分页") |
| | | public IPage<Permission> getPage(@RequestParam int pageNum, int pageSize){ |
| | | Page<Permission> page = new Page<>(pageNum, pageSize); |
| | | return permissionService.getAllWithPage(page); |
| | | } |
| | | |
| | | @PutMapping |
| | | @ApiOperation(value = "编辑") |
| | | public boolean update(@RequestBody Permission permission){ |
| | | return permissionService.update(permission); |
| | | } |
| | | |
| | | @DeleteMapping |
| | | @ApiOperation(value = "删除") |
| | | public boolean delete(@RequestParam int id){ |
| | | return permissionService.delete(id); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | |
| | | /**===============角色与权限================**/ |
| | | |
| | | /** |
| | | * TODO 采用caffeine重写 |
| | | */ |
| | | /* |
| | | @PostMapping("bindingRoleWithPermission") |
| | | @ApiOperation(value = "绑定角色-权限") |
| | | @CacheEvict(cacheNames = {"userBridge"},allEntries = true) //全部清除 |
| | | public boolean bindingRoleWithPermission(@RequestParam int roleId,int permissionId){ |
| | | return rolePermissionService.bindingUserWithRole(roleId,permissionId); |
| | | } |
| | | }*/ |
| | | |
| | | /** |
| | | * TODO 采用caffeine重写 |
| | |
| | | return new Response().set(1,roleMenus); |
| | | } |
| | | |
| | | @GetMapping("allMenuByRoleId") |
| | | @ApiOperation(value = "查询角色对应所有状态的菜单") |
| | | public Response getAllMenuByRoleId(@RequestParam Integer roleId){ |
| | | List<Menu> roleMenus = service.getAllMenuByRoleId(roleId); |
| | | |
| | | return new Response().set(1,roleMenus); |
| | | } |
| | | |
| | | public Response getMenu(@RequestParam Integer roleId){ |
| | | List<Menu> roleMenus = service.getMenuByRoleId(roleId); |
| | | |
| | | return new Response().set(1,roleMenus); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.controller; |
| | | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.RoleMenuPermissionDTO; |
| | | import com.whyc.service.RoleMenuService; |
| | | import com.whyc.service.RolePermissionService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PutMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | @RequestMapping("userMenuPermission") |
| | | @RestController |
| | | @Api(tags = "角色-菜单-权限") |
| | | public class RoleMenuPermissionController { |
| | | |
| | | @Autowired |
| | | private RoleMenuService roleMenuService; |
| | | |
| | | @Autowired |
| | | private RolePermissionService rolePermissionService; |
| | | |
| | | @PutMapping("menuAndPermission") |
| | | @ApiOperation(value = "更新菜单-权限",notes = "传入对象的字段都只需id,status即可") |
| | | public Response updateMenuAndPermission(@RequestBody RoleMenuPermissionDTO dto){ |
| | | |
| | | roleMenuService.updateBatch(dto.getRoleMenuList()); |
| | | rolePermissionService.updateBatch(dto.getRolePermissionList()); |
| | | |
| | | return new Response().set(1,"更新成功"); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.controller; |
| | | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.Permission; |
| | | import com.whyc.service.RolePermissionService; |
| | | import com.whyc.util.CommonUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestParam; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.List; |
| | | |
| | | @RestController |
| | | @RequestMapping("rolePermission") |
| | | @Api(tags = "角色权限") |
| | | public class RolePermissionController { |
| | | |
| | | @Autowired |
| | | private RolePermissionService service; |
| | | |
| | | @GetMapping("permissionListByMenuId") |
| | | @ApiOperation(value = "查询角色的菜单对应的权限列表") |
| | | public Response getPermissionListByMenuId(@RequestParam Integer roleId, @RequestParam Integer menuId){ |
| | | List<Permission> permissionList = service.getPermissionListByMenuId(roleId,menuId); |
| | | return new Response().set(1,permissionList); |
| | | } |
| | | |
| | | |
| | | /*====== 用户-权限 ======*/ |
| | | @GetMapping("permissionList") |
| | | @ApiOperation(value = "查询当前用户对应的权限列表") |
| | | public Response getPermissionList(HttpServletRequest request){ |
| | | Integer userId = CommonUtil.getUser(request).getId(); |
| | | List<Permission> permissionList = service.getPermissionListByUserId(userId); |
| | | return new Response().set(1,permissionList); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.whyc.dto; |
| | | |
| | | import com.whyc.pojo.RoleMenu; |
| | | import com.whyc.pojo.RolePermission; |
| | | |
| | | import java.util.List; |
| | | |
| | | public class RoleMenuPermissionDTO { |
| | | private List<RoleMenu> roleMenuList; |
| | | private List<RolePermission> rolePermissionList; |
| | | |
| | | public List<RoleMenu> getRoleMenuList() { |
| | | return roleMenuList; |
| | | } |
| | | |
| | | public void setRoleMenuList(List<RoleMenu> roleMenuList) { |
| | | this.roleMenuList = roleMenuList; |
| | | } |
| | | |
| | | public List<RolePermission> getRolePermissionList() { |
| | | return rolePermissionList; |
| | | } |
| | | |
| | | public void setRolePermissionList(List<RolePermission> rolePermissionList) { |
| | | this.rolePermissionList = rolePermissionList; |
| | | } |
| | | } |
| | |
| | | |
| | | List<Menu> getUserMenu(Integer id); |
| | | |
| | | List<Menu> getMenuByRoleId(Integer roleId); |
| | | List<Menu> getMenuByRoleId(Integer roleId,Integer status); |
| | | |
| | | boolean updateBatch(List<RoleMenu> roleMenus); |
| | | } |
| | |
| | | List<Permission> getPermissions(Integer userId); |
| | | |
| | | List<String> findPermissionsByUserId(@Param("userId") int userId); |
| | | |
| | | List<Permission> getPermissionListByUserId(Integer userId, Integer status); |
| | | |
| | | List<Permission> getPermissionListByMenuId(Integer roleId, Integer menuId); |
| | | |
| | | boolean updateBatch(List<RolePermission> rolePermissions); |
| | | } |
| | |
| | | /**排序*/ |
| | | private Integer order; |
| | | |
| | | /**菜单在用户中的状态*/ |
| | | @TableField(exist = false) |
| | | private Integer status; |
| | | |
| | | @TableField(exist = false) |
| | | private List<Menu> menuList; |
| | | |
| | |
| | | public void setMenuList(List<Menu> menuList) { |
| | | this.menuList = menuList; |
| | | } |
| | | |
| | | public Integer getStatus() { |
| | | return status; |
| | | } |
| | | |
| | | public void setStatus(Integer status) { |
| | | this.status = status; |
| | | } |
| | | } |
| | |
| | | package com.whyc.pojo; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.baomidou.mybatisplus.annotation.TableName; |
| | | import org.apache.ibatis.type.Alias; |
| | | |
| | |
| | | public class Permission { |
| | | |
| | | private Integer id; |
| | | /**权限名*/ |
| | | private String name; |
| | | /** |
| | | * 权限值-资源:操作 |
| | | *举例: |
| | | * deviceInf:getAll |
| | | * */ |
| | | private String value; |
| | | /**菜单id*/ |
| | | private Integer menuId; |
| | | |
| | | /** |
| | | * 这个是用户-权限表的状态 |
| | | */ |
| | | @TableField(exist = false) |
| | | private Integer status; |
| | | |
| | | public Integer getId() { |
| | | return id; |
| | |
| | | public void setName(String name) { |
| | | this.name = name; |
| | | } |
| | | |
| | | public String getValue() { |
| | | return value; |
| | | } |
| | | |
| | | public void setValue(String value) { |
| | | this.value = value; |
| | | } |
| | | |
| | | public Integer getMenuId() { |
| | | return menuId; |
| | | } |
| | | |
| | | public void setMenuId(Integer menuId) { |
| | | this.menuId = menuId; |
| | | } |
| | | |
| | | public Integer getStatus() { |
| | | return status; |
| | | } |
| | | |
| | | public void setStatus(Integer status) { |
| | | this.status = status; |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 角色对应权限 |
| | | */ |
| | | @Alias("RolePrivilege") |
| | | @TableName( schema = "`db_experiment`",value = "tb_role_privilege") |
| | | @Alias("RolePermission") |
| | | @TableName( schema = "`db_experiment`",value = "tb_role_permission") |
| | | public class RolePermission { |
| | | |
| | | private Integer id; |
| | | private Integer roleId; |
| | | private Integer permissionId; |
| | | |
| | | public RolePermission(int roleId, int permissionId) { |
| | | this.roleId = roleId; |
| | | this.permissionId = permissionId; |
| | | } |
| | | private Integer status; |
| | | |
| | | public Integer getId() { |
| | | return id; |
| | |
| | | public void setPermissionId(Integer permissionId) { |
| | | this.permissionId = permissionId; |
| | | } |
| | | |
| | | public Integer getStatus() { |
| | | return status; |
| | | } |
| | | |
| | | public void setStatus(Integer status) { |
| | | this.status = status; |
| | | } |
| | | } |
| | |
| | | System.out.printf("账号不存在"); |
| | | throw new UnknownAccountException("账号不存在"); |
| | | } |
| | | |
| | | return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),getName()); |
| | | } |
| | | |
| | |
| | | //同一个账号,后面登录的,会挤掉之前登录的SessionId |
| | | System.out.println("全局存储中当前SessionId为:"+request.getSession().getId()); |
| | | request.getServletContext().setAttribute(userName,request.getSession().getId()); |
| | | //Session存储当前用户 |
| | | request.getSession().setAttribute("user", subject.getPrincipal()); |
| | | return new Response<>().set(1,true); |
| | | } |
| | | return new Response<>().set(1,false); |
| | | } |
| | | |
| | | public void logout() { |
| | | public void logout(HttpServletRequest request) { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | //移除Session中当前用户 |
| | | request.getSession().removeAttribute("user"); |
| | | subject.logout(); |
| | | } |
| | | } |
| | |
| | | |
| | | public List<Menu> getMenuByRoleId(Integer roleId) { |
| | | |
| | | List<Menu> menuList = mapper.getMenuByRoleId(roleId); |
| | | List<Menu> menuList = mapper.getMenuByRoleId(roleId,1); |
| | | |
| | | for (int i = 0; i < menuList.size(); i++) { |
| | | Integer parentId = menuList.get(i).getParentId(); |
| | |
| | | } |
| | | return menuList; |
| | | } |
| | | |
| | | public List<Menu> getAllMenuByRoleId(Integer roleId) { |
| | | |
| | | List<Menu> menuList = mapper.getMenuByRoleId(roleId,null); |
| | | |
| | | for (int i = 0; i < menuList.size(); i++) { |
| | | Integer parentId = menuList.get(i).getParentId(); |
| | | if(parentId !=0){ |
| | | //将这个menu插入到parent中 |
| | | for (int j = 0; j < menuList.size(); j++) { |
| | | if(menuList.get(j).getId()==parentId){ |
| | | List<Menu> menuListTemp = menuList.get(j).getMenuList(); |
| | | if(menuListTemp==null) { |
| | | menuListTemp = new LinkedList<>(); |
| | | } |
| | | menuListTemp.add(menuList.get(i)); |
| | | menuList.get(j).setMenuList(menuListTemp); |
| | | menuList.remove(i); |
| | | i--; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | return menuList; |
| | | } |
| | | |
| | | public boolean updateBatch(List<RoleMenu> roleMenus){ |
| | | return mapper.updateBatch(roleMenus); |
| | | } |
| | | |
| | | } |
| | |
| | | @Resource |
| | | private RolePermissionMapper mapper; |
| | | |
| | | public boolean bindingUserWithRole(int roleId, int permissionId) { |
| | | return mapper.insert(new RolePermission(roleId,permissionId))>0; |
| | | } |
| | | |
| | | @Transactional |
| | | public boolean bindingUserWithRoleBatch(List<RolePermission> rolePermissions) { |
| | | return mapper.insertBatchSomeColumn(rolePermissions)== rolePermissions.size(); |
| | |
| | | public List<Permission> getPermissions(Integer userId) { |
| | | return mapper.getPermissions(userId); |
| | | } |
| | | |
| | | public List<Permission> getPermissionListByMenuId(Integer roleId, Integer menuId) { |
| | | return mapper.getPermissionListByMenuId(roleId,menuId); |
| | | } |
| | | |
| | | public List<Permission> getPermissionListByUserId(Integer userId) { |
| | | return mapper.getPermissionListByUserId(userId,null); |
| | | } |
| | | |
| | | public boolean updateBatch(List<RolePermission> rolePermissions) { |
| | | return mapper.updateBatch(rolePermissions); |
| | | } |
| | | } |
| | |
| | | <result property="url" column="url"/> |
| | | <result property="parentId" column="parent_id"/> |
| | | <result property="order" column="order"/> |
| | | <result property="status" column="status"/> |
| | | </resultMap> |
| | | <update id="updateBatch"> |
| | | <foreach collection="list" item="roleMenu" separator=";"> |
| | | update db_experiment.tb_role_menu set status = #{roleMenu.status} where id = #{roleMenu.id} |
| | | </foreach> |
| | | </update> |
| | | |
| | | <select id="getUserMenu" resultType="com.whyc.pojo.Menu"> |
| | | |
| | | </select> |
| | | <select id="getMenuByRoleId" resultMap="Map_Menu"> |
| | | SELECT |
| | | rm.status, |
| | | m.* |
| | | FROM |
| | | db_experiment.tb_role_menu rm, |
| | | db_experiment.tb_menu m |
| | | WHERE |
| | | rm.menu_id = m.id AND rm.role_id = #{roleId} |
| | | AND |
| | | rm.status=1 |
| | | <if test="status!=null"> |
| | | AND rm.status=#{status} |
| | | </if> |
| | | |
| | | </select> |
| | | </mapper> |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.whyc.mapper.RolePermissionMapper" > |
| | | <update id="updateBatch"> |
| | | <foreach collection="list" item="item" separator=";"> |
| | | update db_experiment.tb_role_permission set status = #{item.status} where id = #{item.id} |
| | | </foreach> |
| | | </update> |
| | | |
| | | |
| | | <select id="findPermissionsByUserId" resultType="java.lang.String"> |
| | | select p.name from tb_user_role ur,tb_role_permission rp,tb_permission p |
| | |
| | | and rp.permission_id=p.id |
| | | and user_id=#{userId}; |
| | | </select> |
| | | <select id="getPermissionListByUserId" resultType="Permission"> |
| | | SELECT |
| | | p.* |
| | | FROM |
| | | db_experiment.tb_role_permission rp, |
| | | db_experiment.tb_permission p, |
| | | db_experiment.tb_user u |
| | | WHERE |
| | | rp.permission_id = p.id |
| | | AND rp.role_id = u.role_id |
| | | AND u.id = #{userId} |
| | | AND rp.status = 1 |
| | | </select> |
| | | <select id="getPermissionListByMenuId" resultType="Permission"> |
| | | SELECT |
| | | rp.status, |
| | | p.* |
| | | FROM |
| | | db_experiment.tb_role_permission rp, |
| | | db_experiment.tb_permission p |
| | | WHERE |
| | | rp.permission_id = p.id |
| | | AND rp.role_id = #{roleId} |
| | | AND p.menu_id = #{menuId} |
| | | </select> |
| | | </mapper> |