java对接Wx支付的问题

本文介绍了如何在SpringBoot项目中配置payPropertiesBean以集成微信支付,以及如何处理Ajax跨域问题,包括使用CORS配置和自定义Filter实现跨域功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

payProperties Bean的问题

package com.example.pay.common;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 微信支付配置
 * @author shuqingyou 2022/9/16
 */
@Data
@Component
@Configuration
@ConfigurationProperties(prefix = "pay")
public class payProperties {
    //微信公众号appid
    private String appid;

    //公众号设置的api密钥
    private String api_key;

    //#微信商户平台 商户id
    private String mch_id;
}

ajax跨域

1、跨域配置

@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:63342") // 允许的前端地址
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的 HTTP 方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true); // 允许发送身份验证信息
    }
}

2、跨域过滤器

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;

        response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342"); // 允许跨域的前端地址
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");

        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, res);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }
}

有多个要扫描的位置时

@ComponentScan(basePackages = {"com.example.pay.filter", "com.example.pay", "com.example.pay.common"})

跟这个Java接入微信支付超级详细教程——从入门到精通_java对接微信支付-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值