Shiro之前後端分離時獲取請求頭Authorization中的token
阿新 • • 發佈:2018-12-21
*/
@RequestMapping(value = “/getSessionId”,method= RequestMethod.POST)
@ResponseBody
@Override
protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
// 從請求頭中獲取token
String token = WebUtils.toHttp(request).getHeader(“Authorization”);
// 判斷是否有值
if (token != null && token.length()>0) {
// 設定當前session狀態
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE, “url”);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID, token);
request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_IS_VALID, Boolean.TRUE);
return token;
}
// 若header獲取不到token則嘗試從cookie中獲取
return super.getSessionId(request, response);
}
}