在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

場(chǎng)景

在使用 Cors 進(jìn)行跨域登錄操作時(shí),登陸一切正常,但客戶端并沒(méi)有添加 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)嵌頁(yè)面進(jìn)行登錄
 *
 * @return 返回一個(gè)是否成功
 */
@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("登錄失敗");
    }
}

至于如何解決的跨域,只是在后臺(tái)配置了一個(gè)攔截器:

在后臺(tái)添加一個(gè) Filter 過(guò)濾器

/**
 * 使用自定義的 Filter 攔截器實(shí)現(xiàn)跨域請(qǐng)求、
 * 適用于所有的 Java Web 項(xiàng)目并且不局限于某個(gè)框架
 * 注:此處的 @Component 僅為讓 Spring 知道這個(gè) Bean, 不然攔截器不會(huì)加載
 *
 * @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;
        //允許所有來(lái)源
        String allowOrigin = "*";
        //允許以下請(qǐng)求方法
        String allowMethods = "GET,POST,PUT,DELETE,OPTIONS";
        //允許以下請(qǐng)求頭
        String allowHeaders = "Content-Type,X-Token";
        //允許有認(rèn)證信息(cookie)
        String allowCredentials = "true";

        String origin = request.getHeader("Origin");
        //此處是為了兼容需要認(rèn)證信息(cookie)的時(shí)候不能設(shè)置為 * 的問(wèn)題
        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 文件中添加攔截器配置(注:如果可能就配置成第一個(gè) Filter

<!--cors 跨域訪問(wèn)-->
<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>

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

回答
編輯回答
安若晴

shiro 注冊(cè)了過(guò)濾器一類(lèi)的東西去操作這些,話說(shuō) session id 不是 web 容器 在負(fù)責(zé)嗎。

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

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

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

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