This is an automated email from the ASF dual-hosted git repository.
benjobs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git
The following commit(s) were added to refs/heads/dev by this push:
new 2f3e7be37 [Bug] Fix the NPE bug and refactor some code (#1656)
2f3e7be37 is described below
commit 2f3e7be374fe79a234bad275bb6a2eb9b0df487b
Author: 1996fanrui <[email protected]>
AuthorDate: Tue Sep 20 21:38:35 2022 +0800
[Bug] Fix the NPE bug and refactor some code (#1656)
---
.../streampark/console/base/util/MoreFutures.java | 8 ++---
.../streampark/console/base/util/ShaHashUtils.java | 9 +++--
.../security/impl/ldap/LdapAuthenticator.java | 42 +++++++++++-----------
.../system/service/impl/UserServiceImpl.java | 6 ++--
4 files changed, 34 insertions(+), 31 deletions(-)
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/MoreFutures.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/MoreFutures.java
index aa57e2da3..83d2b6575 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/MoreFutures.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/MoreFutures.java
@@ -60,27 +60,27 @@ public final class MoreFutures {
}
public static <T> CompletionStage<T>
exceptionallyCompose(CompletionStage<T> stage, Function<Throwable, ? extends
CompletionStage<T>> fn) {
- return stage.thenApply((res) ->
CompletableFuture.completedFuture(res)).exceptionally((Function<Throwable, ?
extends CompletableFuture<T>>) fn).thenCompose(Function.identity());
+ return
stage.thenApply(CompletableFuture::completedFuture).exceptionally((Function<Throwable,
? extends CompletableFuture<T>>) fn).thenCompose(Function.identity());
}
public static <T> CompletableFuture<T> completeImmediately(Supplier<T>
supplier) {
try {
return CompletableFuture.completedFuture(supplier.get());
} catch (Throwable e) {
- CompletableFuture<T> f = new CompletableFuture();
+ CompletableFuture<T> f = new CompletableFuture<>();
f.completeExceptionally(e);
return f;
}
}
public static <T> CompletableFuture<T> completedExceptionally(Throwable
cause) {
- CompletableFuture<T> future = new CompletableFuture();
+ CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(cause);
return future;
}
public static <T> ScheduledFuture<T> completedScheduledFuture(T value) {
- return new CompletedScheduledFuture(value);
+ return new CompletedScheduledFuture<>(value);
}
private static final class CompletedScheduledFuture<T> implements
ScheduledFuture<T> {
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/ShaHashUtils.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/ShaHashUtils.java
index a9a272926..bf7557d55 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/ShaHashUtils.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/ShaHashUtils.java
@@ -24,6 +24,8 @@ import java.util.Random;
public final class ShaHashUtils {
+ public static final int DEFAULT_SALT_LENGTH = 26;
+
private ShaHashUtils() {
}
@@ -36,8 +38,11 @@ public final class ShaHashUtils {
* @return
*/
public static String encrypt(String salt, String password) {
- String pass = new Sha256Hash(password, ByteSource.Util.bytes(salt),
1024).toHex();
- return pass;
+ return new Sha256Hash(password, ByteSource.Util.bytes(salt),
1024).toHex();
+ }
+
+ public static String getRandomSalt() {
+ return getRandomSalt(DEFAULT_SALT_LENGTH);
}
/**
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/security/impl/ldap/LdapAuthenticator.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/security/impl/ldap/LdapAuthenticator.java
index 4d465f07c..1853ab966 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/security/impl/ldap/LdapAuthenticator.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/security/impl/ldap/LdapAuthenticator.java
@@ -46,28 +46,26 @@ public class LdapAuthenticator extends
AbstractAuthenticator {
// ldapUser is null, login by default
if (ldapUser == null) {
return passwordAuthenticator.login(userId, password);
- } else {
- //check if user exist
- User user = usersService.findByName(userId);
- if (user != null) {
- return passwordAuthenticator.login(userId, password);
- } else {
- // create ....
- User newUser = new User();
- newUser.setCreateTime(new Date());
- newUser.setUsername(userId);
- newUser.setRoleId("100001");
- newUser.setNickName(userId);
- newUser.setStatus("1");
- newUser.setSex("1");
-
- String salt = ShaHashUtils.getRandomSalt(26);
- String saltPass = ShaHashUtils.encrypt(salt,
user.getPassword());
- newUser.setSalt(salt);
- newUser.setPassword(saltPass);
- usersService.createUser(newUser);
- return newUser;
- }
}
+ //check if user exist
+ User user = usersService.findByName(userId);
+ if (user != null) {
+ return passwordAuthenticator.login(userId, password);
+ }
+ // create ....
+ User newUser = new User();
+ newUser.setCreateTime(new Date());
+ newUser.setUsername(userId);
+ newUser.setRoleId("100001");
+ newUser.setNickName(userId);
+ newUser.setStatus("1");
+ newUser.setSex("1");
+
+ String salt = ShaHashUtils.getRandomSalt();
+ String saltPass = ShaHashUtils.encrypt(salt, password);
+ newUser.setSalt(salt);
+ newUser.setPassword(saltPass);
+ usersService.createUser(newUser);
+ return newUser;
}
}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/service/impl/UserServiceImpl.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/service/impl/UserServiceImpl.java
index 564137c59..2c410913f 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/service/impl/UserServiceImpl.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/service/impl/UserServiceImpl.java
@@ -105,7 +105,7 @@ public class UserServiceImpl extends
ServiceImpl<UserMapper, User> implements Us
public void createUser(User user) throws Exception {
user.setCreateTime(new Date());
user.setAvatar(User.DEFAULT_AVATAR);
- String salt = ShaHashUtils.getRandomSalt(26);
+ String salt = ShaHashUtils.getRandomSalt();
String password = ShaHashUtils.encrypt(salt, user.getPassword());
user.setSalt(salt);
user.setPassword(password);
@@ -151,7 +151,7 @@ public class UserServiceImpl extends
ServiceImpl<UserMapper, User> implements Us
@Transactional(rollbackFor = Exception.class)
public void updatePassword(String username, String password) throws
Exception {
User user = new User();
- String salt = ShaHashUtils.getRandomSalt(26);
+ String salt = ShaHashUtils.getRandomSalt();
password = ShaHashUtils.encrypt(salt, password);
user.setSalt(salt);
user.setPassword(password);
@@ -163,7 +163,7 @@ public class UserServiceImpl extends
ServiceImpl<UserMapper, User> implements Us
public void resetPassword(String[] usernames) throws Exception {
for (String username : usernames) {
User user = new User();
- String salt = ShaHashUtils.getRandomSalt(26);
+ String salt = ShaHashUtils.getRandomSalt();
String password = ShaHashUtils.encrypt(salt,
User.DEFAULT_PASSWORD);
user.setSalt(salt);
user.setPassword(password);