添加测试模块

This commit is contained in:
youbaokun 2021-04-23 10:26:58 +08:00
parent 528baa4996
commit 4ff69d7b7d
3 changed files with 57 additions and 91 deletions

View File

@ -2,6 +2,7 @@ package com.ruoyi.common.security.aspect;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature; import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
@ -19,81 +20,67 @@ import com.ruoyi.system.api.model.LoginUser;
/** /**
* 自定义权限实现 * 自定义权限实现
* *
* @author ruoyi * @author ruoyi
*/ */
@Aspect @Aspect
@Component @Component
public class PreAuthorizeAspect public class PreAuthorizeAspect {
{
@Autowired @Autowired
private TokenService tokenService; private TokenService tokenService;
/** 所有权限标识 */ /**
* 所有权限标识
*/
private static final String ALL_PERMISSION = "*:*:*"; private static final String ALL_PERMISSION = "*:*:*";
/** 管理员角色权限标识 */ /**
* 管理员角色权限标识
*/
private static final String SUPER_ADMIN = "admin"; private static final String SUPER_ADMIN = "admin";
/** 数组为0时 */ /**
* 数组为0时
*/
private static final Integer ARRAY_EMPTY = 0; private static final Integer ARRAY_EMPTY = 0;
@Around("@annotation(com.ruoyi.common.security.annotation.PreAuthorize)") @Around("@annotation(com.ruoyi.common.security.annotation.PreAuthorize)")
public Object around(ProceedingJoinPoint point) throws Throwable public Object around(ProceedingJoinPoint point) throws Throwable {
{
Signature signature = point.getSignature(); Signature signature = point.getSignature();
MethodSignature methodSignature = (MethodSignature) signature; MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod(); Method method = methodSignature.getMethod();
PreAuthorize annotation = method.getAnnotation(PreAuthorize.class); PreAuthorize annotation = method.getAnnotation(PreAuthorize.class);
if (annotation == null) if (annotation == null) {
{
return point.proceed(); return point.proceed();
} }
if (!StringUtils.isEmpty(annotation.hasPermi())) if (!StringUtils.isEmpty(annotation.hasPermi())) {
{ if (hasPermi(annotation.hasPermi())) {
if (hasPermi(annotation.hasPermi()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
} } else if (!StringUtils.isEmpty(annotation.lacksPermi())) {
else if (!StringUtils.isEmpty(annotation.lacksPermi())) if (lacksPermi(annotation.lacksPermi())) {
{
if (lacksPermi(annotation.lacksPermi()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
} } else if (ARRAY_EMPTY < annotation.hasAnyPermi().length) {
else if (ARRAY_EMPTY < annotation.hasAnyPermi().length) if (hasAnyPermi(annotation.hasAnyPermi())) {
{
if (hasAnyPermi(annotation.hasAnyPermi()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
} } else if (!StringUtils.isEmpty(annotation.hasRole())) {
else if (!StringUtils.isEmpty(annotation.hasRole())) if (hasRole(annotation.hasRole())) {
{
if (hasRole(annotation.hasRole()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
} } else if (!StringUtils.isEmpty(annotation.lacksRole())) {
else if (!StringUtils.isEmpty(annotation.lacksRole())) if (lacksRole(annotation.lacksRole())) {
{
if (lacksRole(annotation.lacksRole()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
} } else if (ARRAY_EMPTY < annotation.hasAnyRoles().length) {
else if (ARRAY_EMPTY < annotation.hasAnyRoles().length) if (hasAnyRoles(annotation.hasAnyRoles())) {
{
if (hasAnyRoles(annotation.hasAnyRoles()))
{
return point.proceed(); return point.proceed();
} }
throw new PreAuthorizeException(); throw new PreAuthorizeException();
@ -104,15 +91,13 @@ public class PreAuthorizeAspect
/** /**
* 验证用户是否具备某权限 * 验证用户是否具备某权限
* *
* @param permission 权限字符串 * @param permission 权限字符串
* @return 用户是否具备某权限 * @return 用户是否具备某权限
*/ */
public boolean hasPermi(String permission) public boolean hasPermi(String permission) {
{
LoginUser userInfo = tokenService.getLoginUser(); LoginUser userInfo = tokenService.getLoginUser();
if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) {
{
return false; return false;
} }
return hasPermissions(userInfo.getPermissions(), permission); return hasPermissions(userInfo.getPermissions(), permission);
@ -124,8 +109,7 @@ public class PreAuthorizeAspect
* @param permission 权限字符串 * @param permission 权限字符串
* @return 用户是否不具备某权限 * @return 用户是否不具备某权限
*/ */
public boolean lacksPermi(String permission) public boolean lacksPermi(String permission) {
{
return hasPermi(permission) != true; return hasPermi(permission) != true;
} }
@ -135,18 +119,14 @@ public class PreAuthorizeAspect
* @param permissions 权限列表 * @param permissions 权限列表
* @return 用户是否具有以下任意一个权限 * @return 用户是否具有以下任意一个权限
*/ */
public boolean hasAnyPermi(String[] permissions) public boolean hasAnyPermi(String[] permissions) {
{
LoginUser userInfo = tokenService.getLoginUser(); LoginUser userInfo = tokenService.getLoginUser();
if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getPermissions())) {
{
return false; return false;
} }
Collection<String> authorities = userInfo.getPermissions(); Collection<String> authorities = userInfo.getPermissions();
for (String permission : permissions) for (String permission : permissions) {
{ if (permission != null && hasPermissions(authorities, permission)) {
if (permission != null && hasPermissions(authorities, permission))
{
return true; return true;
} }
} }
@ -155,21 +135,17 @@ public class PreAuthorizeAspect
/** /**
* 判断用户是否拥有某个角色 * 判断用户是否拥有某个角色
* *
* @param role 角色字符串 * @param role 角色字符串
* @return 用户是否具备某角色 * @return 用户是否具备某角色
*/ */
public boolean hasRole(String role) public boolean hasRole(String role) {
{
LoginUser userInfo = tokenService.getLoginUser(); LoginUser userInfo = tokenService.getLoginUser();
if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) {
{
return false; return false;
} }
for (String roleKey : userInfo.getRoles()) for (String roleKey : userInfo.getRoles()) {
{ if (SUPER_ADMIN.equals(roleKey) || roleKey.equals(role)) {
if (SUPER_ADMIN.equals(roleKey) || roleKey.equals(role))
{
return true; return true;
} }
} }
@ -182,8 +158,7 @@ public class PreAuthorizeAspect
* @param role 角色名称 * @param role 角色名称
* @return 用户是否不具备某角色 * @return 用户是否不具备某角色
*/ */
public boolean lacksRole(String role) public boolean lacksRole(String role) {
{
return hasRole(role) != true; return hasRole(role) != true;
} }
@ -193,17 +168,13 @@ public class PreAuthorizeAspect
* @param roles 角色列表 * @param roles 角色列表
* @return 用户是否具有以下任意一个角色 * @return 用户是否具有以下任意一个角色
*/ */
public boolean hasAnyRoles(String[] roles) public boolean hasAnyRoles(String[] roles) {
{
LoginUser userInfo = tokenService.getLoginUser(); LoginUser userInfo = tokenService.getLoginUser();
if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) if (StringUtils.isEmpty(userInfo) || CollectionUtils.isEmpty(userInfo.getRoles())) {
{
return false; return false;
} }
for (String role : roles) for (String role : roles) {
{ if (hasRole(role)) {
if (hasRole(role))
{
return true; return true;
} }
} }
@ -212,13 +183,12 @@ public class PreAuthorizeAspect
/** /**
* 判断是否包含权限 * 判断是否包含权限
* *
* @param authorities 权限列表 * @param authorities 权限列表
* @param permission 权限字符串 * @param permission 权限字符串
* @return 用户是否具备某权限 * @return 用户是否具备某权限
*/ */
private boolean hasPermissions(Collection<String> authorities, String permission) private boolean hasPermissions(Collection<String> authorities, String permission) {
{
return authorities.stream().filter(StringUtils::hasText) return authorities.stream().filter(StringUtils::hasText)
.anyMatch(x -> ALL_PERMISSION.contains(x) || PatternMatchUtils.simpleMatch(permission, x)); .anyMatch(x -> ALL_PERMISSION.contains(x) || PatternMatchUtils.simpleMatch(permission, x));
} }

View File

@ -3,6 +3,8 @@ package com.ruoyi.system.controller;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -23,20 +25,18 @@ import com.ruoyi.system.service.ISysOperLogService;
/** /**
* 操作日志记录 * 操作日志记录
* *
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
@RequestMapping("/operlog") @RequestMapping("/operlog")
public class SysOperlogController extends BaseController public class SysOperlogController extends BaseController {
{
@Autowired @Autowired
private ISysOperLogService operLogService; private ISysOperLogService operLogService;
@PreAuthorize(hasPermi = "system:operlog:list") @PreAuthorize(hasPermi = "system:operlog:list")
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo list(SysOperLog operLog) public TableDataInfo list(SysOperLog operLog) {
{
startPage(); startPage();
List<SysOperLog> list = operLogService.selectOperLogList(operLog); List<SysOperLog> list = operLogService.selectOperLogList(operLog);
return getDataTable(list); return getDataTable(list);
@ -45,8 +45,7 @@ public class SysOperlogController extends BaseController
@Log(title = "操作日志", businessType = BusinessType.EXPORT) @Log(title = "操作日志", businessType = BusinessType.EXPORT)
@PreAuthorize(hasPermi = "system:operlog:export") @PreAuthorize(hasPermi = "system:operlog:export")
@PostMapping("/export") @PostMapping("/export")
public void export(HttpServletResponse response, SysOperLog operLog) throws IOException public void export(HttpServletResponse response, SysOperLog operLog) throws IOException {
{
List<SysOperLog> list = operLogService.selectOperLogList(operLog); List<SysOperLog> list = operLogService.selectOperLogList(operLog);
ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class); ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
util.exportExcel(response, list, "操作日志"); util.exportExcel(response, list, "操作日志");
@ -54,23 +53,20 @@ public class SysOperlogController extends BaseController
@PreAuthorize(hasPermi = "system:operlog:remove") @PreAuthorize(hasPermi = "system:operlog:remove")
@DeleteMapping("/{operIds}") @DeleteMapping("/{operIds}")
public AjaxResult remove(@PathVariable Long[] operIds) public AjaxResult remove(@PathVariable Long[] operIds) {
{
return toAjax(operLogService.deleteOperLogByIds(operIds)); return toAjax(operLogService.deleteOperLogByIds(operIds));
} }
@PreAuthorize(hasPermi = "system:operlog:remove") @PreAuthorize(hasPermi = "system:operlog:remove")
@Log(title = "操作日志", businessType = BusinessType.CLEAN) @Log(title = "操作日志", businessType = BusinessType.CLEAN)
@DeleteMapping("/clean") @DeleteMapping("/clean")
public AjaxResult clean() public AjaxResult clean() {
{
operLogService.cleanOperLog(); operLogService.cleanOperLog();
return AjaxResult.success(); return AjaxResult.success();
} }
@PostMapping @PostMapping
public AjaxResult add(@RequestBody SysOperLog operLog) public AjaxResult add(@RequestBody SysOperLog operLog) {
{
return toAjax(operLogService.insertOperlog(operLog)); return toAjax(operLogService.insertOperlog(operLog));
} }
} }

View File

@ -1,5 +1,5 @@
server: server:
port: 8091 port: 8092
spring: spring:
application: application:
name: servier-order name: servier-order