feat: 全量提交

This commit is contained in:
horizons
2022-10-24 07:50:54 +08:00
commit de9157143a
128 changed files with 7493 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package com.youlai.system.util;
import cn.hutool.json.JSONUtil;
import com.youlai.system.common.result.Result;
import com.youlai.system.common.result.ResultCode;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* response 响应工具类
*
* @author haoxr
* @date 2022/10/18
*/
public class ResponseUtils {
/**
* 异常消息返回(适用过滤器异常响应)
*
* @param response
* @param resultCode
*/
public static void writeErrMsg(HttpServletResponse response, ResultCode resultCode) {
switch (resultCode) {
case ACCESS_UNAUTHORIZED:
case TOKEN_INVALID_OR_EXPIRED:
response.setStatus(HttpStatus.UNAUTHORIZED.value());
break;
case TOKEN_ACCESS_FORBIDDEN:
response.setStatus(HttpStatus.FORBIDDEN.value());
break;
default:
response.setStatus(HttpStatus.BAD_REQUEST.value());
break;
}
response.setCharacterEncoding("UTF-8");
try {
String bodyJsonStr = JSONUtil.toJsonStr(Result.failed(resultCode));
PrintWriter printWriter = response.getWriter();
printWriter.print(bodyJsonStr);
printWriter.flush();
printWriter.close();
} catch (IOException e) {
}
}
}

View File

@@ -0,0 +1,111 @@
package com.youlai.system.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.security.userdetails.SysUserDetails;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
public class SecurityUtils {
/**
* 获取当前登录人信息
*
* @return
*/
public static SysUserDetails getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object principal = authentication.getPrincipal();
if (principal instanceof SysUserDetails) {
return (SysUserDetails) authentication.getPrincipal();
}
}
return null;
}
/**
* 获取用户角色集合
*
* @return
*/
public static Set<String> getRoles() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Set<String> roles = null;
if (CollectionUtil.isNotEmpty(authorities)) {
roles = authorities.stream().filter(item -> item.getAuthority().startsWith("ROLE_"))
.map(item -> StrUtil.removePrefix(item.getAuthority(), "ROLE_"))
.collect(Collectors.toSet());
} else {
roles = Collections.EMPTY_SET;
}
return roles;
}
/**
* 获取用户权限集合
*
* @return
*/
public static Set<String> getPerms() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Set<String> perms = null;
if (CollectionUtil.isNotEmpty(authorities)) {
perms = authorities.stream().filter(item -> !item.getAuthority().startsWith("ROLE_"))
.map(item -> item.getAuthority())
.collect(Collectors.toSet());
} else {
perms = Collections.EMPTY_SET;
}
return perms;
}
/**
* 是否超级管理员
* <p>
* 超级管理员忽视任何权限判断
*
* @return
*/
public static boolean isRoot() {
Set<String> roles = getRoles();
if (roles.contains(SystemConstants.ROOT_ROLE_CODE)) {
return true;
}
return false;
}
/**
* 是否拥有权限判断
* <p>
* 适用业务判断(接口权限判断适用Spring Security 自带注解 PreAuthorize 判断即可 )
*
* @return
*/
public static boolean hasPerm(String perm) {
if (isRoot()) {
return true;
}
Set<String> perms = getPerms();
boolean hasPerm = perms.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(perm, item));
return hasPerm;
}
}