第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Spring容器 service層獲取當(dāng)前登錄用戶(hù)信息?

如何在 Spring容器 service層獲取當(dāng)前登錄用戶(hù)信息?

慕斯王 2019-03-01 10:29:17
用戶(hù)信息綁定會(huì)話(huà),怎樣才可以在服務(wù)層注入?
查看完整描述

7 回答

?
紅顏莎娜

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊

如果不想在controller拿到用戶(hù)信息傳到service層,直接在service層也是可以拿到的。
spring mvc在處理請(qǐng)求的時(shí)候,會(huì)把請(qǐng)求對(duì)象放到RequestContextHolder持有的ThreadLocal對(duì)象中,你可以去看看DispatcherServlet類(lèi)的源代碼。
在service層可以按照如下代碼獲取:

//獲取到當(dāng)前線(xiàn)程綁定的請(qǐng)求對(duì)象
HttpServletRequest?request?=?((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
//已經(jīng)拿到session,就可以拿到session中保存的用戶(hù)信息了。
????????System.out.println(request.getSession().getAttribute("userInfo"));
查看完整回答
反對(duì) 回復(fù) 2019-03-01
?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

設(shè)計(jì)不合理,用戶(hù)信息應(yīng)該在Controller層獲取,然后傳入Service層使用。

查看完整回答
反對(duì) 回復(fù) 2019-03-01
?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        ShiroUser user = null;
        if (req != null) {
            user = (ShiroUser) req.getSession().getAttribute("currentUser");
        }

所以你想再service層獲取的是request 對(duì)不對(duì)?。?/p>

查看完整回答
反對(duì) 回復(fù) 2019-03-01
?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

SecurityContextHolder 可以處理登錄用戶(hù)的存取,

UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
        userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource()
        .buildDetails(httpRequest));
SecurityContextHolder.getContext().setAuthentication(authentication);
UsernamePasswordAuthenticationToken authentication = SecurityContextHolder.getContext().getAuthentication();
查看完整回答
反對(duì) 回復(fù) 2019-03-01
?
三國(guó)紛爭(zhēng)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

ThreadLocal?

查看完整回答
反對(duì) 回復(fù) 2019-03-01
?
阿波羅的戰(zhàn)車(chē)

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

比較簡(jiǎn)單方法就是頁(yè)面直接將用戶(hù)信息傳入servlet然后顯示到頁(yè)面上
ActionContext.getContext().put("name", name);
${username }

查看完整回答
反對(duì) 回復(fù) 2019-03-01
  • 7 回答
  • 0 關(guān)注
  • 3057 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)