I’m using Spring Boot w/ Gradle for a simple login page. But it can’t find my login.html in resources>templates folder. I have looked at other similar questions but their solutions haven’t worked for me. My directory layout is correct and all mappings point correctly to where it needs to go.
Security Config
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authorizeRequests ->
authorizeRequests
.requestMatchers("/**", "/home", "/login", "/callback/", "/webjars/**", "/css**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
)
.formLogin(formLogin ->
formLogin
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/", true)
.permitAll()
)
.logout(LogoutConfigurer::permitAll
);
return http.build();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Auth Controller
@Controller
public class AuthController {
@Autowired
private UserService userService;
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("user", new User());
return "login";
}
@GetMapping("/register")
public String registerUser(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public String registerUser(@ModelAttribute User user) {
userService.createUser(user.getEmail(), user.getUsername(), user.getPassword());
return "redirect:/login";
}
}
Project Structure
Structure
application.properties
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'org.inthisyear'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.oracle.database.jdbc:ojdbc11'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Application starts and I can navigate to the web page but I’m then hit with the Whitelabel error:
**Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jun 19 19:56:23 EDT 2024
There was an unexpected error (type=Not Found, status=404).
No static resource .**
I’ve been through any post I can find on similar matters but I’ve found no solution to this problem.
Laura Ferry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.