我已经为我的应用程序提供了基于JWT令牌的身份验证机制,现在仅针对API,我需要使用oracle数据库添加身份验证:我的应用程序用户还将是DB oracle用户。
我想先不知道数据源的用户名和密码来启动spring boot应用程序,它将在前端进行身份验证后恢复。
我第一次希望应用程序在不连接任何数据库的情况下启动,但是一旦用户尝试进行身份验证,我们将使用其用户名和密码来创建与数据库的连接。目的是我们希望从oracle数据库的身份验证中受益。
当用户尝试进行身份验证时,我大惊小怪地收到了这个错误
“消息”:“无法获取JDBC连接;嵌套的异常是org.hibernate.exception.GenericJDBCException:无法获取JDBC连接”,
DataSourceConfig
@Component
public class DataSourceConfig {
@NotNull
private String username;
@NotNull
private String password;
@NotNull
private String url;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) { this.password = password; }
public void setUrl(String url) { this.url = url; }
public String getUrl() { return url; }
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
@Bean
public DataSource dataSource() throws SQLException {
OracleDataSource dataSource = new OracleDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setURL(url);
dataSource.setImplicitCachingEnabled(true);
dataSource.setFastConnectionFailoverEnabled(true);
return dataSource;
}
}
AuthenticationController
@RestController
@CrossOrigin
@RequestMapping("/Authentication")
public class AuthenticationController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private UserService userDetailsService;
@Autowired
private MenuService menuservice;
@Autowired
private Environment env;
@Autowired
private DataSourceConfig dataSourceConfig;
@RequestMapping(method = RequestMethod.POST,consumes = "application/x-www-form-urlencoded")
public ResponseEntity<?> createAuthenticationToken(JwtRequest authenticationRequest) throws Exception {
dataSourceConfig.setUsername(authenticationRequest.getUsername());
dataSourceConfig.setPassword(authenticationRequest.getPassword());
dataSourceConfig.setUrl(env.getProperty("spring.datasource.url"));
dataSourceConfig.dataSource();
String url = env.getProperty("spring.datasource.url");
User userconnected
=userDetailsService.getUserbyUsernameAndPassword(authenticationRequest.getUsername(),
authenticationRequest.getPassword(),url);
if (userconnected != null){
ModelDTO model = menuservice.GetModel(userconnected);
final UserDetails userDetails = userDetailsService
.loadUserByUsername(authenticationRequest.getUsername());
String ConnectionString = "url=" + url + ";user=" + authenticationRequest.getUsername() + ";password=" + authenticationRequest.getPassword();
final String token = jwtTokenUtil.generateToken(userDetails, ConnectionString);
return ResponseEntity.ok(new JwtResponse(token,userconnected.getUsername(),model));
}
else{
return ResponseEntity.ok("Utilisateur/ ou Mot de passe incorrecte");
}
}
}
Config.properties
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
jasypt.encryptor.password=aLPa@@sToRm++
#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect