Solve the problem of null session caused by the front end cross domain access session ID inconsistency caused by the separation of front end and back end projects of vue+springboot.

Keywords: Java Session axios Vue SpringBoot

Problem: the front-end cross domain access to the back-end interface does not carry cookie s by default under the browser's security policy, so each request opens a new session.

When we print the sessionID in the background, we will find that the sessionID of each request is different. Since each request is a new session, it is naturally null when we go to get the session.

The solution is as follows:

Environment:

vue 2.0

springboot 2.1.6

 

I. front end

1. Add the following code where vue introduces axios

import axios from 'axios'
axios.defaults.withCredentials = true;// Allow cross domain carrying cookie

  

II. Back office

1. Create a new FilterConfig class and write an interceptor class

/**
 * Allow cross domain requests
 * @author Administrator
 *
 */
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.*;
@Component
public class FilterConfig implements HandlerInterceptor{
    
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
    }
 
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2)
            throws Exception {
    }
 
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
 
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));//Support cross domain requests
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");//Whether to support cookie Cross domain
        response.setHeader("Access-Control-Allow-Headers", "Authorization,Origin, X-Requested-With, Content-Type, Accept,Access-Token");//Origin, X-Requested-With, Content-Type, Accept,Access-Token
        return true;
    }
}

2. When creating a SpringMVCConfig class, please use the interceptor

/**
 * Enable cross domain configuration
 * Write spring mvcconfig class to use the configuration in FilterConfig
 * @author Administrator
 *
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SuppressWarnings("deprecation")
@SpringBootConfiguration
public class SpringMVCConfig extends WebMvcConfigurerAdapter{
    @Autowired
    private FilterConfig filterConfig;
    
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(filterConfig).addPathPatterns("/**");
    }
}

Solve the problem. After restarting the project, we print the sessionID of each request again, and we will find that the sessionID is consistent. Of course, we can also get the content we saved into the session.

System.out.println(request.getSession().getId());

Posted by robotta1530 on Mon, 21 Oct 2019 09:12:28 -0700