uni-app HarmonyOS NEXT基于CAS身份认证失败问题,302重定向后cookies丢失问题

uni-app HarmonyOS NEXT基于CAS身份认证失败问题,302重定向后cookies丢失问题

开发环境 版本号 项目创建方式
Windows win11 HBuilderX

操作步骤:

CAS身份认证 重定向后不携带cookie,无法完成身份认证。

预期结果:

正常认证携带cookie

实际结果:

重向定的请求未携带

bug描述:

发布日志:4.36.2024112817 App-HarmonyOS平台 新增 网络请求相关接口持久化保存 cookie,并且发送请求时会自动携带
但是通过抓包工具,正常请求的是携带的,但是302重定向后再请求的不携带cookie。
android之前报过的参考https://ask.dcloud.net.cn/question/106697

大概率是302状态下Set-Cookie的值未设置。

相关链接:


更多关于uni-app HarmonyOS NEXT基于CAS身份认证失败问题,302重定向后cookies丢失问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

更新: 这个问题目前是鸿蒙未开放相关功能,无法中断手动控制,你可以参考下面逻辑。
目前鸿蒙的 httpRequest 方法没有暴露手动处理重定向的相关逻辑,不能在跳转过程中做额外处理。跳转携带 header 不是默认行为,你可以使用下面的 uts 插件来兼容鸿蒙。
在安卓中能手动控制跳转的逻辑,所以可以直接修改,鸿蒙上需要临时这样来做。
这里用到了底层的 rcp 来手动控制跳转逻辑,手动处理 cookie 和跳转逻辑
import { rcp } from ‘@kit.RemoteCommunicationKit’;
import Rcp from ‘@hms.collaboration.rcp’

export const handleRedirectFetch = async (url : string, cookieKey : string, cookieValue : string) => {
const config : Rcp.RequestCookies = {
// [cookieKey]: cookieValue
};
config[cookieKey] = cookieValue

const sessionConfig : rcp.SessionConfiguration = {
requestConfiguration: {
transfer: {
autoRedirect: false,
},
},
headers: {
“Content-Type”: “application/json”
},
cookies: config
};

const session = rcp.createSession(sessionConfig);
await session.get(url).then((res : Rcp.Response) => {

if (res.statusCode >= 300 && res.statusCode < 400) {  
  console.log(String(res.statusCode))  
  // if 302 手动跳转  
  const location = res.headers.location  

  if (location) {  
    handleRedirectFetch(location, cookieKey, cookieValue)  
  }  

} else {  
  console.log(JSON.stringify(res));  
}  

})
} 私聊提供复现工程,说明操作步骤吧

更多关于uni-app HarmonyOS NEXT基于CAS身份认证失败问题,302重定向后cookies丢失问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


已私信,经初步分析是302重定向可能没再次经过uni.request请求,直接由鸿蒙的http接口接管了,所以设置cookie就请求了。

回复 e***@163.com: 我更新下 uts 解决方案

如果仍有问题,你可以进一步交流,目前问题归类为 bug 已修复

回到顶部