本文档说明小程序登录服务需要配置的内容,包括数据库、Redis、微信配置等。
需要确保 applet_user
表已创建,表结构如下:
CREATE TABLE `applet_user` (
`id` varchar(32) NOT NULL COMMENT '主键',
`create_by` varchar(50) DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建日期',
`update_by` varchar(50) DEFAULT NULL COMMENT '更新人',
`update_time` datetime DEFAULT NULL COMMENT '更新日期',
`sys_org_code` varchar(64) DEFAULT NULL COMMENT '所属部门',
`name` varchar(100) DEFAULT NULL COMMENT '昵称',
`openid` varchar(100) DEFAULT NULL COMMENT '第三方认证id',
`phone` varchar(20) DEFAULT NULL COMMENT '手机号',
`bmi` decimal(10,2) DEFAULT NULL COMMENT '体总指数',
`fat` decimal(10,2) DEFAULT NULL COMMENT '脂肪',
`avatar` varchar(500) DEFAULT NULL COMMENT '头像',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_openid` (`openid`),
UNIQUE KEY `uk_phone` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='小程序用户表';
在 application.yml
中配置数据库连接:
spring:
datasource:
url: jdbc:mysql://localhost:3306/jeecg-boot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
在 application.yml
中配置Redis:
spring:
redis:
host: localhost
port: 6379
password:
database: 0
timeout: 10000ms
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
需要创建Redis配置类:
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置key的序列化方式
template.setKeySerializer(new StringRedisSerializer());
// 设置value的序列化方式
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 设置hash key的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
// 设置hash value的序列化方式
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
在 application.yml
中配置微信小程序信息:
wechat:
mpAppId: your_applet_appid
mpAppSecret: your_applet_secret
merchantId: your_merchant_id # 如果需要支付功能
确保 WxHttpUtils
类能正确读取配置:
@Component
public class WxHttpUtils {
@Value("${wechat.mpAppId}")
private String appid;
@Value("${wechat.mpAppSecret}")
private String secret;
}
在 application.yml
中配置JWT密钥:
jeecg:
jwt:
secret: your_jwt_secret_key
expire: 604800 # 7天,单位秒
确保 JwtUtil
类能正确读取配置:
@Component
public class JwtUtil {
@Value("${jeecg.jwt.secret}")
private String secret;
@Value("${jeecg.jwt.expire}")
private long expire;
}
在 application.yml
中配置日志级别:
logging:
level:
org.jeecg.modules.applet: DEBUG
org.jeecg.modules.common.wxUtils: DEBUG
如果需要跨域访问,需要配置CORS:
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
确保 pom.xml
中包含以下依赖:
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- HTTP客户端 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- JSON处理 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
在生产环境中,建议使用环境变量或配置中心来管理敏感信息:
wechat:
mpAppId: ${WECHAT_MP_APPID:default_appid}
mpAppSecret: ${WECHAT_MP_SECRET:default_secret}
spring:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
可以添加健康检查端点:
management:
endpoints:
web:
exposure:
include: health,info,metrics
endpoint:
health:
show-details: always