20230713

文章详细描述了在Vue项目中使用Axios遇到的跨域问题及解决方案,包括设置withCredentials和调整后端CORS配置。同时,文章还介绍了如何配置Redis主从,包括创建从节点目录、复制配置文件、修改配置以指定主节点,并使用Docker运行从节点。
摘要由CSDN通过智能技术生成

作业:

  1. 完成带有验证码的注册功能。

  2. 博客:redis记录的主从搭配。

完成带有验证码的注册功能

关于vue页面携带cookie到后端,然后request无法识别,解决方案:

参考源:

©️ https://www.ken-chy129.cn/archives/3<CORS跨域请求出现问题>,

©️https://blog.csdn.net/HermitSun/article/details/100797223<axios设置withCredentials导致“跨域”的解决方案>

首先在vue页面的axios配置上更新

import axios from 'axios'


const $request = axios.create({
    timeout: 1000000000
});

$request.defaults.withCredentials = true



$request.interceptors.request.use(
    config=>{
        config.headers['Content-Type']='application/json;charset=utf-8';
        return  config;
    },error=>{
        return Promise.reject(error);
    }
);

export default $request;
  • 如上图中,line8加入一行代码true

通过dubug运行后,发现cookie可以识别到了,但是产生了另一个问题,是跨域问题

Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

大概意思是说,在预请求时,请求就失败了。
当请求的证书模式为“include”时(即我们在axios配置的时候写的$request.defaults.withCredentials = true),Access-Control-Allow-Origin不能为 *
这是你的请求没有通过的原因
这个证书模式初始化是通过Ajax的withCredentials属性控制的

查询了一下:

withCredentials的情况下,后端要设置Access-Control-Allow-Origin为你的源地址,例如http://localhost:8080,不能是*,而且还要设置header(‘Access-Control-Allow-Credentials: true’);

于是回去重新更改了一下common模块的跨域配置

@Configuration
public class MarketWebConfig implements WebMvcConfigurer{
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTION")
                .maxAge(3600);
    }
}

在line6,我写了具体的请求源地址,允许携带Credentials

redis记录的主从搭配

从(slave)的文件夹

[root@localhost redis]# mkdir -p 6380/conf 6380/data 6380/log
[root@localhost log]# touch redis.log
[root@localhost log]# chmod 777 redis.log 
[root@localhost log]# ls
redis.log
[root@localhost log]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 713 16:53 redis.log

上传、拷贝配置文件到6380/conf

image-20230713165859006

拷贝主Redis中的conf文件到从的相对应的位置

[root@localhost conf]# cp redis.conf ../../6380/conf

image-20230713170201786

修改redis.conf

这里填的是主Redis的IP和端口

并设置只读配置为no

image-20230713170825952

image-20230713171003975

运行安装从Redis,注意这里的端口号,对外的是6380,对内的不变,相应的挂载的文件夹的路径也要对应到6380

docker run -it \
--name redis_6380 \
--privileged \
-p 6380:6379 \
--network wn_docker_net \
--ip 172.18.12.11 \
-v /usr/local/software/redis/6380/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6380/data/:/data \
-v /usr/local/software/redis/6380/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf

查看log文件夹中的redis.log文件!!注意,这个log文档一定要设置权限为777否则run指令会失败,即docker ps中的状态时exit(1)

image-20230713171259583

image-20230713171410027

image-20230713171523141

搭建成功!
log文件!!注意,这个log文档一定要设置权限为777否则run指令会失败,即docker ps中的状态时exit(1)

image-20230713171259583

image-20230713171410027

image-20230713171523141

搭建成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值