我知道有数十亿人质疑这个问题,但我无法解决这个问题。我有Spring + Hibernate的应用程序。 Hibernate有2个配置(用于客户和管理数据库)。客户的hibernate支持使用DATABASE方法的多租户。我也使用Spring Security。
这是我的租户ID解决方案:
@Component
public class MyTenantIdentifierResolver implements CurrentTenantIdentifierResolver {
@Autowired
private UserService userService;
public String resolveCurrentTenantIdentifier() {
User u = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
com.lspsystem.model.User us = userService.getByLogin(u.getUsername());
return us.getCompany().getTenantId();
}
public boolean validateExistingCurrentSessions() {
return true;
}
}
这是我的UserService:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
public User get(Integer id) {
return userDAO.get(id);
}
public boolean delete(Integer id) {
return userDAO.delete(id);
}
public void update(User u) {
userDAO.update(u);
}
public List<User> getAll(User u) {
return userDAO.getAllUsers();
}
public User getByLogin(String login) {
return userDAO.getByLogin(login);
}
}
我的问题:为什么UserService
中的MyTenantIdResolver
总是null
?但是,例如在下一课(来自安全),它不是null
。
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;
public User loadUserByUsername(String s) throws UsernameNotFoundException {
com.lspsystem.model.User u = userService.getByLogin(s);
return new User(u.getEmail(), u.getPassword(), true, true, true, true, getGrantedAuthorities(u));
}
@Autowired
public UserDetailsServiceImpl(UserService userService) {
this.userService = userService;
}
private List<GrantedAuthority> getGrantedAuthorities(com.lspsystem.model.User u) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
return authorities;
}
}
请注意,UserService和MyTenantIdResolver属于不同的hibernate配置。
已更新
客户休眠配置:
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:customerHibernate.properties"})
public class RemoteDBHibernateConfig {
@Autowired
private Environment environment;
@Bean(name = "customerSessionFactory")
public SessionFactory sessionFactory() throws IOException{
LocalSessionFactoryBean builder =
new LocalSessionFactoryBean();
builder.setDataSource(dataSource1());
builder.setPackagesToScan("com.lspsystem.model");
builder.setHibernateProperties(hibernateProperties());
builder.afterPropertiesSet();
return builder.getObject();
}
@Bean("defaultDS")
public DataSource dataSource1() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.multiTenancy", environment.getRequiredProperty("hibernate.multiTenancy"));
properties.put("hibernate.multi_tenant_connection_provider", environment.getRequiredProperty("hibernate.multi_tenant_connection_provider"));
properties.put("hibernate.tenant_identifier_resolver", environment.getRequiredProperty("hibernate.tenant_identifier_resolver"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager() throws IOException{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}}
Admin hibernate config:
@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:adminHibernate.properties"})
public class AdminDBHibernateConfig {
@Autowired
private Environment environment;
@Bean(name = "adminSessionFactory")
public SessionFactory sessionFactory() throws IOException {
LocalSessionFactoryBean builder =
new LocalSessionFactoryBean();
builder.setDataSource(dataSource());
builder.setPackagesToScan("com.lspsystem.model");
builder.setHibernateProperties(hibernateProperties());
builder.afterPropertiesSet();
return builder.getObject();
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("admin.jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("admin.jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("admin.jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("admin.jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("admin.hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("admin.hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("admin.hibernate.format_sql"));
properties.put("hibernate.hbm2dll.auto", environment.getRequiredProperty("admin.hibernate.hbm2ddl.auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager() throws IOException{
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory());
return txManager;
}}
网络应用配置:
@Configuration
@EnableWebMvc
@Import(SecurityConfiguration.class)
public class WebApplicationConfig extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver view = new InternalResourceViewResolver();
view.setViewClass(JstlView.class);
view.setPrefix("/WEB-INF/jsp/");
view.setSuffix(".jsp");
return view;
}}
UserDAO,例如:
@Repository
public class UserDAOImpl implements UserDAO {
@Autowired
@Qualifier("adminSessionFactory")
private SessionFactory sessionFactory;
public com.lspsystem.model.User get(Integer id) {
Session session = sessionFactory.openSession();
User u = (User)session.load(User.class, id);
return u;
}
public User getByLogin(String login) {
Session session = sessionFactory.openSession();
Query q = session.createQuery("SELECT u FROM User u WHERE u.email = :email");
q.setParameter("email", login);
User u = (User)q.list().get(0);
return u;
}}
答案 0 :(得分:2)
这不可能是真的! @Autowire
将强制该字段为非null。所以userService=null
的唯一情况是MyTenantIdentifierResolver
不是春天豆!
将MyTenantIdentifierResolver
的实例设为spring-bean。