dev-v2 #3

Merged
bunny merged 122 commits from dev-v2 into master-v2 2024-03-30 23:40:58 +08:00
6 changed files with 94 additions and 18 deletions
Showing only changes of commit 4da125d737 - Show all commits

View File

@ -18,10 +18,10 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- lombok的依赖 -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>com.atguigu</groupId>
<artifactId>lombok</artifactId> <artifactId>spzx-model</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<!-- gateway --> <!-- gateway -->
<dependency> <dependency>

View File

@ -0,0 +1,37 @@
package com.atguigu.gateway.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
@Slf4j
public class CorsConfig {
/**
* * 解决跨域
*/
@Bean
protected CorsWebFilter addCorsMappings() {
log.info("WebMvcConfiguration===>开始跨域注册表...");
// 当allowCredentials设置为true时allowedOrigins属性不能使用通配符"*"而是需要显式列出允许的源或使用allowedOriginPatterns
CorsConfiguration config = new CorsConfiguration();
// 设置跨域请求地址
// config.addAllowedOrigin("*");
config.addAllowedMethod("*");
config.addAllowedHeader("*");
// 使用allowedOriginPatterns属性在Spring Framework 5.3及更高版本中
// 您可以使用allowedOriginPatterns属性它允许您使用正则表达式模式来匹配允许的源这样您可以更灵活地配置允许的源例如
// 使用"https?://example.com"来匹配http://example.com和https://example.com两个源
config.addAllowedOriginPattern("*");
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}

View File

@ -0,0 +1,17 @@
package com.atguigu.gateway.controller;
import com.atguigu.spzx.model.vo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/")
@RestController
@Slf4j
public class IndexController {
@GetMapping()
public Result<String> index() {
return Result.success("欢迎访问。。。");
}
}

View File

@ -5,6 +5,9 @@ spring:
active: dev active: dev
application: application:
name: service-gateway name: service-gateway
main:
web-application-type: reactive
allow-bean-definition-overriding: true
cloud: cloud:
nacos: nacos:
@ -20,16 +23,16 @@ spring:
locator: locator:
enabled: true enabled: true
# 解决跨域 # 解决跨域
globalcors: # globalcors:
cors-configurations: # cors-configurations:
'[/**]': # '[/**]':
allowedOriginPatterns: "*" # allowedOriginPatterns: "*"
# 允许请求中携带的头信息 # # 允许请求中携带的头信息
allowedHeaders: "*" # allowedHeaders: "*"
# 运行跨域的请求方式 # # 运行跨域的请求方式
allowedMethods: "*" # allowedMethods: "*"
# 跨域检测的有效期,单位s # # 跨域检测的有效期,单位s
maxAge: 36000 # maxAge: 3600099
# 路由 # 路由
routes: routes:
- id: service-product - id: service-product

View File

@ -20,4 +20,16 @@
<dependencies> <dependencies>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -1,9 +1,11 @@
package com.atguigu.user.service.impl; package com.atguigu.user.service.impl;
import com.atguigu.constant.MessageConstant; import com.atguigu.constant.MessageConstant;
import com.atguigu.exception.BunnyException; import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.user.mapper.UserInfoMapper;
import com.atguigu.user.service.SmsService; import com.atguigu.user.service.SmsService;
import com.atguigu.user.service.module.SmsServiceModule; import com.atguigu.user.service.module.SmsServiceModule;
import com.atguigu.utils.EmptyUtil;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -15,6 +17,10 @@ import java.util.concurrent.TimeUnit;
public class SmsServiceImpl implements SmsService { public class SmsServiceImpl implements SmsService {
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private EmptyUtil emptyUtil;
/** /**
* 发送短信 * 发送短信
@ -24,9 +30,10 @@ public class SmsServiceImpl implements SmsService {
@Override @Override
public void sendValidateCode(String phone) throws Exception { public void sendValidateCode(String phone) throws Exception {
Object isExist = redisTemplate.opsForValue().get(phone); Object isExist = redisTemplate.opsForValue().get(phone);
if (isExist != null) { UserInfo userInfo = userInfoMapper.selectByUsername(phone);
throw new BunnyException(MessageConstant.MESSAGE_CODE_NOT_PASS); emptyUtil.isNotEmpty(isExist, MessageConstant.MESSAGE_CODE_NOT_PASS);
} emptyUtil.isNotEmpty(userInfo, MessageConstant.USER_DOES_IS_EXIST);
// 1. 生成验证码 // 1. 生成验证码
String code = RandomStringUtils.randomNumeric(4); String code = RandomStringUtils.randomNumeric(4);
// 2. 生成的验证码放到Redis中设置过期时间 // 2. 生成的验证码放到Redis中设置过期时间