Skip to content

Commit

Permalink
optimize: add secure authentication to interfaces in ClusterController (
Browse files Browse the repository at this point in the history
  • Loading branch information
ggbocoder authored Nov 29, 2023
1 parent 03e88d5 commit 1921b2d
Show file tree
Hide file tree
Showing 13 changed files with 566 additions and 29 deletions.
4 changes: 4 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@
<artifactId>httpclient</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.common.exception;


/**
* Exception indicating authentication failure. This exception is typically thrown
* when the authentication process fails, and it extends SecurityException to
* signal that it is a security-related issue.
*/
public class AuthenticationFailedException extends SecurityException {

/**
* Constructs a new AuthenticationFailedException with no detailed message.
*/
public AuthenticationFailedException() {
super();
}

/**
* Constructs a new AuthenticationFailedException with the specified detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the getMessage() method).
*/
public AuthenticationFailedException(String message) {
super(message);
}

/**
* Constructs a new AuthenticationFailedException with the specified cause.
*
* @param cause the cause (which is saved for later retrieval by the
* getCause() method).
*/
public AuthenticationFailedException(Throwable cause) {
super(cause);
}

/**
* Constructs a new AuthenticationFailedException with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the getMessage() method).
* @param cause the cause (which is saved for later retrieval by the
* getCause() method).
*/
public AuthenticationFailedException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.common.exception;



/**
* Exception indicating a retryable failure. This exception is typically thrown
* when a retryable process fails, and it extends RuntimeException to
* signal that it is an unchecked exception.
*/
public class RetryableException extends Exception {

/**
* Constructs a new RetryableException with no detailed message.
*/
public RetryableException() {
super();
}

/**
* Constructs a new RetryableException with the specified detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the getMessage() method).
*/
public RetryableException(String message) {
super(message);
}

/**
* Constructs a new RetryableException with the specified cause.
*
* @param cause the cause (which is saved for later retrieval by the
* getCause() method).
*/
public RetryableException(Throwable cause) {
super(cause);
}

/**
* Constructs a new RetryableException with the specified detail
* message and cause.
*
* @param message the detail message (which is saved for later retrieval
* by the getMessage() method).
* @param cause the cause (which is saved for later retrieval by the
* getCause() method).
*/
public RetryableException(String message, Throwable cause) {
super(message, cause);
}
}

35 changes: 23 additions & 12 deletions common/src/main/java/io/seata/common/util/HttpClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.seata.common.util;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class HttpClientUtil {
private static final Map<Integer/*timeout*/, CloseableHttpClient> HTTP_CLIENT_MAP = new ConcurrentHashMap<>();

private static final PoolingHttpClientConnectionManager POOLING_HTTP_CLIENT_CONNECTION_MANAGER =
new PoolingHttpClientConnectionManager();
new PoolingHttpClientConnectionManager();

static {
POOLING_HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(10);
Expand All @@ -66,25 +67,35 @@ public class HttpClientUtil {
})));
}


// post request
public static CloseableHttpResponse doPost(String url, Map<String, String> params, Map<String, String> header,
int timeout) throws IOException {
int timeout) throws IOException {
try {
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();
HttpPost httpPost = new HttpPost(uri);
String contentType = "";
if (header != null) {
header.forEach(httpPost::addHeader);
contentType = header.get("Content-Type");
}
if (StringUtils.isNotBlank(contentType)) {
if (ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType)) {
List<NameValuePair> nameValuePairs = new ArrayList<>();
params.forEach((k, v) -> {
nameValuePairs.add(new BasicNameValuePair(k, v));
});
String requestBody = URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8);
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_FORM_URLENCODED);
httpPost.setEntity(stringEntity);
} else if (ContentType.APPLICATION_JSON.getMimeType().equals(contentType)) {
ObjectMapper objectMapper = new ObjectMapper();
String requestBody = objectMapper.writeValueAsString(params);
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
}
}
List<NameValuePair> nameValuePairs = new ArrayList<>();
params.forEach((k, v) -> {
nameValuePairs.add(new BasicNameValuePair(k, v));
});
String requestBody = URLEncodedUtils.format(nameValuePairs, StandardCharsets.UTF_8);

StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_FORM_URLENCODED);
httpPost.setEntity(stringEntity);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpClient client = HTTP_CLIENT_MAP.computeIfAbsent(timeout,
k -> HttpClients.custom().setConnectionManager(POOLING_HTTP_CLIENT_CONNECTION_MANAGER)
.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout)
Expand All @@ -99,7 +110,7 @@ public static CloseableHttpResponse doPost(String url, Map<String, String> param

// get request
public static CloseableHttpResponse doGet(String url, Map<String, String> param, Map<String, String> header,
int timeout) throws IOException {
int timeout) throws IOException {
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
Expand Down
Loading

0 comments on commit 1921b2d

Please sign in to comment.