Ein einfaches Kontaktformular erstellen
Hinweis: Diese Inhalte wurden mit Unterstützung von Künstlicher Intelligenz erstellt und redaktionell überprüft (Transparenzhinweis gemäß Art. 50 EU AI Act).
Kontaktformular erstellen mit Spring boot mvc
Spring boot Starter
- Spring Web Starter
- Thymeleaf
- Spring Boot DevTools
- Spring Data JPA Starter
- Spring Security Starter
Projektstruktur
- src
- main
- java
- com.example.demo
- controller
- HomeController.java
- model
- Contact.java
- repository
- ContactRepository.java
- service
- ContactService.java
- DemoApplication.java
- controller
- com.example.demo
- resources
- static
- css
- style.css
- css
- templates
- contact.html
- index.html
- login.html
- register.html
- welcome.html
- application.properties
- static
- java
- main
Roadmap
- Erstellen Sie ein neues Spring Boot-Projekt
- Fügen Sie die Abhängigkeiten hinzu
- Konfigurieren Sie die Datenbank
- Erstellen Sie das Modell
- Erstellen Sie das Repository
- Erstellen Sie den Service
- Erstellen Sie den Controller
- Erstellen Sie die Ansichten
- Konfigurieren Sie die Sicherheit
- Testen Sie die Anwendung
Konfigurieren Sie die Datenbank h2
- application.properties
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true spring.h2.console.path=/h2
Konfigration erstellen Security
Konfigration erstellen Security
@Configuration
@EnableWebSecurity
public class Anmeldung {
@Value("${spring.security.user.name}")
private String username;
@Value("${spring.security.user.password}")
private String password;
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authz) -> authz
.requestMatchers("/admin/**").denyAll() // Verweigere Zugriff auf /admin/** für alle Benutzer
.requestMatchers("/**").permitAll() // Erlaube Zugriff auf /public/** ohne Authentifizierung
.anyRequest().authenticated() // Alle anderen Anfragen erfordern Authentifizierung
)
.formLogin(withDefaults()); // Aktiviere Standard-Formularanmeldung
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = org.springframework.security.core.userdetails.User.withUsername(username)
.password(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode(password))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
Hinweis: Diese Inhalte wurden mit Unterstützung von Künstlicher Intelligenz erstellt und redaktionell überprüft (Transparenzhinweis gemäß Art. 50 EU AI Act).