前端都采用POST方法请求后台,在main.js中增加一个拦截器
Vue.http.options.emulateJSON = false;
// Vue.http.options.emulateJSON = true;
Vue.http.options.xhr = { withCredentials: true };
Vue.http.interceptors.push((request, next) => {
request.credentials = true
next()
});
1.跨域问题
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
String origin = request.getHeader("Origin");
if(origin == null) {
origin = request.getHeader("Referer");
}
context.addZuulResponseHeader("Content-Type","application/x-www-form-urlencoded");
context.addZuulResponseHeader("Access-Control-Allow-Origin",origin);
context.addZuulResponseHeader("Access-Control-Allow-Credentials","true");
2.Seesion管理
微服务Seesion管理采用HttpSession。
(1)pom.xml 增加依赖
org.springframework.session
spring-session
org.springframework.boot
spring-boot-starter-redis
1.4.7.RELEASE
@EnableRedisHttpSession
public class HttpSessionConfig {
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setHostName("192.168.10.10");
connection.setPort(6379);
return connection;
}
}
RequestContext context = RequestContext.getCurrentContext();
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
if(userInfo==null){
// 需要校验登录状态
context.setSendZuulResponse(false);
context.setResponseStatusCode(200);
Iresp_common iresp_common = new Iresp_common("2000","尚未登录");
context.setResponseBody(gson.toJson(iresp_common));
return null;
}