Spring Boot+angularjs because of cross-domain causes session loss

Source: Internet
Author: User

http://blog.csdn.net/dalangzhonghangxing/article/details/52446821

If you're still having trouble with cross-domain issues, check out the blogger's resolution angular+spring boot cross-domain issue that article.

Bloggers in the project development process, encountered due to cross-domain caused by the loss of the session, very disgusting, but after the online access to a variety of information, found that the solution is very simple.

In each of our data requests, the browser sends a jsession to the background, which automatically finds the session object with ID jsession based on this value. However, when cross-domain, the value of the jsession will change each time, the background will assume that a new session opened, and then to create a new object, and the original session object can not be found.

The solution to this problem is simply to set Withcredentials to true in the request, then the value of the cookie in the header of each request, and the Jsession value is recorded in the cookie, so the session is not lost.

In Angularjs, the global setting can be used to make withcredentials true

[JavaScript]View PlainCopy  
    1. var utils = angular.module (' utils ', []);
    2. Utils.config ([' $httpProvider ', Config]);
    3. function Config ($httpProvider) {
    4. $httpProvider. defaults.withcredentials = true;
    5. }

Then let the "Utils" be injected into all the apps, and the withcredentials in all the request headers will be set to true so that the session will not be lost.

Of course, you can also set the withcredentials of the current request in the following ways.

[JavaScript]View PlainCopy 
    1. $http. xhr.withcredentials

Spring Boot+angularjs because of cross-domain causes session loss

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.