给Kkfileview加请求头鉴权接入

所有接入前端token放localStorage, 或者后端cookie中获取鉴权

本案例以放localStorage为例
一、创建global.js

$(document).ready(function() {
    // 设置全局的 AJAX 请求头
    $.ajaxSetup({
        headers: {
            'Authentication': localStorage.getItem('Authentication') 
        }
    });
});

二。全部模板都引入

给Kkfileview加请求头鉴权接入_第1张图片

三、修改window.open()

// 在每次请求时读取认证信息
        fetch('${baseUrl}onlinePreview?url=' + encodeURIComponent(b64Encoded), {
            headers: {
                'Authentication': localStorage.getItem('Authentication') // 从 localStorage 中读取
            }
        })
       .then(response => response.text())
       .then(data => {
           var newWindow = window.open(); // 打开新窗口
           if (!newWindow) {
               console.error('Failed to open new window. It might be blocked by the browser.');
               return;
           }
           newWindow.document.write(data); // 写入内容
           newWindow.document.close(); // 结束写入
           newWindow.location.replace('${baseUrl}onlinePreview?url=' + encodeURIComponent(b64Encoded)); // 强制更新 URL
       })
       .catch(error => {
           console.error('Fetch error:', error); // 打印错误信息
       });

给Kkfileview加请求头鉴权接入_第2张图片

你可能感兴趣的:(okhttp,kkfileview)