鍍金池/ 問答/Java  HTML/ Shiro 的 SessionId 是在什么時候設(shè)置到客戶端的呢?

Shiro 的 SessionId 是在什么時候設(shè)置到客戶端的呢?

場景

在使用 Cors 進行跨域登錄操作時,登陸一切正常,但客戶端并沒有添加 SessionId(Cookie 里面),不清楚發(fā)生了什么。。。

調(diào)用 js 代碼:

fetch('http://localhost:8080/RMASystem/a/embeddedLogin', {
    method: 'POST',
    body: params,
})
    .then(res => res.json())
    .then(json => console.log(json))

返回的 json 數(shù)據(jù):

{code: 200, message: "登錄成功"}

后端 Java 代碼基本如下:

/**
 * 用于內(nèi)嵌頁面進行登錄
 *
 * @return 返回一個是否成功
 */
@RequestMapping(value = "${adminPath}/embeddedLogin", method = RequestMethod.POST)
@ResponseBody
public OperationResult embeddedLogin(
        HttpServletRequest request,
        HttpServletResponse response
) {
    final AuthenticationToken token = formAuthenticationFilter.createToken(request, response);
    try {
        systemAuthorizingRealm.doGetAuthenticationInfo(token);
        return OperationResult.buildSuccessResult("登錄成功");
    } catch (Exception ex) {
        return OperationResult.buildErrorResult("登錄失敗");
    }
}

至于如何解決的跨域,只是在后臺配置了一個攔截器:

在后臺添加一個 Filter 過濾器

/**
 * 使用自定義的 Filter 攔截器實現(xiàn)跨域請求、
 * 適用于所有的 Java Web 項目并且不局限于某個框架
 * 注:此處的 @Component 僅為讓 Spring 知道這個 Bean, 不然攔截器不會加載
 *
 * @author rxliuli
 */
public class CustomCorsFilterConfig implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //允許所有來源
        String allowOrigin = "*";
        //允許以下請求方法
        String allowMethods = "GET,POST,PUT,DELETE,OPTIONS";
        //允許以下請求頭
        String allowHeaders = "Content-Type,X-Token";
        //允許有認證信息(cookie)
        String allowCredentials = "true";

        String origin = request.getHeader("Origin");
        //此處是為了兼容需要認證信息(cookie)的時候不能設(shè)置為 * 的問題
        response.setHeader("Access-Control-Allow-Origin", origin == null ? allowOrigin : origin);
        response.setHeader("Access-Control-Allow-Methods", allowMethods);
        response.setHeader("Access-Control-Allow-Credentials", allowCredentials);
        response.setHeader("Access-Control-Allow-Headers", allowHeaders);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}

web.xml 文件中添加攔截器配置(注:如果可能就配置成第一個 Filter

<!--cors 跨域訪問-->
<filter>
  <filter-name>customCorsFilterConfig</filter-name>
  <filter-class>CustomCorsFilterConfig</filter-class>
</filter>
<filter-mapping>
  <filter-name>customCorsFilterConfig</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

然而正常請求結(jié)束后并未在客戶端的 cookie 中設(shè)置 SessionId,這是為什么呢?ヽ(?_?;)ノ

回答
編輯回答
安若晴

shiro 注冊了過濾器一類的東西去操作這些,話說 session id 不是 web 容器 在負責(zé)嗎。

2017年12月18日 19:12
編輯回答
朕略萌

現(xiàn)在問題解決了,還是客戶端的請求沒有設(shè)置正確,只要需要帶上/返回 認證信息 的請求都必須添加參數(shù) credentials。

現(xiàn)在的請求大概是這樣的:

fetch('http://localhost:8089/test/setSession', {
    credentials: "include"
})
    .then(res => res.json())
    .then(json => console.log(json))
2018年4月18日 02:24