SAP Spartacus 用户请求中的 current id 是从哪里来的
从 SAP Spartacus 成功登录之后,能在 Local Storage 里观察到 userId 为 current:
![](http://pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2ltYWdlMTA5LjM2MGRvYy5jbi9Eb3dubG9hZEltZy8yMDIxLzA4LzA3MTQvMjI3ODI4MTA1XzFfMjAyMTA4MDcwMjQzNTI1ODIuanBn.jpg)
这个 current id 从哪里来的呢?在 Spartacus 应用初始化的时候,从浏览器的 local storage 里取的。参考下图 syncWithStorage 方法:
![](http://pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2ltYWdlMTA5LjM2MGRvYy5jbi9Eb3dubG9hZEltZy8yMDIxLzA4LzA3MTQvMjI3ODI4MTA1XzJfMjAyMTA4MDcwMjQzNTI2OTEuanBn.jpg)
取到之后,设置到内存中:this.userIdService.setUserID:
![](http://pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2ltYWdlMTA5LjM2MGRvYy5jbi9Eb3dubG9hZEltZy8yMDIxLzA4LzA3MTQvMjI3ODI4MTA1XzNfMjAyMTA4MDcwMjQzNTI4MDAuanBn.jpg)
手动清除浏览器 Local Storage 之后,F5 刷新页面:
![](http://pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2ltYWdlMTA5LjM2MGRvYy5jbi9Eb3dubG9hZEltZy8yMDIxLzA4LzA3MTQvMjI3ODI4MTA1XzRfMjAyMTA4MDcwMjQzNTI4NjMuanBn.jpg)
此时 userId 被重新设置成 anonymous:
![](http://pic.ikafan.com/imgp/L3Byb3h5L2h0dHBzL2ltYWdlMTA5LjM2MGRvYy5jbi9Eb3dubG9hZEltZy8yMDIxLzA4LzA3MTQvMjI3ODI4MTA1XzVfMjAyMTA4MDcwMjQzNTMzNS5qcGc=.jpg)
anomymous 在 Spartacus 的设置里是一个常量:OCC_USER_ID_ANONYMOUS:
![](http://n4.ikafan.com/assetsj/blank.gif)
点了 sign in 之后:
![](http://n4.ikafan.com/assetsj/blank.gif)
使用用户输入的用户名,密码,以及 Spartacus 应用的 client_id 和 client_secret, 去换取 Access Token:
![](http://n4.ikafan.com/assetsj/blank.gif)
换取 token 的 url 为:
https://20.83.184.244:9002/authorizationserver/oauth/token
![](http://n4.ikafan.com/assetsj/blank.gif)
换取 token 之后,当前用户名被硬编码成 current,即常量 OCC_USER_ID_CURRENT:
![](http://n4.ikafan.com/assetsj/blank.gif)
赞 (0)