优化模块结构
This commit is contained in:
parent
4f9f057d21
commit
8718506433
|
@ -2,7 +2,7 @@ package com.diboot.example.controller;
|
||||||
|
|
||||||
import com.diboot.core.vo.JsonResult;
|
import com.diboot.core.vo.JsonResult;
|
||||||
import com.diboot.core.vo.Status;
|
import com.diboot.core.vo.Status;
|
||||||
import com.diboot.shiro.BaseJwtAuthenticationToken;
|
import com.diboot.shiro.jwt.BaseJwtAuthenticationToken;
|
||||||
import com.diboot.shiro.config.AuthType;
|
import com.diboot.shiro.config.AuthType;
|
||||||
import com.diboot.shiro.entity.SysUser;
|
import com.diboot.shiro.entity.SysUser;
|
||||||
import com.diboot.shiro.service.AuthWayService;
|
import com.diboot.shiro.service.AuthWayService;
|
||||||
|
@ -11,17 +11,14 @@ import org.apache.shiro.subject.Subject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/token")
|
@RequestMapping("/auth")
|
||||||
public class AuthTokenController {
|
public class AuthTokenController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(AuthTokenController.class);
|
private static final Logger logger = LoggerFactory.getLogger(AuthTokenController.class);
|
||||||
|
@ -38,7 +35,7 @@ public class AuthTokenController {
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public JsonResult login(@ModelAttribute SysUser sysUser, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
public JsonResult login(@RequestBody SysUser sysUser, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||||
String errorMsg = "登录失败";
|
String errorMsg = "登录失败";
|
||||||
try{
|
try{
|
||||||
BaseJwtAuthenticationToken authToken = new BaseJwtAuthenticationToken(authWayServiceMap, sysUser.getUsername(), sysUser.getPassword(), AuthType.USERNAME_PASSWORD);
|
BaseJwtAuthenticationToken authToken = new BaseJwtAuthenticationToken(authWayServiceMap, sysUser.getUsername(), sysUser.getPassword(), AuthType.USERNAME_PASSWORD);
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.diboot.example.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.diboot.core.util.V;
|
||||||
|
import com.diboot.core.vo.JsonResult;
|
||||||
|
import com.diboot.core.vo.Status;
|
||||||
|
import com.diboot.shiro.entity.SysUser;
|
||||||
|
import com.diboot.shiro.service.RoleService;
|
||||||
|
import com.diboot.shiro.service.SysUserService;
|
||||||
|
import com.diboot.shiro.util.JwtHelper;
|
||||||
|
import com.diboot.shiro.vo.RoleVO;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/sysUser")
|
||||||
|
public class SysUserController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RoleService roleService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SysUserService sysUserService;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 获取登录用户信息
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@GetMapping("/info")
|
||||||
|
public JsonResult info(HttpServletRequest request) throws Exception{
|
||||||
|
String token = JwtHelper.getRequestToken(request);
|
||||||
|
if (V.isEmpty(token)){
|
||||||
|
return new JsonResult(Status.FAIL_OPERATION, new String[]{"获取数据失败"});
|
||||||
|
}
|
||||||
|
|
||||||
|
String username = JwtHelper.getAccountFromToken(token);
|
||||||
|
if (V.isEmpty(username)){
|
||||||
|
return new JsonResult(Status.FAIL_OPERATION, new String[]{"获取数据失败"});
|
||||||
|
}
|
||||||
|
|
||||||
|
QueryWrapper<SysUser> query = new QueryWrapper<>();
|
||||||
|
query.lambda()
|
||||||
|
.eq(SysUser::getUsername, username);
|
||||||
|
List<SysUser> userList = sysUserService.getEntityList(query);
|
||||||
|
if (V.isEmpty(userList)){
|
||||||
|
return new JsonResult(Status.FAIL_OPERATION, new String[]{"获取数据失败"});
|
||||||
|
}
|
||||||
|
|
||||||
|
SysUser user = userList.get(0);
|
||||||
|
|
||||||
|
List<RoleVO> roleVOList = roleService.getRelatedRoleAndPermissionListByUser(SysUser.class.getSimpleName(), user.getId());
|
||||||
|
if (V.isEmpty(roleVOList)){
|
||||||
|
return new JsonResult(Status.FAIL_OPERATION, new String[]{"获取用户角色失败"});
|
||||||
|
}
|
||||||
|
|
||||||
|
user.setRoleVOList(roleVOList);
|
||||||
|
|
||||||
|
return new JsonResult(Status.OK, user, new String[]{"获取角色列表成功"});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package com.diboot.shiro.config;
|
package com.diboot.shiro.config;
|
||||||
|
|
||||||
import com.diboot.shiro.BaseJwtAuthenticationFilter;
|
import com.diboot.shiro.jwt.BaseJwtAuthenticationFilter;
|
||||||
import com.diboot.shiro.BaseJwtRealm;
|
import com.diboot.shiro.jwt.BaseJwtRealm;
|
||||||
import org.apache.shiro.mgt.SecurityManager;
|
import org.apache.shiro.mgt.SecurityManager;
|
||||||
import org.apache.shiro.realm.Realm;
|
import org.apache.shiro.realm.Realm;
|
||||||
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
||||||
|
@ -57,9 +57,9 @@ public class ShiroConfig {
|
||||||
|
|
||||||
filterChainDefinitionMap.put("/", "anon");
|
filterChainDefinitionMap.put("/", "anon");
|
||||||
filterChainDefinitionMap.put("/static/**", "anon");
|
filterChainDefinitionMap.put("/static/**", "anon");
|
||||||
filterChainDefinitionMap.put("/token/login", "anon");
|
filterChainDefinitionMap.put("/auth/login", "anon");
|
||||||
filterChainDefinitionMap.put("/error", "anon");
|
filterChainDefinitionMap.put("/error", "anon");
|
||||||
filterChainDefinitionMap.put("/token/logout", "logout");
|
filterChainDefinitionMap.put("/auth/logout", "logout");
|
||||||
filterChainDefinitionMap.put("/**", "jwt");
|
filterChainDefinitionMap.put("/**", "jwt");
|
||||||
|
|
||||||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
package com.diboot.shiro.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.diboot.core.controller.BaseCrudRestController;
|
||||||
|
import com.diboot.core.service.BaseService;
|
||||||
|
import com.diboot.core.util.BeanUtils;
|
||||||
|
import com.diboot.core.util.V;
|
||||||
|
import com.diboot.core.vo.JsonResult;
|
||||||
|
import com.diboot.core.vo.Pagination;
|
||||||
|
import com.diboot.core.vo.Status;
|
||||||
|
import com.diboot.shiro.entity.Permission;
|
||||||
|
import com.diboot.shiro.service.PermissionService;
|
||||||
|
import com.diboot.shiro.vo.PermissionVO;
|
||||||
|
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.ui.ModelMap;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Organization相关Controller
|
||||||
|
* @author Mazhicheng
|
||||||
|
* @version 2018/12/23
|
||||||
|
* Copyright © www.dibo.ltd
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/permission")
|
||||||
|
public class PermissionController extends BaseCrudRestController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(PermissionService.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PermissionService permissionService;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 查询ViewObject的分页数据 (此为非继承的自定义使用案例,更简化的调用父类案例请参考UserController)
|
||||||
|
* <p>
|
||||||
|
* url参数示例: /list?_pageSize=20&_pageIndex=1&_orderBy=id&code=TST
|
||||||
|
* </p>
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("permission:list")
|
||||||
|
@GetMapping("/list")
|
||||||
|
public JsonResult getVOList(HttpServletRequest request) throws Exception{
|
||||||
|
QueryWrapper<Permission> queryWrapper = buildQuery(request);
|
||||||
|
// 构建分页
|
||||||
|
Pagination pagination = buildPagination(request);
|
||||||
|
// 查询当前页的Entity主表数据
|
||||||
|
List entityList = getService().getEntityList(queryWrapper, pagination);
|
||||||
|
// 自动转换VO中注解绑定的关联
|
||||||
|
List<PermissionVO> voList = super.convertToVoAndBindRelations(entityList, PermissionVO.class);
|
||||||
|
|
||||||
|
return new JsonResult(Status.OK, voList).bindPagination(pagination);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 创建Entity
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("permission:add")
|
||||||
|
@PostMapping("/")
|
||||||
|
public JsonResult createEntity(@ModelAttribute PermissionVO viewObject, BindingResult result, HttpServletRequest request, ModelMap modelMap)
|
||||||
|
throws Exception{
|
||||||
|
// 转换
|
||||||
|
Permission entity = BeanUtils.convert(viewObject, Permission.class);
|
||||||
|
// 创建
|
||||||
|
return super.createEntity(entity, result, modelMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 查询Entity
|
||||||
|
* @param id ID
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("permission:get")
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public JsonResult getModel(@PathVariable("id")Long id, HttpServletRequest request, ModelMap modelMap)
|
||||||
|
throws Exception{
|
||||||
|
PermissionVO vo = permissionService.getViewObject(id, PermissionVO.class);
|
||||||
|
return new JsonResult(vo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 更新Entity
|
||||||
|
* @param id ID
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("permission:update")
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public JsonResult updateModel(@PathVariable("id")Long id, @ModelAttribute Permission entity, BindingResult result,
|
||||||
|
HttpServletRequest request, ModelMap modelMap) throws Exception{
|
||||||
|
return super.updateEntity(entity, result, modelMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 删除用户
|
||||||
|
* @param id 用户ID
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("permission:delete")
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public JsonResult deleteModel(@PathVariable("id")Long id, HttpServletRequest request) throws Exception{
|
||||||
|
return super.deleteEntity(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected BaseService getService() {
|
||||||
|
return permissionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package com.diboot.shiro.entity;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.diboot.core.entity.BaseEntity;
|
import com.diboot.core.entity.BaseEntity;
|
||||||
|
import com.diboot.shiro.vo.RoleVO;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -31,6 +32,9 @@ public class SysUser extends BaseEntity {
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private List<Role> roleList;
|
private List<Role> roleList;
|
||||||
|
|
||||||
|
@TableField(exist = false)
|
||||||
|
private List<RoleVO> roleVOList;
|
||||||
|
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private List<Permission> permissionList;
|
private List<Permission> permissionList;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package com.diboot.shiro;
|
package com.diboot.shiro.jwt;
|
||||||
|
|
||||||
import com.diboot.core.util.JSON;
|
import com.diboot.core.util.JSON;
|
||||||
import com.diboot.core.util.V;
|
import com.diboot.core.util.V;
|
||||||
import com.diboot.core.vo.JsonResult;
|
import com.diboot.core.vo.JsonResult;
|
||||||
import com.diboot.core.vo.Status;
|
import com.diboot.core.vo.Status;
|
||||||
import com.diboot.shiro.util.JwtHelper;
|
import com.diboot.shiro.util.JwtHelper;
|
||||||
import org.apache.shiro.SecurityUtils;
|
|
||||||
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
|
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -40,7 +39,7 @@ public class BaseJwtAuthenticationFilter extends BasicHttpAuthenticationFilter {
|
||||||
logger.warn("Token为空!url="+httpRequest.getRequestURL());
|
logger.warn("Token为空!url="+httpRequest.getRequestURL());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//获取userId
|
//获取username
|
||||||
String account = JwtHelper.getAccountFromToken(accessToken);
|
String account = JwtHelper.getAccountFromToken(accessToken);
|
||||||
if(V.notEmpty(account)){
|
if(V.notEmpty(account)){
|
||||||
logger.debug("Token认证成功!account="+account);
|
logger.debug("Token认证成功!account="+account);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.diboot.shiro;
|
package com.diboot.shiro.jwt;
|
||||||
|
|
||||||
import com.diboot.core.util.V;
|
import com.diboot.core.util.V;
|
||||||
import com.diboot.shiro.config.AuthType;
|
import com.diboot.shiro.config.AuthType;
|
|
@ -1,11 +1,8 @@
|
||||||
package com.diboot.shiro;
|
package com.diboot.shiro.jwt;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.diboot.core.entity.BaseEntity;
|
import com.diboot.core.entity.BaseEntity;
|
||||||
import com.diboot.core.util.V;
|
import com.diboot.core.util.V;
|
||||||
import com.diboot.shiro.entity.Permission;
|
import com.diboot.shiro.entity.Permission;
|
||||||
import com.diboot.shiro.entity.Role;
|
|
||||||
import com.diboot.shiro.entity.UserRole;
|
|
||||||
import com.diboot.shiro.service.*;
|
import com.diboot.shiro.service.*;
|
||||||
import com.diboot.shiro.vo.RoleVO;
|
import com.diboot.shiro.vo.RoleVO;
|
||||||
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.AuthenticationException;
|
||||||
|
@ -91,27 +88,7 @@ public class BaseJwtRealm extends AuthorizingRealm {
|
||||||
BaseEntity user = (BaseEntity) principals.getPrimaryPrincipal();
|
BaseEntity user = (BaseEntity) principals.getPrimaryPrincipal();
|
||||||
|
|
||||||
// 根据用户类型与用户id获取roleList
|
// 根据用户类型与用户id获取roleList
|
||||||
QueryWrapper<UserRole> query = new QueryWrapper<>();
|
List<RoleVO> roleVOList = roleService.getRelatedRoleAndPermissionListByUser(userType, user.getId());
|
||||||
query.lambda()
|
|
||||||
.eq(UserRole::getUserType, userType)
|
|
||||||
.eq(UserRole::getUserId, user.getId());
|
|
||||||
List<UserRole> userRoleList = userRoleService.getEntityList(query);
|
|
||||||
if (V.isEmpty(userRoleList)){
|
|
||||||
return authorizationInfo;
|
|
||||||
}
|
|
||||||
List<Long> roleIdList = userRoleList.stream()
|
|
||||||
.map(UserRole::getRoleId)
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
if (V.isEmpty(roleIdList)){
|
|
||||||
return authorizationInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取角色列表,并使用VO自动多对多关联permission
|
|
||||||
QueryWrapper<Role> roleQuery = new QueryWrapper<>();
|
|
||||||
roleQuery
|
|
||||||
.lambda()
|
|
||||||
.in(Role::getId, roleIdList);
|
|
||||||
List<RoleVO> roleVOList = roleService.getViewObjectList(roleQuery, null, RoleVO.class);
|
|
||||||
|
|
||||||
if (V.isEmpty(roleVOList)){
|
if (V.isEmpty(roleVOList)){
|
||||||
return authorizationInfo;
|
return authorizationInfo;
|
|
@ -1,7 +1,7 @@
|
||||||
package com.diboot.shiro.service;
|
package com.diboot.shiro.service;
|
||||||
|
|
||||||
import com.diboot.core.entity.BaseEntity;
|
import com.diboot.core.entity.BaseEntity;
|
||||||
import com.diboot.shiro.BaseJwtAuthenticationToken;
|
import com.diboot.shiro.jwt.BaseJwtAuthenticationToken;
|
||||||
import com.diboot.shiro.config.AuthType;
|
import com.diboot.shiro.config.AuthType;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
|
|
@ -2,6 +2,10 @@ package com.diboot.shiro.service;
|
||||||
|
|
||||||
import com.diboot.core.service.BaseService;
|
import com.diboot.core.service.BaseService;
|
||||||
import com.diboot.shiro.entity.Role;
|
import com.diboot.shiro.entity.Role;
|
||||||
|
import com.diboot.shiro.vo.RoleVO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色相关Service
|
* 角色相关Service
|
||||||
|
@ -11,4 +15,12 @@ import com.diboot.shiro.entity.Role;
|
||||||
*/
|
*/
|
||||||
public interface RoleService extends BaseService<Role> {
|
public interface RoleService extends BaseService<Role> {
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 根据用户类型和用户id获取角色关联权限列表
|
||||||
|
* @param userType
|
||||||
|
* @param userId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<RoleVO> getRelatedRoleAndPermissionListByUser(String userType, Long userId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,25 @@
|
||||||
package com.diboot.shiro.service.impl;
|
package com.diboot.shiro.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.diboot.core.service.impl.BaseServiceImpl;
|
import com.diboot.core.service.impl.BaseServiceImpl;
|
||||||
|
import com.diboot.core.util.V;
|
||||||
|
import com.diboot.shiro.entity.Permission;
|
||||||
import com.diboot.shiro.entity.Role;
|
import com.diboot.shiro.entity.Role;
|
||||||
|
import com.diboot.shiro.entity.UserRole;
|
||||||
import com.diboot.shiro.mapper.RoleMapper;
|
import com.diboot.shiro.mapper.RoleMapper;
|
||||||
import com.diboot.shiro.service.RoleService;
|
import com.diboot.shiro.service.RoleService;
|
||||||
|
import com.diboot.shiro.service.UserRoleService;
|
||||||
|
import com.diboot.shiro.vo.RoleVO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色相关Service
|
* 角色相关Service
|
||||||
* @author Yangzhao
|
* @author Yangzhao
|
||||||
|
@ -17,4 +30,33 @@ import org.springframework.stereotype.Service;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements RoleService {
|
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements RoleService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRoleService userRoleService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<RoleVO> getRelatedRoleAndPermissionListByUser(String userType, Long userId) {
|
||||||
|
// 根据用户类型与用户id获取roleList
|
||||||
|
QueryWrapper<UserRole> query = new QueryWrapper<>();
|
||||||
|
query.lambda()
|
||||||
|
.eq(UserRole::getUserType, userType)
|
||||||
|
.eq(UserRole::getUserId, userId);
|
||||||
|
List<UserRole> userRoleList = userRoleService.getEntityList(query);
|
||||||
|
if (V.isEmpty(userRoleList)){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<Long> roleIdList = userRoleList.stream()
|
||||||
|
.map(UserRole::getRoleId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (V.isEmpty(roleIdList)){
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取角色列表,并使用VO自动多对多关联permission
|
||||||
|
QueryWrapper<Role> roleQuery = new QueryWrapper<>();
|
||||||
|
roleQuery
|
||||||
|
.lambda()
|
||||||
|
.in(Role::getId, roleIdList);
|
||||||
|
List<RoleVO> roleVOList = this.getViewObjectList(roleQuery, null, RoleVO.class);
|
||||||
|
return roleVOList;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,8 @@ package com.diboot.shiro.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.diboot.core.entity.BaseEntity;
|
import com.diboot.core.entity.BaseEntity;
|
||||||
import com.diboot.core.util.BeanUtils;
|
|
||||||
import com.diboot.core.util.V;
|
import com.diboot.core.util.V;
|
||||||
import com.diboot.shiro.BaseJwtAuthenticationToken;
|
import com.diboot.shiro.jwt.BaseJwtAuthenticationToken;
|
||||||
import com.diboot.shiro.config.AuthType;
|
import com.diboot.shiro.config.AuthType;
|
||||||
import com.diboot.shiro.entity.SysUser;
|
import com.diboot.shiro.entity.SysUser;
|
||||||
import com.diboot.shiro.service.AuthWayService;
|
import com.diboot.shiro.service.AuthWayService;
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.diboot.shiro.util;
|
||||||
|
|
||||||
|
import com.diboot.core.entity.BaseEntity;
|
||||||
|
import org.apache.shiro.SecurityUtils;
|
||||||
|
import org.apache.shiro.subject.Subject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class AuthHelper {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AuthHelper.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到当前登录的用户名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static <T extends BaseEntity>T getCurrentUser(){
|
||||||
|
try{
|
||||||
|
Subject subject = SecurityUtils.getSubject();
|
||||||
|
if(subject != null && subject.isAuthenticated()){
|
||||||
|
return (T)subject.getPrincipal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e){
|
||||||
|
logger.warn("获取用户信息异常", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 得到当前登录的用户id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Long getCurrentUserId(){
|
||||||
|
BaseEntity user = getCurrentUser();
|
||||||
|
if(user != null){
|
||||||
|
return (Long)user.getId();
|
||||||
|
}
|
||||||
|
if(logger.isDebugEnabled()){
|
||||||
|
logger.warn("无法获取当前用户Id!");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.diboot.shiro.vo;
|
||||||
|
|
||||||
|
import com.diboot.core.binding.annotation.BindEntityList;
|
||||||
|
import com.diboot.shiro.entity.Permission;
|
||||||
|
import com.diboot.shiro.entity.Role;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yangzhao
|
||||||
|
* @version v2.0
|
||||||
|
* @date 2019/6/6
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PermissionVO extends Permission {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 860775286174387052L;
|
||||||
|
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import com.diboot.shiro.entity.Role;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Yangzhao
|
* @author Yangzhao
|
||||||
|
|
Loading…
Reference in New Issue