feat(新增): 跨域配置第二套
Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
parent
dd8d37e343
commit
4da125d737
|
@ -18,10 +18,10 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- lombok的依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>spzx-model</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- gateway -->
|
||||
<dependency>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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("欢迎访问。。。");
|
||||
}
|
||||
}
|
|
@ -5,6 +5,9 @@ spring:
|
|||
active: dev
|
||||
application:
|
||||
name: service-gateway
|
||||
main:
|
||||
web-application-type: reactive
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
|
@ -19,17 +22,17 @@ spring:
|
|||
discovery:
|
||||
locator:
|
||||
enabled: true
|
||||
# 解决跨域
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowedOriginPatterns: "*"
|
||||
# 允许请求中携带的头信息
|
||||
allowedHeaders: "*"
|
||||
# 运行跨域的请求方式
|
||||
allowedMethods: "*"
|
||||
# 跨域检测的有效期,单位s
|
||||
maxAge: 36000
|
||||
# 解决跨域
|
||||
# globalcors:
|
||||
# cors-configurations:
|
||||
# '[/**]':
|
||||
# allowedOriginPatterns: "*"
|
||||
# # 允许请求中携带的头信息
|
||||
# allowedHeaders: "*"
|
||||
# # 运行跨域的请求方式
|
||||
# allowedMethods: "*"
|
||||
# # 跨域检测的有效期,单位s
|
||||
# maxAge: 3600099
|
||||
# 路由
|
||||
routes:
|
||||
- id: service-product
|
||||
|
|
|
@ -20,4 +20,16 @@
|
|||
<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>
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.atguigu.user.service.impl;
|
||||
|
||||
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.module.SmsServiceModule;
|
||||
import com.atguigu.utils.EmptyUtil;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
@ -15,6 +17,10 @@ import java.util.concurrent.TimeUnit;
|
|||
public class SmsServiceImpl implements SmsService {
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private UserInfoMapper userInfoMapper;
|
||||
@Autowired
|
||||
private EmptyUtil emptyUtil;
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
|
@ -24,9 +30,10 @@ public class SmsServiceImpl implements SmsService {
|
|||
@Override
|
||||
public void sendValidateCode(String phone) throws Exception {
|
||||
Object isExist = redisTemplate.opsForValue().get(phone);
|
||||
if (isExist != null) {
|
||||
throw new BunnyException(MessageConstant.MESSAGE_CODE_NOT_PASS);
|
||||
}
|
||||
UserInfo userInfo = userInfoMapper.selectByUsername(phone);
|
||||
emptyUtil.isNotEmpty(isExist, MessageConstant.MESSAGE_CODE_NOT_PASS);
|
||||
emptyUtil.isNotEmpty(userInfo, MessageConstant.USER_DOES_IS_EXIST);
|
||||
|
||||
// 1. 生成验证码
|
||||
String code = RandomStringUtils.randomNumeric(4);
|
||||
// 2. 生成的验证码放到Redis中,设置过期时间
|
||||
|
|
Loading…
Reference in New Issue