- 使用 ReturnT.ofFail() 替代 new ReturnT<String>(ReturnT.FAIL_CODE, ...) 来返回错误信息 - 将权限验证相关方法移至 JobGroupPermissionUtil工具类中 - 优化了多个控制器中的错误处理和权限验证逻辑 - 统一了错误消息的返回格式3.2.0-release
parent
be44018394
commit
6fa72373f2
@ -1,98 +0,0 @@
|
|||||||
package com.xxl.job.admin.util;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.Cookie;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cookie.Util
|
|
||||||
*
|
|
||||||
* @author xuxueli 2015-12-12 18:01:06
|
|
||||||
*/
|
|
||||||
public class CookieUtil {
|
|
||||||
|
|
||||||
// 默认缓存时间,单位/秒, 2H
|
|
||||||
private static final int COOKIE_MAX_AGE = Integer.MAX_VALUE;
|
|
||||||
// 保存路径,根路径
|
|
||||||
private static final String COOKIE_PATH = "/";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存
|
|
||||||
*
|
|
||||||
* @param response
|
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
* @param ifRemember
|
|
||||||
*/
|
|
||||||
public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
|
|
||||||
int age = ifRemember?COOKIE_MAX_AGE:-1;
|
|
||||||
set(response, key, value, null, COOKIE_PATH, age, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保存
|
|
||||||
*
|
|
||||||
* @param response
|
|
||||||
* @param key
|
|
||||||
* @param value
|
|
||||||
* @param maxAge
|
|
||||||
*/
|
|
||||||
private static void set(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
|
|
||||||
Cookie cookie = new Cookie(key, value);
|
|
||||||
if (domain != null) {
|
|
||||||
cookie.setDomain(domain);
|
|
||||||
}
|
|
||||||
cookie.setPath(path);
|
|
||||||
cookie.setMaxAge(maxAge);
|
|
||||||
cookie.setHttpOnly(isHttpOnly);
|
|
||||||
response.addCookie(cookie);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询value
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @param key
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String getValue(HttpServletRequest request, String key) {
|
|
||||||
Cookie cookie = get(request, key);
|
|
||||||
if (cookie != null) {
|
|
||||||
return cookie.getValue();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询Cookie
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @param key
|
|
||||||
*/
|
|
||||||
private static Cookie get(HttpServletRequest request, String key) {
|
|
||||||
Cookie[] arr_cookie = request.getCookies();
|
|
||||||
if (arr_cookie != null && arr_cookie.length > 0) {
|
|
||||||
for (Cookie cookie : arr_cookie) {
|
|
||||||
if (cookie.getName().equals(key)) {
|
|
||||||
return cookie;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除Cookie
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @param response
|
|
||||||
* @param key
|
|
||||||
*/
|
|
||||||
public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
|
|
||||||
Cookie cookie = get(request, key);
|
|
||||||
if (cookie != null) {
|
|
||||||
set(response, key, "", null, COOKIE_PATH, 0, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.xxl.job.admin.util;
|
||||||
|
|
||||||
|
import com.xxl.job.admin.constant.Consts;
|
||||||
|
import com.xxl.job.admin.model.XxlJobGroup;
|
||||||
|
import com.xxl.sso.core.helper.XxlSsoHelper;
|
||||||
|
import com.xxl.sso.core.model.LoginInfo;
|
||||||
|
import com.xxl.tool.core.StringTool;
|
||||||
|
import com.xxl.tool.response.Response;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* jobGroup permission util
|
||||||
|
*
|
||||||
|
* @author xuxueli 2025-08-24
|
||||||
|
*/
|
||||||
|
public class JobGroupPermissionUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if has jobgroup permission
|
||||||
|
*/
|
||||||
|
public static boolean hasJobGroupPermission(LoginInfo loginInfo, int jobGroup){
|
||||||
|
if (XxlSsoHelper.hasRole(loginInfo, Consts.ADMIN_ROLE).isSuccess()) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
List<String> jobGroups = (loginInfo.getExtraInfo()!=null && loginInfo.getExtraInfo().containsKey("jobGroups"))
|
||||||
|
? List.of(StringTool.tokenizeToArray(loginInfo.getExtraInfo().get("jobGroups"), ",")) :new ArrayList<>();
|
||||||
|
return jobGroups.contains(String.valueOf(jobGroup));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* valid jobGroup permission
|
||||||
|
*/
|
||||||
|
public static LoginInfo validJobGroupPermission(HttpServletRequest request, int jobGroup) {
|
||||||
|
Response<LoginInfo> loginInfoResponse = XxlSsoHelper.loginCheckWithAttr(request);
|
||||||
|
if (!(loginInfoResponse.isSuccess() && hasJobGroupPermission(loginInfoResponse.getData(), jobGroup))) {
|
||||||
|
throw new RuntimeException(I18nUtil.getString("system_permission_limit") + "[username="+ loginInfoResponse.getData().getUserName() +"]");
|
||||||
|
}
|
||||||
|
return loginInfoResponse.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* filter jobGroupList by permission
|
||||||
|
*/
|
||||||
|
public static List<XxlJobGroup> filterJobGroupByPermission(HttpServletRequest request, List<XxlJobGroup> jobGroupListTotal){
|
||||||
|
Response<LoginInfo> loginInfoResponse = XxlSsoHelper.loginCheckWithAttr(request);
|
||||||
|
|
||||||
|
if (XxlSsoHelper.hasRole(loginInfoResponse.getData(), Consts.ADMIN_ROLE).isSuccess()) {
|
||||||
|
return jobGroupListTotal;
|
||||||
|
} else {
|
||||||
|
List<String> jobGroups = (loginInfoResponse.getData().getExtraInfo()!=null && loginInfoResponse.getData().getExtraInfo().containsKey("jobGroups"))
|
||||||
|
? List.of(StringTool.tokenizeToArray(loginInfoResponse.getData().getExtraInfo().get("jobGroups"), ",")) :new ArrayList<>();
|
||||||
|
|
||||||
|
return jobGroupListTotal
|
||||||
|
.stream()
|
||||||
|
.filter(jobGroup -> jobGroups.contains(String.valueOf(jobGroup.getId())))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
//package com.xxl.job.admin.util;
|
||||||
|
//
|
||||||
|
//import jakarta.servlet.http.Cookie;
|
||||||
|
//import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
//import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * Cookie.Util
|
||||||
|
// *
|
||||||
|
// * @author xuxueli 2015-12-12 18:01:06
|
||||||
|
// */
|
||||||
|
//public class CookieUtil {
|
||||||
|
//
|
||||||
|
// // 默认缓存时间,单位/秒, 2H
|
||||||
|
// private static final int COOKIE_MAX_AGE = Integer.MAX_VALUE;
|
||||||
|
// // 保存路径,根路径
|
||||||
|
// private static final String COOKIE_PATH = "/";
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 保存
|
||||||
|
// *
|
||||||
|
// * @param response
|
||||||
|
// * @param key
|
||||||
|
// * @param value
|
||||||
|
// * @param ifRemember
|
||||||
|
// */
|
||||||
|
// public static void set(HttpServletResponse response, String key, String value, boolean ifRemember) {
|
||||||
|
// int age = ifRemember?COOKIE_MAX_AGE:-1;
|
||||||
|
// set(response, key, value, null, COOKIE_PATH, age, true);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 保存
|
||||||
|
// *
|
||||||
|
// * @param response
|
||||||
|
// * @param key
|
||||||
|
// * @param value
|
||||||
|
// * @param maxAge
|
||||||
|
// */
|
||||||
|
// private static void set(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
|
||||||
|
// Cookie cookie = new Cookie(key, value);
|
||||||
|
// if (domain != null) {
|
||||||
|
// cookie.setDomain(domain);
|
||||||
|
// }
|
||||||
|
// cookie.setPath(path);
|
||||||
|
// cookie.setMaxAge(maxAge);
|
||||||
|
// cookie.setHttpOnly(isHttpOnly);
|
||||||
|
// response.addCookie(cookie);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 查询value
|
||||||
|
// *
|
||||||
|
// * @param request
|
||||||
|
// * @param key
|
||||||
|
// * @return
|
||||||
|
// */
|
||||||
|
// public static String getValue(HttpServletRequest request, String key) {
|
||||||
|
// Cookie cookie = get(request, key);
|
||||||
|
// if (cookie != null) {
|
||||||
|
// return cookie.getValue();
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 查询Cookie
|
||||||
|
// *
|
||||||
|
// * @param request
|
||||||
|
// * @param key
|
||||||
|
// */
|
||||||
|
// private static Cookie get(HttpServletRequest request, String key) {
|
||||||
|
// Cookie[] arr_cookie = request.getCookies();
|
||||||
|
// if (arr_cookie != null && arr_cookie.length > 0) {
|
||||||
|
// for (Cookie cookie : arr_cookie) {
|
||||||
|
// if (cookie.getName().equals(key)) {
|
||||||
|
// return cookie;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 删除Cookie
|
||||||
|
// *
|
||||||
|
// * @param request
|
||||||
|
// * @param response
|
||||||
|
// * @param key
|
||||||
|
// */
|
||||||
|
// public static void remove(HttpServletRequest request, HttpServletResponse response, String key) {
|
||||||
|
// Cookie cookie = get(request, key);
|
||||||
|
// if (cookie != null) {
|
||||||
|
// set(response, key, "", null, COOKIE_PATH, 0, true);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//}
|
Loading…
Reference in new issue