微服务改造
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target/
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.aida.seller.common.interceptor;
|
||||||
|
|
||||||
|
import com.aida.seller.common.context.UserContext;
|
||||||
|
import com.aida.seller.model.vo.AuthPrincipalVo;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 Gateway 转发的请求头中读取已鉴权的用户身份,写入 UserContext。
|
||||||
|
* <p>
|
||||||
|
* Gateway 验证 JWT 后将 X-User-Id 和 X-User-Info 写入请求头,
|
||||||
|
* 此拦截器负责将信息填充到 ThreadLocal,供业务代码使用。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserContextInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
private static final String USER_ID_HEADER = "X-User-Id";
|
||||||
|
private static final String USER_INFO_HEADER = "X-User-Info";
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
|
String userInfoJson = request.getHeader(USER_INFO_HEADER);
|
||||||
|
if (userInfoJson != null && !userInfoJson.isBlank()) {
|
||||||
|
try {
|
||||||
|
AuthPrincipalVo principal = objectMapper.readValue(userInfoJson, AuthPrincipalVo.class);
|
||||||
|
UserContext.setUserHolder(principal);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Failed to parse X-User-Info header: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
Object handler, Exception ex) {
|
||||||
|
UserContext.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package com.aida.seller.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
||||||
|
import org.apache.ibatis.session.SqlSessionFactory;
|
||||||
|
import org.mybatis.spring.SqlSessionTemplate;
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = "com.aida.seller.module.*.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory")
|
||||||
|
public class PrimaryDataSourceConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MybatisPlusInterceptor mybatisPlusInterceptor;
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "primaryDataSource")
|
||||||
|
@ConfigurationProperties(prefix = "spring.datasource.primary")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return DataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "primarySqlSessionFactory")
|
||||||
|
public SqlSessionFactory sqlSessionFactory(
|
||||||
|
@Qualifier("primaryDataSource") DataSource dataSource,
|
||||||
|
ApplicationContext applicationContext) throws Exception {
|
||||||
|
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
|
||||||
|
bean.setDataSource(dataSource);
|
||||||
|
bean.setMapperLocations(applicationContext.getResources("classpath*:/mapper/**/*.xml"));
|
||||||
|
|
||||||
|
GlobalConfig globalConfig = new GlobalConfig();
|
||||||
|
globalConfig.setBanner(false);
|
||||||
|
bean.setGlobalConfig(globalConfig);
|
||||||
|
|
||||||
|
bean.setPlugins(mybatisPlusInterceptor);
|
||||||
|
return bean.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Bean(name = "primarySqlSessionTemplate")
|
||||||
|
public SqlSessionTemplate sqlSessionTemplate(
|
||||||
|
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
|
||||||
|
return new SqlSessionTemplate(sqlSessionFactory);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user