fix profile cannot access while logining from GG account

This commit is contained in:
2026-07-02 14:26:08 +07:00
parent ba587a40cb
commit ba7a43e637
10 changed files with 26 additions and 26 deletions
+7 -15
View File
@@ -1,20 +1,12 @@
import java.sql.Connection; import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestOracleJDBC { public class TestOracleJDBC {
@SuppressWarnings("deprecation")
public static void main(String[] args) { public static void main(String[] args) {
String jdbcUrl = "jdbc:oracle:thin:@192.168.1.15:1521/sisvietnam"; MessageDigestPasswordEncoder encoder = new MessageDigestPasswordEncoder("SHA-256");
String username = "sisvietnam"; String hash = encoder.encode("admin");
String password = "sisvietnam"; System.out.println("Encoded hash for admin: " + hash);
boolean matches = encoder.matches("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918");
try { System.out.println("Does 'admin' match my hash? " + matches);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
System.out.println("Connection successful!");
connection.close();
} catch (SQLException e) {
System.err.println("Connection failed!");
e.printStackTrace();
}
} }
} }
@@ -12,7 +12,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint; import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationEntryPoint;
import org.springframework.security.oauth2.server.resource.web.access.BearerTokenAccessDeniedHandler; import org.springframework.security.oauth2.server.resource.web.access.BearerTokenAccessDeniedHandler;
@@ -42,8 +42,9 @@ public class SecurityConfiguration {
} }
@Bean @Bean
@SuppressWarnings("deprecation")
public PasswordEncoder passwordEncoder() { public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); return new MessageDigestPasswordEncoder("SHA-256");
} }
@Bean @Bean
@@ -43,8 +43,8 @@ public class User extends AbstractAuditingEntity<Long> implements Serializable {
@JsonIgnore @JsonIgnore
@NotNull @NotNull
@Size(min = 60, max = 60) @Size(min = 60, max = 100)
@Column(name = "password_hash", length = 60, nullable = false) @Column(name = "password_hash", length = 100, nullable = false)
private String password; private String password;
@Size(max = 50) @Size(max = 50)
@@ -11,6 +11,8 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.ClaimAccessor; import org.springframework.security.oauth2.core.ClaimAccessor;
import org.springframework.security.oauth2.jose.jws.MacAlgorithm; import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.core.user.OAuth2User;
/** /**
* Utility class for Spring Security. * Utility class for Spring Security.
@@ -42,6 +44,10 @@ public final class SecurityUtils {
return springSecurityUser.getUsername(); return springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof Jwt jwt) { } else if (authentication.getPrincipal() instanceof Jwt jwt) {
return jwt.getSubject(); return jwt.getSubject();
} else if (authentication.getPrincipal() instanceof OidcUser oidcUser) {
return oidcUser.getEmail() != null ? oidcUser.getEmail() : oidcUser.getName();
} else if (authentication.getPrincipal() instanceof OAuth2User oauth2User) {
return oauth2User.getAttribute("email") != null ? oauth2User.getAttribute("email") : oauth2User.getName();
} else if (authentication.getPrincipal() instanceof String s) { } else if (authentication.getPrincipal() instanceof String s) {
return s; return s;
} }
@@ -231,7 +231,7 @@ public class UserService {
*/ */
public void updateUser(String firstName, String lastName, String email, String langKey, String imageUrl) { public void updateUser(String firstName, String lastName, String email, String langKey, String imageUrl) {
SecurityUtils.getCurrentUserLogin() SecurityUtils.getCurrentUserLogin()
.flatMap(userRepository::findOneByLogin) .flatMap(login -> userRepository.findOneByLogin(login).or(() -> userRepository.findOneByEmailIgnoreCase(login)))
.ifPresent(user -> { .ifPresent(user -> {
user.setFirstName(firstName); user.setFirstName(firstName);
user.setLastName(lastName); user.setLastName(lastName);
@@ -249,7 +249,7 @@ public class UserService {
@Transactional @Transactional
public void changePassword(String currentClearTextPassword, String newPassword) { public void changePassword(String currentClearTextPassword, String newPassword) {
SecurityUtils.getCurrentUserLogin() SecurityUtils.getCurrentUserLogin()
.flatMap(userRepository::findOneByLogin) .flatMap(login -> userRepository.findOneByLogin(login).or(() -> userRepository.findOneByEmailIgnoreCase(login)))
.ifPresent(user -> { .ifPresent(user -> {
String currentEncryptedPassword = user.getPassword(); String currentEncryptedPassword = user.getPassword();
if (!passwordEncoder.matches(currentClearTextPassword, currentEncryptedPassword)) { if (!passwordEncoder.matches(currentClearTextPassword, currentEncryptedPassword)) {
@@ -279,7 +279,8 @@ public class UserService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Optional<User> getUserWithAuthorities() { public Optional<User> getUserWithAuthorities() {
return SecurityUtils.getCurrentUserLogin().flatMap(userRepository::findOneWithAuthoritiesByLogin); return SecurityUtils.getCurrentUserLogin()
.flatMap(login -> userRepository.findOneWithAuthoritiesByLogin(login).or(() -> userRepository.findOneWithAuthoritiesByEmailIgnoreCase(login)));
} }
/** /**
@@ -34,7 +34,7 @@ spring:
indent-output: true indent-output: true
datasource: datasource:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
url: jdbc:oracle:thin:@localhost:1521/FREEPDB1 url: jdbc:oracle:thin:@localhost:1521/sisvietnam
username: sisvietnam username: sisvietnam
password: "sisvietnam" password: "sisvietnam"
hikari: hikari:
@@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport"> <meta content="width=device-width, initial-scale=1" name="viewport">
<link th:href="@{/login-assets/css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/style.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/login-dl.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/login-dl.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/customer-style.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/customer-style.css}" rel="stylesheet" type="text/css">
@@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport"> <meta content="width=device-width, initial-scale=1" name="viewport">
<link th:href="@{/login-assets/css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/style.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/login-dl.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/login-dl.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/customer-style.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/customer-style.css}" rel="stylesheet" type="text/css">
@@ -7,7 +7,7 @@
<meta content="width=device-width, initial-scale=1" name="viewport"> <meta content="width=device-width, initial-scale=1" name="viewport">
<!-- BEGIN GLOBAL MANDATORY STYLES --> <!-- BEGIN GLOBAL MANDATORY STYLES -->
<link th:href="@{/login-assets/css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/style.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/font-awesome.min.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/font-awesome.min.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/simple-line-icons.min.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/simple-line-icons.min.css}" rel="stylesheet" type="text/css">
<link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css"> <link th:href="@{/login-assets/bootstrap.min.css}" rel="stylesheet" type="text/css">