Solve around vue + springboot end of the separation project, session sessionID front-end cross-domain access issues caused by the inconsistency is null

Question: front-end interface to back-end cross-domain access, the default browser in the security policy is not to carry a cookie, so every request to open a new session.

We find that in the background print sessionID, each request sessionID is different, since each request is a new session, then we go get session when the nature is null.

Solution is as follows: 

surroundings: 

2.0 view

springboot 2.1.6

 

A front end portion

1. In introducing vue add the following code position axios

Import Axios from 'Axios' 
axios.defaults.withCredentials = to true ; // permit cross carried cookie

  

Second, the background section

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

/**
 * 允许跨域请求
 * @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"));//支持跨域请求
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");//是否支持cookie跨域
        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. Create a SpringMVCConfig class, with interceptors

/**
 * 启用跨域配置
 * 编写SpringMVCConfig类使用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, restart the project after we print each request again sessionID will find sessionID is the same, of course, we can get into the content of the session.

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

Guess you like

Origin www.cnblogs.com/zhainan-blog/p/11711427.html