|
|
@@ -0,0 +1,171 @@
|
|
|
+package com.jeeplus.assess.aiAssistant.service;
|
|
|
+
|
|
|
+import com.jeeplus.assess.aiAssistant.client.WeKnoraEmbedClient;
|
|
|
+import com.jeeplus.assess.aiAssistant.client.dto.EmbedExchangeResp;
|
|
|
+import com.jeeplus.assess.aiAssistant.config.WeKnoraProperties;
|
|
|
+import com.jeeplus.assess.aiAssistant.service.dto.AiChannelDTO;
|
|
|
+import com.jeeplus.assess.aiAssistant.service.dto.AiTokenDTO;
|
|
|
+import com.jeeplus.assess.aiAssistant.service.dto.AiTokenRespDTO;
|
|
|
+import com.jeeplus.common.SecurityUtils;
|
|
|
+import com.jeeplus.sys.service.dto.UserDTO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.security.access.AccessDeniedException;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 知识助手渠道目录与短期令牌service
|
|
|
+ *
|
|
|
+ * <p><b>本类故意不加 {@code @Transactional}</b>(方案 §5 第 3 条):换令牌是一次外部 HTTP 调用,
|
|
|
+ * 把它圈进事务等于让数据库连接等一个外网往返;而且审计写失败要能被吞掉而不影响发令牌,
|
|
|
+ * 一旦本类进了事务,吞异常就等于吞掉回滚。落库动作全部交给
|
|
|
+ * {@link AiAssistantAccessService}(那边单独开事务)。</p>
|
|
|
+ *
|
|
|
+ * @author: 徐滕
|
|
|
+ * @version: 2026-11-16 10:20
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AiAssistantChannelService {
|
|
|
+
|
|
|
+ /** 渠道权限标识前缀,与隐藏菜单 ai:assistant:{channelCode} 对应(方案 §7) */
|
|
|
+ private static final String PERMISSION_PREFIX = "ai:assistant:";
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeKnoraProperties weKnoraProperties;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WeKnoraEmbedClient weKnoraEmbedClient;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AiAssistantAccessService aiAssistantAccessService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前用户可见的渠道目录。
|
|
|
+ *
|
|
|
+ * <p>过滤在服务端做,前端只管显示(方案 §5 接口 1):配置里有 N 条,
|
|
|
+ * 用户有其中几条的 ai:assistant:{code},就返回几条。</p>
|
|
|
+ *
|
|
|
+ * @return 已按 sort 排序的渠道列表,没有可见渠道时返回空列表
|
|
|
+ */
|
|
|
+ public List<AiChannelDTO> listAuthorizedChannels() {
|
|
|
+ List<AiChannelDTO> list = new ArrayList<>();
|
|
|
+ for (WeKnoraProperties.Channel channel : weKnoraProperties.getChannelsSorted()) {
|
|
|
+ if (hasChannelAuthority(channel.getCode())) {
|
|
|
+ list.add(toChannelDTO(channel));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 为指定渠道换一枚 30 分钟短期令牌(前端建好 iframe 后由 bootstrap_request 触发)。
|
|
|
+ *
|
|
|
+ * <p>三道关卡:渠道必须在配置里 → 当前用户必须有该渠道权限(<b>前端传的 channelCode 不算数,
|
|
|
+ * 这里按权限码再判一次</b>,方案 §5 第 1 条)→ 才允许调 exchange。</p>
|
|
|
+ *
|
|
|
+ * @param aiTokenDTO 入参,只用 channelCode
|
|
|
+ * @return 短期令牌与建 iframe 需要的 channelId / baseUrl / pageTitle
|
|
|
+ * @throws AccessDeniedException 用户没有该渠道权限(403)
|
|
|
+ * @throws IllegalStateException 渠道未配置、或 WeKnora 侧失败
|
|
|
+ */
|
|
|
+ public AiTokenRespDTO issueToken(AiTokenDTO aiTokenDTO) {
|
|
|
+ String channelCode = aiTokenDTO == null ? null : aiTokenDTO.getChannelCode();
|
|
|
+ if (StringUtils.isBlank(channelCode)) {
|
|
|
+ throw new IllegalStateException("缺少渠道标识 channelCode");
|
|
|
+ }
|
|
|
+ WeKnoraProperties.Channel channel = weKnoraProperties.getChannel(channelCode);
|
|
|
+ if (channel == null) {
|
|
|
+ // 配置里查不到就明确报出来,别让它伪装成「没权限」,否则漏配 yml 会很难定位(方案 §7 第 4 条)
|
|
|
+ log.warn("知识助手渠道未配置 channelCode={}", channelCode);
|
|
|
+ throw new IllegalStateException("知识助手渠道未配置:" + channelCode);
|
|
|
+ }
|
|
|
+ if (!hasChannelAuthority(channel.getCode())) {
|
|
|
+ log.warn("知识助手越权换令牌被拒 channelCode={}", channelCode);
|
|
|
+ throw new AccessDeniedException("无权使用该知识助手渠道");
|
|
|
+ }
|
|
|
+ EmbedExchangeResp.EmbedExchangeData data =
|
|
|
+ weKnoraEmbedClient.exchange(channel.getChannelId(), channel.getPublishToken());
|
|
|
+ recordAccessQuietly(channel);
|
|
|
+
|
|
|
+ AiTokenRespDTO resp = new AiTokenRespDTO();
|
|
|
+ resp.setChannelId(channel.getChannelId());
|
|
|
+ resp.setToken(data.getSessionToken());
|
|
|
+ resp.setExpiresIn(data.getExpiresIn());
|
|
|
+ resp.setBaseUrl(weKnoraProperties.getBaseUrl());
|
|
|
+ resp.setPageTitle(channel.getPageTitle());
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 令牌对应的权限码是否在当前登录态里。
|
|
|
+ *
|
|
|
+ * <p>直接读 {@code SecurityContextHolder} 里的 authorities(JWT 过滤器已按权限码填好),
|
|
|
+ * 不再为这一次判断额外跑一趟用户服务。</p>
|
|
|
+ *
|
|
|
+ * @param channelCode 渠道标识
|
|
|
+ * @return true=有该渠道权限
|
|
|
+ */
|
|
|
+ public boolean hasChannelAuthority(String channelCode) {
|
|
|
+ if (StringUtils.isBlank(channelCode)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Authentication authentication = SecurityUtils.getAuthentication();
|
|
|
+ if (authentication == null || authentication.getAuthorities() == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String required = PERMISSION_PREFIX + channelCode;
|
|
|
+ for (GrantedAuthority authority : authentication.getAuthorities()) {
|
|
|
+ if (required.equals(authority.getAuthority())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 落审计,失败只记 ERROR、不打断发令牌(方案 §5 第 3 条)。
|
|
|
+ *
|
|
|
+ * <p>本方法所在的类没有事务,所以这里的 catch 不会吞掉任何回滚;
|
|
|
+ * {@code AiAssistantAccessService.record} 自带事务,失败会自己回滚干净。
|
|
|
+ * 日志只含 userId 与渠道码,不含正文、不含任何令牌。</p>
|
|
|
+ *
|
|
|
+ * @param channel 渠道配置
|
|
|
+ */
|
|
|
+ private void recordAccessQuietly(WeKnoraProperties.Channel channel) {
|
|
|
+ String userId = null;
|
|
|
+ try {
|
|
|
+ UserDTO user = SecurityUtils.getCurrentUserDTO();
|
|
|
+ userId = user == null ? null : user.getId();
|
|
|
+ aiAssistantAccessService.record(userId, user == null ? null : user.getName(),
|
|
|
+ channel, aiAssistantAccessService.currentClientIp());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("知识助手审计写入失败 userId={} channelCode={}", userId, channel.getCode(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 渠道配置转出参。baseUrl 一并给出,前端建 iframe 与 postMessage 的 targetOrigin 都用它。
|
|
|
+ *
|
|
|
+ * @param channel 渠道配置
|
|
|
+ * @return 不含令牌的渠道出参
|
|
|
+ */
|
|
|
+ private AiChannelDTO toChannelDTO(WeKnoraProperties.Channel channel) {
|
|
|
+ AiChannelDTO dto = new AiChannelDTO();
|
|
|
+ dto.setCode(channel.getCode());
|
|
|
+ dto.setName(channel.getName());
|
|
|
+ dto.setChannelId(channel.getChannelId());
|
|
|
+ dto.setBaseUrl(weKnoraProperties.getBaseUrl());
|
|
|
+ dto.setPageTitle(channel.getPageTitle());
|
|
|
+ dto.setAgentId(channel.getAgentId());
|
|
|
+ dto.setSort(channel.getSort());
|
|
|
+ dto.setIsDefault(channel.getIsDefault());
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+}
|