1. 程式人生 > >Shiro之前後端分離時獲取請求頭Authorization中的token

Shiro之前後端分離時獲取請求頭Authorization中的token

  • 前後端分離將從請求頭中獲取sesssionid
    */
    @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);
    }
    }