Код: Выделить всё
@Override
protected Object determineCurrentLookupKey() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication!= null && authentication.getPrincipal() instanceof User user) {
return user.getTenant_id();
}
return null;
}
Код: Выделить всё
public ResponseEntity loginUser(AuthenticateRequest authenticateRequest) {
//authenticate the User
authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(authenticateRequest.getEmail(),authenticateRequest.getPassword()));
//this line of code runs oly if authentication is successful
User user = userRepository.findByEmail(authenticateRequest.getEmail()).orElseThrow();
String jwt = jwtService.generateToken(user);
//set the security context to allow routing of db and to know the tenantID and also set the datasource
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken(user.getUsername(),null,user.getAuthorities()));
var mds = new MultiTenantDataSourceRouter();
mds.setTargetDataSources(dataSourceConfig.loadAllDataSources());
mds.afterPropertiesSet();
//create an Employee Object for the user once
Employee employee = employeeRepository.findByEmail(user.getEmail());
if(employee!= null){
Employee newEmployee = Employee.builder()
.email(user.getEmail())
.name(user.getName())
.build();
}
return ResponseEntity.ok().body(new AuthenticationResponse(jwt));
}
Это приводит к ошибке, поскольку в моей базе данных по умолчанию нет таблицы сотрудников.
Подробнее здесь: https://stackoverflow.com/questions/784 ... -in-spring