遇到的問題:
項目開發中,遇到一個bug,該bug是多次刷新頁面。就會偶現404頁面現象,404是因爲後臺根據登錄角色查詢權限未查出來。頁面菜單路由是根據權限配置顯示,後臺查詢權限接口:
JSONArray jsonArray = new JSONArray();
OAuth2Authentication authentication = (OAuth2Authentication) principal;
authentication.getAuthorities().parallelStream().forEach(ga -> jsonArray.add((ga.getAuthority())));
經過排查,發現是parallelStream()存在問題,底層使用arrayList,該list是線程不安全的。多線程情況下,會出現數據丟失的問題。
解決方案:
修改parallelStream()方法,1:直接用foreach。2:list.stream()
authentication.getAuthorities().forEach(ga -> jsonArray.add((ga.getAuthority())));
或者
authentication.getAuthorities().stream().forEach(ga -> jsonArray.add((ga.getAuthority())));
關於parallelStream詳情,可以瀏覽
https://www.jianshu.com/p/17d432f211f4