uni-app小程序后端使用shiro登录session统一

1、session统一问题

1)spring boot项目

/**
 * 重写web mvc 配置
 * @author ZhangJi
 */
@Configuration
public class DefaultWebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /*
         * SystemStaticParam.affixRequest 为前端URL访问路径
         * "file:" + SystemStaticParam.fileSysPath 是本地磁盘映射
         */
        registry.addResourceHandler(SystemStaticParam.affixRequestRule).addResourceLocations("file:///" + SystemStaticParam.fileSysPath);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域允许
        registry.addMapping("*")
                //允许域名
                .allowedOrigins(SystemStaticParam.webDomain)
                .allowedMethods("GET", "POST")
                .allowedHeaders("*")
                //允许携带cookie
                .allowCredentials(true)
        ;
    }

}

2) 小程序请求携带cookie与sessionId

uni.request({
                url: '请求地址·?sessionId=' + sessionId,
                header: {
                    'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
                    'cookie': uni.getStorageSync("cookie")
                },
                withCredentials: true,
                xhrFields: {withCredentials: true },

2、登录验证码session统一问题

先发一次请求,获得sessionId,在获取验证码时携带返回sessionId

@GetMapping("/init-session")
@ResponseBody
public Result initSession(HttpServletRequest request, HttpServletResponse response) {

    response.setHeader("sessionId", request.getSession().getId());

    return Result.success(BaseConstant.SUCCESS_STR, request.getSession().getId());
}

你可能感兴趣的:(session,shiro,java,小程序,spring)