Merge branch 'feature/SUPPORT-8381_file_upload_auth' into feature/SUPPORT-8381_file_upload
This commit is contained in:
commit
de0d42f55f
2 changed files with 36 additions and 5 deletions
|
|
@ -3,7 +3,9 @@ package ervu;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.CommonClientConfigs;
|
||||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
|
import org.apache.kafka.common.config.SaslConfigs;
|
||||||
import org.apache.kafka.common.serialization.StringSerializer;
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
@ -17,8 +19,18 @@ import org.springframework.kafka.core.ProducerFactory;
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class KafkaProducerConfig {
|
public class KafkaProducerConfig {
|
||||||
@Value("${kafka.send.url:#{null}}")
|
@Value("${kafka.send.url}")
|
||||||
private String kafkaUrl;
|
private String kafkaUrl;
|
||||||
|
@Value("${kafka.send.security.protocol}")
|
||||||
|
private String securityProtocol;
|
||||||
|
@Value("${kafka.send.login.module:org.apache.kafka.common.security.scram.ScramLoginModule}")
|
||||||
|
private String loginModule;
|
||||||
|
@Value("${kafka.send.username}")
|
||||||
|
private String username;
|
||||||
|
@Value("${kafka.send.password}")
|
||||||
|
private String password;
|
||||||
|
@Value("${kafka.sasl.mechanism}")
|
||||||
|
private String saslMechanism;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ProducerFactory<String, String> producerFactory() {
|
public ProducerFactory<String, String> producerFactory() {
|
||||||
|
|
@ -27,13 +39,16 @@ public class KafkaProducerConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Map<String, Object> producerConfigs() {
|
public Map<String, Object> producerConfigs() {
|
||||||
if (this.kafkaUrl == null) {
|
|
||||||
throw new RuntimeException("Property kafka.send.url is null");
|
|
||||||
}
|
|
||||||
Map<String, Object> props = new HashMap<>();
|
Map<String, Object> props = new HashMap<>();
|
||||||
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaUrl);
|
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaUrl);
|
||||||
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
||||||
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
||||||
|
|
||||||
|
props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocol);
|
||||||
|
props.put(SaslConfigs.SASL_JAAS_CONFIG, loginModule + " required username=\""
|
||||||
|
+ username + "\" password=\"" + password + "\";");
|
||||||
|
props.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
|
||||||
|
|
||||||
return props;
|
return props;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package ervu.client.fileupload;
|
package ervu.client.fileupload;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.Authenticator;
|
||||||
|
import java.net.PasswordAuthentication;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
|
|
@ -21,13 +23,27 @@ public class EmployeeInfoWebDavClient {
|
||||||
|
|
||||||
@Value("${file.webdav.upload.url:http://localhost:5757}")
|
@Value("${file.webdav.upload.url:http://localhost:5757}")
|
||||||
private String url;
|
private String url;
|
||||||
|
@Value("${file.webdav.upload.username}")
|
||||||
|
private String username;
|
||||||
|
@Value("${file.webdav.upload.password}")
|
||||||
|
private String password;
|
||||||
|
|
||||||
public boolean webDavUploadFile(String filename, MultipartFile multipartFile) {
|
public boolean webDavUploadFile(String filename, MultipartFile multipartFile) {
|
||||||
try {
|
try {
|
||||||
HttpClient httpClient = HttpClient.newBuilder().build();
|
HttpClient httpClient = HttpClient.newBuilder()
|
||||||
|
.authenticator(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(username, password.toCharArray());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(this.url + "/" + filename))
|
HttpRequest httpRequest = HttpRequest.newBuilder().uri(URI.create(this.url + "/" + filename))
|
||||||
.PUT(HttpRequest.BodyPublishers.ofByteArray(multipartFile.getBytes())).build();
|
.PUT(HttpRequest.BodyPublishers.ofByteArray(multipartFile.getBytes())).build();
|
||||||
|
|
||||||
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
|
logger.debug("Response starus code: {}", response.statusCode());
|
||||||
return (response.statusCode() >= 200) && (response.statusCode() <= 202);
|
return (response.statusCode() >= 200) && (response.statusCode() <= 202);
|
||||||
}
|
}
|
||||||
catch (IOException | InterruptedException e) {
|
catch (IOException | InterruptedException e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue