修改: 修改权限判断,支持restful资源判断

This commit is contained in:
wangiegie@gmail.com 2017-11-08 10:22:10 +08:00
parent e2ec5b75c4
commit 8394ba93f7
19 changed files with 668 additions and 69 deletions

View File

@ -1,12 +1,19 @@
package com.github.pig.admin.controller;
import com.github.pig.admin.service.MenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.dto.MenuTree;
import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.service.SysMenuService;
import com.github.pig.admin.util.TreeUtil;
import com.github.pig.common.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
@ -17,7 +24,8 @@ import java.util.Set;
@RequestMapping("/menu")
public class MenuController {
@Autowired
private MenuService menuService;
private SysMenuService menuService;
/**
* 通过用户名查询用户菜单
*
@ -25,7 +33,29 @@ public class MenuController {
* @return 菜单列表
*/
@GetMapping("/findMenuByRole/{role}")
public Set<String> findMenuByRole(@PathVariable String role) {
public Set<MenuVo> findMenuByRole(@PathVariable String role) {
return menuService.findMenuByRole(role);
}
@GetMapping(value = "/tree")
public List<MenuTree> getTree(String title) {
return getMenuTree(menuService.selectList(new EntityWrapper<>()), -1);
}
private List<MenuTree> getMenuTree(List<SysMenu> menus, int root) {
List<MenuTree> trees = new ArrayList<MenuTree>();
MenuTree node = null;
for (SysMenu menu : menus) {
node = new MenuTree();
node.setId(menu.getMenuId());
node.setParentId(menu.getParentId());
node.setTitle(menu.getMenuName());
node.setHref(menu.getUrl());
node.setPath(menu.getUrl());
node.setCode(menu.getMenuName());
node.setLabel(menu.getMenuName());
trees.add(node);
}
return TreeUtil.bulid(trees, root);
}
}

View File

@ -3,12 +3,17 @@ package com.github.pig.admin.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.entity.SysRole;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.admin.service.SysRoleService;
import com.github.pig.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
/**
@ -73,6 +78,7 @@ public class RoleController {
SysRole condition = new SysRole();
condition.setDelFlag(CommonConstant.STATUS_NORMAL);
return sysRoleService.selectList(new EntityWrapper<>(condition));
}
/**
@ -86,6 +92,7 @@ public class RoleController {
public Page rolePage(Integer page, Integer limit) {
SysRole condition = new SysRole();
condition.setDelFlag(CommonConstant.STATUS_NORMAL);
return sysRoleService.selectPage(new Page<>(page, limit),new EntityWrapper<>(condition));
return sysRoleService.selectPage(new Page<>(page, limit), new EntityWrapper<>(condition));
}
}

View File

@ -33,7 +33,7 @@ public class UserController extends BaseController {
*
* @return 用户名
*/
@GetMapping
@GetMapping("/info")
public String user() {
return getUser();
}
@ -49,8 +49,14 @@ public class UserController extends BaseController {
return userService.selectById(id);
}
/**
* 删除用户信息
*
* @param id ID
* @return boolean
*/
@DeleteMapping("/{id}")
public Boolean userDel(@PathVariable Integer id){
public Boolean userDel(@PathVariable Integer id) {
SysUser sysUser = userService.selectById(id);
sysUser.setDelFlag(CommonConstant.STATUS_DEL);
return userService.updateById(sysUser);
@ -102,7 +108,7 @@ public class UserController extends BaseController {
* @param username 用户名
* @return UseVo 对象
*/
@RequestMapping("/findUserByUsername/{username}")
@GetMapping("/findUserByUsername/{username}")
public UserVo findUserByUsername(@PathVariable String username) {
return userService.findUserByUsername(username);
}

View File

@ -0,0 +1,113 @@
package com.github.pig.admin.dto;
public class MenuTree extends TreeNode {
private String icon;
private String title;
private String href;
private boolean spread = false;
private String path;
private String component;
private String authority;
private String redirect;
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getComponent() {
return component;
}
public void setComponent(String component) {
this.component = component;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
String label;
public MenuTree() {
}
public MenuTree(int id, String name, int parentId) {
this.id = id;
this.parentId = parentId;
this.title = name;
this.label = name;
}
public MenuTree(int id, String name, MenuTree parent) {
this.id = id;
this.parentId = parent.getId();
this.title = name;
this.label = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public boolean isSpread() {
return spread;
}
public void setSpread(boolean spread) {
this.spread = spread;
}
}

View File

@ -0,0 +1,132 @@
package com.github.pig.admin.dto;
import java.io.Serializable;
import java.util.Map;
/**
* @author lengleng
* @date 2017/11/7
*/
public class RouteConfig implements Serializable{
@com.alibaba.fastjson.annotation.JSONField(name = "path")
private String path;
@com.alibaba.fastjson.annotation.JSONField(name = "component")
private String component;
@com.alibaba.fastjson.annotation.JSONField(name = "name")
private String name;
@com.alibaba.fastjson.annotation.JSONField(name = "components")
private String components;
@com.alibaba.fastjson.annotation.JSONField(name = "redirect")
private String redirect;
@com.alibaba.fastjson.annotation.JSONField(name = "props")
private String props;
@com.alibaba.fastjson.annotation.JSONField(name = "alias")
private String alias;
@com.alibaba.fastjson.annotation.JSONField(name = "children")
private String children;
@com.alibaba.fastjson.annotation.JSONField(name = "beforeEnter")
private String beforeEnter;
@com.alibaba.fastjson.annotation.JSONField(name = "meta")
private Map<String,String> meta;
@com.alibaba.fastjson.annotation.JSONField(name = "caseSensitive")
private Boolean caseSensitive;
@com.alibaba.fastjson.annotation.JSONField(name = "pathToRegexpOptions")
private String pathToRegexpOptions;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getComponent() {
return component;
}
public void setComponent(String component) {
this.component = component;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComponents() {
return components;
}
public void setComponents(String components) {
this.components = components;
}
public String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
public String getProps() {
return props;
}
public void setProps(String props) {
this.props = props;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
public String getBeforeEnter() {
return beforeEnter;
}
public void setBeforeEnter(String beforeEnter) {
this.beforeEnter = beforeEnter;
}
public Map<String, String> getMeta() {
return meta;
}
public void setMeta(Map<String, String> meta) {
this.meta = meta;
}
public Boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(Boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public String getPathToRegexpOptions() {
return pathToRegexpOptions;
}
public void setPathToRegexpOptions(String pathToRegexpOptions) {
this.pathToRegexpOptions = pathToRegexpOptions;
}
}

View File

@ -0,0 +1,39 @@
package com.github.pig.admin.dto;
import java.util.ArrayList;
import java.util.List;
public class TreeNode {
protected int id;
protected int parentId;
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
List<TreeNode> children = new ArrayList<TreeNode>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public void add(TreeNode node){
children.add(node);
}
}

View File

@ -8,7 +8,6 @@ import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableName;
import java.io.Serializable;
/**
* <p>
@ -16,7 +15,7 @@ import java.io.Serializable;
* </p>
*
* @author lengleng
* @since 2017-10-29
* @since 2017-11-08
*/
@TableName("sys_menu")
public class SysMenu extends Model<SysMenu> {
@ -26,7 +25,7 @@ public class SysMenu extends Model<SysMenu> {
/**
* 菜单ID
*/
@TableId(value="menu_id", type= IdType.AUTO)
@TableId(value="menu_id", type= IdType.INPUT)
private Integer menuId;
/**
* 菜单名称
@ -39,6 +38,10 @@ public class SysMenu extends Model<SysMenu> {
@TableField("menu_desc")
private String menuDesc;
private String url;
/**
* 请求方法
*/
private String method;
/**
* 父菜单ID
*/
@ -101,6 +104,14 @@ public class SysMenu extends Model<SysMenu> {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Integer getParentId() {
return parentId;
}
@ -161,6 +172,7 @@ public class SysMenu extends Model<SysMenu> {
", menuName=" + menuName +
", menuDesc=" + menuDesc +
", url=" + url +
", method=" + method +
", parentId=" + parentId +
", sort=" + sort +
", type=" + type +

View File

@ -2,6 +2,7 @@ package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.github.pig.admin.entity.SysMenu;
import com.github.pig.common.vo.MenuVo;
import java.util.Set;
@ -21,5 +22,5 @@ public interface SysMenuMapper extends BaseMapper<SysMenu> {
* @param role 角色名称
* @return 菜单列表
*/
Set<String> findMenuByRoleName(String role);
Set<MenuVo> findMenuByRoleName(String role);
}

View File

@ -1,19 +0,0 @@
package com.github.pig.admin.service;
import com.github.pig.admin.entity.SysMenu;
import java.util.Set;
/**
* @author lengleng
* @date 2017/10/31
*/
public interface MenuService {
/**
* 通过角色名称查询URL 权限
*
* @param role 角色名称
* @return 菜单列表
*/
Set<String> findMenuByRole(String role);
}

View File

@ -0,0 +1,26 @@
package com.github.pig.admin.service;
import com.baomidou.mybatisplus.service.IService;
import com.github.pig.admin.entity.SysMenu;
import com.github.pig.common.vo.MenuVo;
import java.util.Set;
/**
* <p>
* 菜单权限表 服务类
* </p>
*
* @author lengleng
* @since 2017-10-29
*/
public interface SysMenuService extends IService<SysMenu> {
/**
* 通过角色名称查询URL 权限
*
* @param role 角色名称
* @return 菜单列表
*/
Set<MenuVo> findMenuByRole(String role);
}

View File

@ -1,25 +1,31 @@
package com.github.pig.admin.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.mapper.SysMenuMapper;
import com.github.pig.admin.service.MenuService;
import com.github.pig.admin.service.SysMenuService;
import com.github.pig.common.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* <p>
* 菜单权限表 服务实现类
* </p>
*
* @author lengleng
* @date 2017/10/31
* @since 2017-10-29
*/
@Service
public class MenuServiceImpl implements MenuService {
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
@Autowired
private SysMenuMapper sysMenuMapper;
@Override
//@Cacheable(value = "menu_details", key = "#role +'_menu'")
public Set<String> findMenuByRole(String role) {
public Set<MenuVo> findMenuByRole(String role) {
return sysMenuMapper.findMenuByRoleName(role);
}
}

View File

@ -22,7 +22,7 @@ public class MybatisPlusGenerator {
gc.setFileOverride(true);
gc.setActiveRecord(true);
// XML 二级缓存
gc.setEnableCache(true);
gc.setEnableCache(false);
// XML ResultMap
gc.setBaseResultMap(true);
// XML columList

View File

@ -0,0 +1,75 @@
package com.github.pig.admin.util;
import com.github.pig.admin.dto.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ace on 2017/6/12.
*/
public class TreeUtil {
/**
* 两层循环实现建树
*
* @param treeNodes 传入的树节点列表
* @return
*/
public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, Object root) {
List<T> trees = new ArrayList<T>();
for (T treeNode : treeNodes) {
if (root.equals(treeNode.getParentId())) {
trees.add(treeNode);
}
for (T it : treeNodes) {
if (it.getParentId() == treeNode.getId()) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<TreeNode>());
}
treeNode.add(it);
}
}
}
return trees;
}
/**
* 使用递归方法建树
*
* @param treeNodes
* @return
*/
public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes,Object root) {
List<T> trees = new ArrayList<T>();
for (T treeNode : treeNodes) {
if (root.equals(treeNode.getParentId())) {
trees.add(findChildren(treeNode, treeNodes));
}
}
return trees;
}
/**
* 递归查找子节点
*
* @param treeNodes
* @return
*/
public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
for (T it : treeNodes) {
if (treeNode.getId() == it.getParentId()) {
if (treeNode.getChildren() == null) {
treeNode.setChildren(new ArrayList<TreeNode>());
}
treeNode.add(findChildren(it, treeNodes));
}
}
return treeNode;
}
}

View File

@ -2,31 +2,46 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.github.pig.admin.mapper.SysMenuMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.github.pig.admin.entity.SysMenu">
<id column="menu_id" property="menuId" />
<result column="menu_name" property="menuName" />
<result column="menu_desc" property="menuDesc" />
<result column="url" property="url" />
<result column="parent_id" property="parentId" />
<result column="sort" property="sort" />
<result column="type" property="type" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
<result column="del_flag" property="delFlag" />
</resultMap>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.github.pig.admin.entity.SysMenu">
<id column="menu_id" property="menuId"/>
<result column="menu_name" property="menuName"/>
<result column="menu_desc" property="menuDesc"/>
<result column="url" property="url"/>
<result column="method" property="method"/>
<result column="parent_id" property="parentId"/>
<result column="sort" property="sort"/>
<result column="type" property="type"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="del_flag" property="delFlag"/>
</resultMap>
<select id="findMenuByRoleName" resultType="string">
SELECT
sys_menu.url
FROM
sys_role
LEFT JOIN sys_role_menu ON sys_role_menu.role_id = sys_role.role_id
LEFT JOIN sys_menu ON sys_menu.menu_id = sys_role_menu.menu_id
WHERE
sys_role.del_flag = 0
AND sys_menu.del_flag = 0
AND sys_menu.url IS NOT NULL AND
sys_role.role_code = #{role}
</select>
<resultMap id="MenuVoResultMap" type="com.github.pig.common.vo.MenuVo">
<id column="menu_id" property="menuId"/>
<result column="menu_name" property="menuName"/>
<result column="menu_desc" property="menuDesc"/>
<result column="url" property="url"/>
<result column="method" property="method"/>
<result column="parent_id" property="parentId"/>
<result column="sort" property="sort"/>
<result column="type" property="type"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="del_flag" property="delFlag"/>
</resultMap>
<select id="findMenuByRoleName" resultMap="MenuVoResultMap">
SELECT
sys_menu.*
FROM
sys_role
LEFT JOIN sys_role_menu ON sys_role_menu.role_id = sys_role.role_id
LEFT JOIN sys_menu ON sys_menu.menu_id = sys_role_menu.menu_id
WHERE
sys_role.del_flag = 0
AND sys_menu.del_flag = 0
AND sys_menu.url IS NOT NULL AND
sys_role.role_code = #{role}
</select>
</mapper>

View File

@ -34,6 +34,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
</dependencies>

View File

@ -0,0 +1,147 @@
package com.github.pig.common.vo;
import java.io.Serializable;
import java.util.Date;
/**
* <p>
* 菜单权限表
* </p>
*
* @author lengleng
* @since 2017-11-08
*/
public class MenuVo implements Serializable {
/**
* 菜单ID
*/
private Integer menuId;
/**
* 菜单名称
*/
private String menuName;
/**
* 菜单描述
*/
private String menuDesc;
private String url;
/**
* 请求方法
*/
private String method;
/**
* 父菜单ID
*/
private Integer parentId;
/**
* 排序值
*/
private Integer sort;
/**
* 菜单类型 0菜单 1按钮
*/
private String type;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 0--正常 1--删除
*/
private String delFlag;
public Integer getMenuId() {
return menuId;
}
public void setMenuId(Integer menuId) {
this.menuId = menuId;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public String getMenuDesc() {
return menuDesc;
}
public void setMenuDesc(String menuDesc) {
this.menuDesc = menuDesc;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getDelFlag() {
return delFlag;
}
public void setDelFlag(String delFlag) {
this.delFlag = delFlag;
}
}

View File

@ -1,5 +1,6 @@
package com.github.pig.gateway.feign;
import com.github.pig.common.vo.MenuVo;
import com.github.pig.gateway.feign.fallback.MenuServiceFallbackImpl;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@ -20,5 +21,5 @@ public interface MenuService {
* @return 菜单列表
*/
@GetMapping(value = "/menu/findMenuByRole/{role}")
Set<String> findMenuByRole(@PathVariable("role") String role);
Set<MenuVo> findMenuByRole(@PathVariable("role") String role);
}

View File

@ -1,5 +1,6 @@
package com.github.pig.gateway.feign.fallback;
import com.github.pig.common.vo.MenuVo;
import com.github.pig.gateway.feign.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -13,7 +14,7 @@ import java.util.Set;
public class MenuServiceFallbackImpl implements MenuService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Set<String> findMenuByRole(String role) {
public Set<MenuVo> findMenuByRole(String role) {
logger.error("调用{}异常{}","findMenuByRole",role);
return null;
}

View File

@ -1,5 +1,6 @@
package com.github.pig.gateway.service.impl;
import com.github.pig.common.vo.MenuVo;
import com.github.pig.gateway.feign.MenuService;
import com.github.pig.gateway.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
@ -35,14 +36,14 @@ public class PermissionServiceImpl implements PermissionService {
boolean hasPermission = false;
if (principal != null) {
Set<String> urls = menuService.findMenuByRole(grantedAuthorityList.get(0).getAuthority());
for (String url : urls) {
if (antPathMatcher.match(url,request.getRequestURI())) {
Set<MenuVo> urls = menuService.findMenuByRole(grantedAuthorityList.get(0).getAuthority());
for (MenuVo menu : urls) {
if (antPathMatcher.match(menu.getUrl(), request.getRequestURI())
&& request.getMethod().equalsIgnoreCase(menu.getMethod())) {
hasPermission = true;
break;
}
}
}
return hasPermission;
}