94 lines
3.7 KiB
Java
94 lines
3.7 KiB
Java
package com.ycjy.config;
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.PropertySource;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
|
|
|
|
import javax.sql.DataSource;
|
|
import java.nio.file.Path;
|
|
|
|
import com.ycjy.common.UploadConstants;
|
|
|
|
@Configuration
|
|
@EnableWebMvc
|
|
@ComponentScan({"com.ycjy.service","com.ycjy.controller","com.ycjy.config"})
|
|
@PropertySource("classpath:db.properties")
|
|
@MapperScan("com.ycjy.mapper")
|
|
public class WebAppConfig implements WebMvcConfigurer {
|
|
// 配置数据源
|
|
@Bean(initMethod = "init")
|
|
public DruidDataSource dataSource(
|
|
@Value("${jdbc.driverClassName}") String driverClassName,
|
|
@Value("${jdbc.url}") String url,
|
|
@Value("${jdbc.user}") String user,
|
|
@Value("${jdbc.pwd}") String pwd
|
|
){
|
|
DruidDataSource ds = new DruidDataSource();
|
|
ds.setDriverClassName(driverClassName);
|
|
ds.setUrl(url);
|
|
ds.setUsername(user);
|
|
ds.setPassword(pwd);
|
|
return ds;
|
|
}
|
|
|
|
// 配置mybatis工厂
|
|
@Bean
|
|
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
|
|
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
|
factoryBean.setDataSource(dataSource);
|
|
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/*.xml"));
|
|
return factoryBean.getObject();
|
|
}
|
|
|
|
// 配置文件上传
|
|
@Bean
|
|
public StandardServletMultipartResolver multipartResolver() {
|
|
return new StandardServletMultipartResolver();
|
|
}
|
|
|
|
// 添加跨域配置
|
|
@Override
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
registry.addMapping("/**")
|
|
.allowedOrigins("http://127.0.0.1:5500")
|
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
|
.allowedHeaders("*")
|
|
.allowCredentials(true)
|
|
.maxAge(3600);
|
|
}
|
|
|
|
@Autowired
|
|
private AuthInterceptor authInterceptor;
|
|
|
|
// 注册拦截器
|
|
@Override
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
registry.addInterceptor(authInterceptor)// 注册拦截器
|
|
.addPathPatterns("/api/**")// 拦截所有以/api开头的请求
|
|
.excludePathPatterns("/api/login", "/api/upload", "/api/prodect", "/api/logs");// 排除不需要拦截的请求
|
|
}
|
|
|
|
// 配置静态资源路径
|
|
@Override
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
String uploadLocation = Path.of(UploadConstants.UPLOAD_DIR).toUri().toString();// 获取上传路径
|
|
if (!uploadLocation.endsWith("/")) uploadLocation += "/";// 确保路径以/结尾
|
|
registry.addResourceHandler("/upload/**")
|
|
.addResourceLocations(uploadLocation);
|
|
}
|
|
}
|