forked from gushen/sunway-user-system
feat: first commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package icu.sunway.ai_spring_example;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AiSpringExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AiSpringExampleApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package icu.sunway.ai_spring_example.Config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 跨域配置类
|
||||
* 用于配置允许的跨域请求来源、方法、头信息等
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
// 创建CorsConfiguration对象
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
|
||||
// 允许所有域名进行跨域请求
|
||||
config.addAllowedOriginPattern("*");
|
||||
|
||||
// 允许的请求头
|
||||
config.addAllowedHeader("*");
|
||||
|
||||
// 允许的请求方法
|
||||
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||
|
||||
// 允许发送Cookie
|
||||
config.setAllowCredentials(true);
|
||||
|
||||
// 预检请求的有效期,单位为秒
|
||||
config.setMaxAge(3600L);
|
||||
|
||||
// 创建UrlBasedCorsConfigurationSource对象
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
|
||||
// 对所有URL应用跨域配置
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
|
||||
// 返回CorsFilter实例
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package icu.sunway.ai_spring_example.Config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
// 启用跨域配置
|
||||
.cors(cors -> cors.configurationSource(request -> new org.springframework.web.cors.CorsConfiguration().applyPermitDefaultValues()))
|
||||
// 禁用默认的登录表单和HTTP基本认证
|
||||
.formLogin(form -> form.disable())
|
||||
.httpBasic(basic -> basic.disable())
|
||||
// 允许所有请求通过,取消默认登录验证
|
||||
.authorizeHttpRequests((authz) -> authz
|
||||
.anyRequest().permitAll()
|
||||
)
|
||||
// 禁用CSRF保护
|
||||
.csrf(csrf -> csrf.disable())
|
||||
// 设置会话创建策略为无状态
|
||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
29
src/main/resources/application.yaml
Normal file
29
src/main/resources/application.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
spring:
|
||||
application:
|
||||
name: ai-spring-example
|
||||
# 动态多数据源配置
|
||||
datasource:
|
||||
dynamic:
|
||||
primary: master
|
||||
strict: false
|
||||
datasource:
|
||||
master:
|
||||
url: jdbc:mysql://182.92.242.196:3306/sys_user?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
username: remote_user
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
fr:
|
||||
url: jdbc:mysql://182.92.242.196:3306/flower_rain?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
|
||||
username: remote_user
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
# Redis配置
|
||||
data:
|
||||
redis:
|
||||
host: 182.92.242.196
|
||||
port: 6379
|
||||
password: sunway
|
||||
database: 0
|
||||
|
||||
server:
|
||||
port: 8080
|
||||
Reference in New Issue
Block a user