Czym jest nazwa użytkownika i hasło podczas uruchamiania Spring Boot z Tomcat?
Podczas wdrażania aplikacji Spring przez Spring Boot and access localhost:8080
muszę uwierzytelnić, ale jaka jest nazwa użytkownika i hasło lub Jak mogę je ustawić? Próbowałem dodać to do pliku tomcat-users
, ale nie zadziałało:
<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="manager-gui"/>
To jest punkt wyjścia aplikacji:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
A to jest zależność Tomcat:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Jak mogę uwierzytelnić na localhost:8080
?
5 answers
Myślę, że masz Spring Security na swojej ścieżce klasowej i wtedy Spring security jest automatycznie skonfigurowany z domyślnym użytkownikiem i wygenerowanym hasłem
Proszę zajrzeć do pom.plik xml dla:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Jeśli masz to w swoim pom, powinieneś mieć wiadomość konsoli dziennika w następujący sposób:
Using default security password: ce6c3d39-8f20-4a41-8e01-803166bb99b6
I w monicie przeglądarki zaimportujesz użytkownika {[3] } i hasło wydrukowane w konsoli.
Lub jeśli chcesz skonfigurować spring security to można spojrzeć na Spring Boot secured przykład
Jest to wyjaśnione w dokumentacji źródłowej Spring Boot w sekcji bezpieczeństwo , wskazuje ona:
The default AuthenticationManager has a single user (‘user’ username and random password, printed at `INFO` level when the application starts up)
Using default security password: 78fa095d-3f4c-48b1-ad50-e24c31d5cf35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-02 20:53:58
Jeśli spring-security
Jary są dodawane w classpath i jeśli jest to spring-boot
aplikacja wszystkie punkty końcowe http będą zabezpieczone domyślną klasą konfiguracji zabezpieczeń SecurityAutoConfiguration
Powoduje to, że wyskakujące okienko przeglądarki pyta o poświadczenia.
Hasło zmienia się dla każdej aplikacji i można je znaleźć w konsoli.
Using default security password: 78fa095d-3f4c-48b1-ad50-e24c31d5cf35
Aby dodać własną warstwę zabezpieczeń aplikacji przed domyślnymi,
@EnableWebSecurity
public class SecurityConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
Lub jeśli chcesz zmienić hasło, możesz zastąpić domyślne z,
Podanie.xml
Bezpieczeństwo.użytkownik.password = new_password
Lub
Podanie.właściwości
spring.security.user.name=<>
spring.security.user.password=<>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-08 11:31:47
Tak więc po prostu dodając zależność spring boot security starter, podstawowe zabezpieczenia zostały już domyślnie skonfigurowane.
Możemy dostosować konfigurację zabezpieczeń, pisząc własną autoryzację i uwierzytelnianie. W tym celu utwórz nową klasę SecurityConfig, która rozszerza WebSecurityConfigurerAdapter i nadpisuje jego metody.
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("javainuse")
.password("javainuse").roles("USER");
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-29 16:36:29
Możesz również poprosić Użytkownika o poświadczenia i ustawić je dynamicznie po uruchomieniu serwera (bardzo skuteczne, gdy trzeba opublikować rozwiązanie w środowisku klienta):
@EnableWebSecurity
public class SecurityConfig {
private static final Logger log = LogManager.getLogger();
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
log.info("Setting in-memory security using the user input...");
Scanner scanner = new Scanner(System.in);
String inputUser = null;
String inputPassword = null;
System.out.println("\nPlease set the admin credentials for this web application");
while (true) {
System.out.print("user: ");
inputUser = scanner.nextLine();
System.out.print("password: ");
inputPassword = scanner.nextLine();
System.out.print("confirm password: ");
String inputPasswordConfirm = scanner.nextLine();
if (inputUser.isEmpty()) {
System.out.println("Error: user must be set - please try again");
} else if (inputPassword.isEmpty()) {
System.out.println("Error: password must be set - please try again");
} else if (!inputPassword.equals(inputPasswordConfirm)) {
System.out.println("Error: password and password confirm do not match - please try again");
} else {
log.info("Setting the in-memory security using the provided credentials...");
break;
}
System.out.println("");
}
scanner.close();
if (inputUser != null && inputPassword != null) {
auth.inMemoryAuthentication()
.withUser(inputUser)
.password(inputPassword)
.roles("USER");
}
}
}
(Maj 2018) aktualizacja-będzie działać na Spring boot 2.x:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger log = LogManager.getLogger();
@Override
protected void configure(HttpSecurity http) throws Exception {
// Note:
// Use this to enable the tomcat basic authentication (tomcat popup rather than spring login page)
// Note that the CSRf token is disabled for all requests
log.info("Disabling CSRF, enabling basic authentication...");
http
.authorizeRequests()
.antMatchers("/**").authenticated() // These urls are allowed by any authenticated user
.and()
.httpBasic();
http.csrf().disable();
}
@Bean
public UserDetailsService userDetailsService() {
log.info("Setting in-memory security using the user input...");
String username = null;
String password = null;
System.out.println("\nPlease set the admin credentials for this web application (will be required when browsing to the web application)");
Console console = System.console();
// Read the credentials from the user console:
// Note:
// Console supports password masking, but is not supported in IDEs such as eclipse;
// thus if in IDE (where console == null) use scanner instead:
if (console == null) {
// Use scanner:
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Username: ");
username = scanner.nextLine();
System.out.print("Password: ");
password = scanner.nextLine();
System.out.print("Confirm Password: ");
String inputPasswordConfirm = scanner.nextLine();
if (username.isEmpty()) {
System.out.println("Error: user must be set - please try again");
} else if (password.isEmpty()) {
System.out.println("Error: password must be set - please try again");
} else if (!password.equals(inputPasswordConfirm)) {
System.out.println("Error: password and password confirm do not match - please try again");
} else {
log.info("Setting the in-memory security using the provided credentials...");
break;
}
System.out.println("");
}
scanner.close();
} else {
// Use Console
while (true) {
username = console.readLine("Username: ");
char[] passwordChars = console.readPassword("Password: ");
password = String.valueOf(passwordChars);
char[] passwordConfirmChars = console.readPassword("Confirm Password: ");
String passwordConfirm = String.valueOf(passwordConfirmChars);
if (username.isEmpty()) {
System.out.println("Error: Username must be set - please try again");
} else if (password.isEmpty()) {
System.out.println("Error: Password must be set - please try again");
} else if (!password.equals(passwordConfirm)) {
System.out.println("Error: Password and Password Confirm do not match - please try again");
} else {
log.info("Setting the in-memory security using the provided credentials...");
break;
}
System.out.println("");
}
}
// Set the inMemoryAuthentication object with the given credentials:
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
if (username != null && password != null) {
String encodedPassword = passwordEncoder().encode(password);
manager.createUser(User.withUsername(username).password(encodedPassword).roles("USER").build());
}
return manager;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-14 08:49:53
Jeśli nie możesz znaleźć hasła na podstawie innych odpowiedzi, które wskazują na domyślną, treść komunikatu dziennika w ostatnich wersjach została zmieniona na
Using generated security password: <some UUID>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-28 18:23:35