|
16 | 16 | */
|
17 | 17 | package org.apache.seata.console.security;
|
18 | 18 |
|
19 |
| -import java.io.BufferedReader; |
20 |
| -import java.io.Console; |
21 |
| -import java.io.IOException; |
22 |
| -import java.io.InputStreamReader; |
23 |
| -import java.util.Arrays; |
| 19 | +import java.util.UUID; |
24 | 20 | import javax.annotation.PostConstruct;
|
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.beans.factory.annotation.Value; |
25 | 24 | import org.springframework.security.core.userdetails.UserDetails;
|
26 | 25 | import org.springframework.security.core.userdetails.UserDetailsService;
|
27 | 26 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
34 | 33 | @Service
|
35 | 34 | public class CustomUserDetailsServiceImpl implements UserDetailsService {
|
36 | 35 |
|
37 |
| - private Console console; |
| 36 | + private static final Logger LOGGER = LoggerFactory.getLogger(CustomUserDetailsServiceImpl.class); |
| 37 | + |
| 38 | + @Value("${console.user.username:seata}") |
| 39 | + private String username; |
| 40 | + |
| 41 | + @Value("${console.user.password:}") |
| 42 | + private String password; |
| 43 | + |
38 | 44 | private User user;
|
39 | 45 |
|
40 | 46 | /**
|
41 | 47 | * Init.
|
42 | 48 | */
|
43 | 49 | @PostConstruct
|
44 |
| - public void init() throws IOException { |
45 |
| - String envUsername = System.getenv("SEATA_CONSOLE_USERNAME"); |
46 |
| - String envPassword = System.getenv("SEATA_CONSOLE_PASSWORD"); |
47 |
| - |
48 |
| - if (envUsername != null && envPassword != null) { |
49 |
| - user = new User(envUsername, envPassword); |
50 |
| - return; |
51 |
| - } |
52 |
| - |
53 |
| - console = System.console(); |
54 |
| - if (console == null) { |
55 |
| - // In an IDE, 'System.console()' returns 'null', so 'BufferedReader' is used instead. |
56 |
| - BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); |
57 |
| - String username = promptInput(reader, "Username: "); |
58 |
| - String password = promptInput(reader, "Password: "); |
59 |
| - user = new User(username, password); |
60 |
| - } else { |
61 |
| - String username = getUsername(); |
62 |
| - String password = getUserPassword(); |
| 50 | + public void init() { |
| 51 | + if (!password.isEmpty()) { |
63 | 52 | user = new User(username, password);
|
| 53 | + return; |
64 | 54 | }
|
65 |
| - } |
66 | 55 |
|
67 |
| - private String promptInput(BufferedReader reader, String message) throws IOException { |
68 |
| - System.out.print(message); |
69 |
| - return reader.readLine(); |
70 |
| - } |
| 56 | + password = generateRandomPassword(); |
| 57 | + LOGGER.info( |
| 58 | + "No password was configured. A random password has been generated for security purposes. You may either:\n" |
| 59 | + + "1. Use the auto-generated password: [{}]\n" |
| 60 | + + "2. Set a custom password in the configuration.", |
| 61 | + password); |
71 | 62 |
|
72 |
| - private String getUserPassword() { |
73 |
| - return Arrays.toString(console.readPassword("Password: ")); |
| 63 | + user = new User(username, password); |
74 | 64 | }
|
75 | 65 |
|
76 |
| - private String getUsername() { |
77 |
| - return console.readLine("Username: "); |
| 66 | + private String generateRandomPassword() { |
| 67 | + return UUID.randomUUID().toString().replace("-", "").substring(0, 8); |
78 | 68 | }
|
79 | 69 |
|
80 | 70 | @Override
|
|
0 commit comments