跳到主要内容

第18篇 权限体系对接机制:Token认证+功能级控制怎么做?

在企业级系统集成中,权限控制是确保系统安全性的核心要素。星云低代码中间件通过创新的Token认证+功能级权限控制机制,实现了与现有系统的无缝权限对接,为企业提供从页面到字段的全方位安全防护。

一、多系统统一认证:基于Token的无缝身份验证

1. Token传递与验证机制

星云低代码采用标准的JWT Token进行身份认证,实现与现有系统的单点登录(SSO):

// Token验证服务 - 与现有系统对接
@Service
public class TokenAuthenticationService {

/**
* 验证Token并获取用户信息
*/
public UserInfo validateToken(String token) {
// 调用现有系统的Token验证接口
ResponseEntity<AuthResponse> response = restTemplate.postForEntity(
existingSystemConfig.getAuthUrl(),
new AuthRequest(token),
AuthResponse.class
);

if (response.getStatusCode().is2xxSuccessful() &&
response.getBody().isValid()) {
return convertToUserInfo(response.getBody());
}
throw new AuthenticationException("Token验证失败");
}

/**
* 星云低代码Token生成(用于内部传递)
*/
public String generateInternalToken(UserInfo userInfo) {
return Jwts.builder()
.setSubject(userInfo.getUserId())
.claim("tenantId", userInfo.getTenantId())
.claim("roles", userInfo.getRoles())
.setExpiration(new Date(System.currentTimeMillis() + 3600000))
.signWith(SignatureAlgorithm.HS512, internalSecret)
.compact();
}
}

2. 前端Token传递方案

// 前端Token传递机制
class TokenManager {
// 从主系统获取Token
static getMainSystemToken() {
return localStorage.getItem('main_system_token') ||
this.getTokenFromParentWindow();
}

// 跨窗口Token传递
static getTokenFromParentWindow() {
try {
return window.parent.getAuthToken?.() ||
window.opener?.getAuthToken?.();
} catch (e) {
console.warn('无法从父窗口获取Token');
return null;
}
}

// 设置星云低代码Token
static setupLowCodeToken() {
const mainToken = this.getMainSystemToken();
if (mainToken) {
// 将Token传递给低代码系统
axios.defaults.headers.common['Authorization'] = `Bearer ${mainToken}`;

// 设置低代码iframe的Token
const lowCodeFrame = document.getElementById('lowcode-frame');
if (lowCodeFrame) {
lowCodeFrame.src = `${lowCodeFrame.src}?token=${encodeURIComponent(mainToken)}`;
}
}
}
}

// 页面加载时初始化Token
document.addEventListener('DOMContentLoaded', () => {
TokenManager.setupLowCodeToken();
});

二、接口级权限控制:精细化API访问管理

1. 权限拦截器实现

// 接口级权限拦截器
@Component
public class ApiPermissionInterceptor implements HandlerInterceptor {

@Autowired
private PermissionService permissionService;

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

// 提取用户信息
UserInfo userInfo = extractUserInfo(request);
if (userInfo == null) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
}

// 获取请求的API路径和方法
String apiPath = getApiPath(request);
String httpMethod = request.getMethod();

// 检查接口访问权限
boolean hasPermission = permissionService.checkApiPermission(
userInfo.getUserId(), apiPath, httpMethod);

if (!hasPermission) {
response.setStatus(HttpStatus.FORBIDDEN.value());
response.getWriter().write("{\"error\": \"接口访问权限不足\"}");
return false;
}

return true;
}

private UserInfo extractUserInfo(HttpServletRequest request) {
String token = request.getHeader("Authorization");
if (token != null && token.startsWith("Bearer ")) {
return tokenAuthenticationService.validateToken(
token.substring(7));
}
return null;
}
}

2. 动态权限规则配置

# 接口权限规则配置
api_permissions:
- resource: "/api/v1/users/**"
methods: ["GET", "POST", "PUT"]
required_roles: ["ADMIN", "USER_MANAGER"]
conditions:
- "user.tenantId == target.tenantId"

- resource: "/api/v1/orders/**"
methods: ["GET"]
required_roles: ["SALES", "CUSTOMER_SERVICE"]
conditions:
- "user.department == 'sales'"

- resource: "/api/v1/financial/**"
methods: ["GET", "POST", "PUT", "DELETE"]
required_roles: ["FINANCE_MANAGER"]
data_scope: "SELF_DEPARTMENT"

三、按钮级权限控制:精细化操作权限管理

1. 权限指令实现(Vue示例)

// 按钮级权限指令
const permissionDirective = {
inserted: function (el, binding) {
const { value } = binding;
const userPermissions = store.getters.permissions;

if (value && value instanceof Array && value.length > 0) {
const requiredPermissions = value;
const hasPermission = userPermissions.some(permission => {
return requiredPermissions.includes(permission);
});

if (!hasPermission && el.parentNode) {
el.parentNode.removeChild(el);
}
} else {
throw new Error(`需要权限配置! 例如: v-permission="['user:add']"`);
}
}
};

// 注册指令
Vue.directive('permission', permissionDirective);

// 使用方法
<template>
<div>
<button v-permission="['user:add']">新增用户</button>
<button v-permission="['user:edit']">编辑用户</button>
<button v-permission="['user:delete']">删除用户</button>
<button v-permission="['user:export']">导出数据</button>
</div>
</template>

2. 星云低代码按钮权限配置

{
"component": "button",
"props": {
"type": "primary",
"text": "审批通过"
},
"permissions": {
"required": ["approval:pass"],
"conditions": [
"{{currentUser.department}} === 'MANAGEMENT'",
"{{currentUser.role}} in ['MANAGER', 'DIRECTOR']"
],
"data_scope": "SELF_SUBORDINATE"
},
"events": {
"click": {
"action": "api_call",
"api": "/api/approval/pass",
"params": {
"orderId": "{{selectedOrder.id}}",
"approver": "{{currentUser.id}}"
}
}
}
}

四、字段级权限控制:数据粒度权限管理

1. 动态表单字段权限

// 字段级权限控制器
class FieldLevelPermission {
static getVisibleFields(userInfo, formConfig) {
return formConfig.fields.filter(field => {
return this.checkFieldPermission(userInfo, field);
});
}

static checkFieldPermission(userInfo, field) {
// 检查角色权限
if (field.permissions?.roles) {
const hasRole = field.permissions.roles.some(role =>
userInfo.roles.includes(role));
if (!hasRole) return false;
}

// 检查数据权限
if (field.permissions?.conditions) {
return this.evaluateConditions(userInfo, field.permissions.conditions);
}

return true;
}

static evaluateConditions(userInfo, conditions) {
// 执行条件表达式
return conditions.every(condition => {
const expression = condition
.replace(/user\./g, 'userInfo.')
.replace(/currentUser\./g, 'userInfo.');
try {
return eval(expression);
} catch (e) {
console.error('条件执行错误:', e);
return false;
}
});
}
}

// 使用示例
const formConfig = {
fields: [
{
name: "salary",
label: "薪资信息",
type: "number",
permissions: {
roles: ["HR", "MANAGER"],
conditions: ["user.department === 'HR'"]
}
},
{
name: "performance",
label: "绩效评价",
type: "text",
permissions: {
roles: ["MANAGER"],
conditions: ["user.role === 'MANAGER'"]
}
}
]
};

2. 星云低代码字段权限配置

# 字段级权限配置示例
user_form:
fields:
- name: "basic_info"
type: "section"
label: "基础信息"
permissions:
roles: ["ALL"]

- name: "username"
type: "text"
label: "用户名"
permissions:
roles: ["ALL"]
read_only_roles: ["GUEST"]

- name: "email"
type: "email"
label: "邮箱"
permissions:
roles: ["USER", "ADMIN"]

- name: "salary"
type: "number"
label: "薪资"
permissions:
roles: ["HR", "ADMIN"]
conditions:
- "user.department == 'HR'"
visibility: "HIDDEN" # HIDDEN, READONLY, EDITABLE

- name: "department"
type: "select"
label: "部门"
permissions:
roles: ["ADMIN", "MANAGER"]
options_scope: "SELF_DEPARTMENT"

五、统一权限管理中心

1. 权限数据模型

// 统一权限模型
@Entity
@Table(name = "permission_center")
public class Permission {
@Id
private String id;

// 权限标识
private String code;
private String name;
private PermissionType type; // API, BUTTON, FIELD, MENU

// 权限规则
private String resourcePattern;
private String httpMethods;
private String conditions;

// 数据权限
private DataScope dataScope;
private String customDataFilter;

// 生效配置
private Boolean enabled;
private Date effectiveTime;
private Date expiryTime;
}

// 数据权限范围枚举
public enum DataScope {
ALL, // 所有数据
SELF, // 本人数据
SELF_DEPARTMENT, // 本部门数据
SELF_AND_SUB, // 本人及下属数据
CUSTOM // 自定义范围
}

2. 权限同步机制

// 权限同步服务
@Service
public class PermissionSyncService {

/**
* 从主系统同步权限数据
*/
@Scheduled(fixedRate = 300000) // 5分钟同步一次
public void syncPermissionsFromMainSystem() {
try {
List<MainSystemPermission> mainPermissions =
mainSystemClient.getLatestPermissions();

List<Permission> lowCodePermissions =
convertToLowCodePermissions(mainPermissions);

permissionRepository.saveAll(lowCodePermissions);

log.info("权限数据同步完成,共同步{}条权限记录",
lowCodePermissions.size());

} catch (Exception e) {
log.error("权限数据同步失败", e);
}
}

/**
* 实时权限更新
*/
@EventListener
public void handlePermissionUpdate(PermissionUpdateEvent event) {
// 清除相关缓存
permissionCache.evictUserPermissions(event.getUserId());

// 通知前端权限变更
websocketService.sendMessage(
event.getUserId(),
new PermissionChangeMessage()
);
}
}

六、完整对接流程示例

1. 系统初始化配置

[图片]

2. 完整权限验证流程

[图片]

七、最佳实践与性能优化

1. 权限缓存策略

// 权限缓存服务
@Service
@CacheConfig(cacheNames = "permissions")
public class PermissionCacheService {

@Cacheable(key = "'user_permissions:' + #userId")
public List<Permission> getUserPermissions(String userId) {
return permissionRepository.findByUserId(userId);
}

@Cacheable(key = "'api_permissions:' + #apiPath + ':' + #method")
public List<Permission> getApiPermissions(String apiPath, String method) {
return permissionRepository.findByResourceAndMethod(apiPath, method);
}

@CacheEvict(key = "'user_permissions:' + #userId")
public void evictUserPermissions(String userId) {
// 清除用户权限缓存
}
}

2. 权限性能监控

// 权限性能监控
@Aspect
@Component
public class PermissionPerformanceAspect {

@Around("@annotation(RequiresPermissions)")
public Object monitorPermissionPerformance(ProceedingJoinPoint joinPoint)
throws Throwable {
long startTime = System.currentTimeMillis();
try {
return joinPoint.proceed();
} finally {
long duration = System.currentTimeMillis() - startTime;
Metrics.timer("permission.check.duration").record(duration);

if (duration > 100) { // 超过100ms记录警告
log.warn("权限检查耗时过长: {}ms", duration);
}
}
}
}

结论:构建企业级统一权限体系

星云低代码通过Token认证 + 功能级权限控制的完整解决方案,实现了:

安全可靠性:

  • 基于JWT的标准Token认证机制
  • 多层次、细粒度的权限控制
  • 实时权限同步与更新

灵活扩展性:

  • 支持多系统统一认证
  • 可配置的权限规则引擎
  • 动态权限调整能力

性能高效性:

  • 多级缓存策略减少数据库压力
  • 异步权限同步机制
  • 实时性能监控与优化

这种权限体系不仅满足了企业级应用的安全要求,更为复杂业务场景下的权限管理提供了完整的技术支撑,是星云低代码在企业级市场中的核心竞争优势。