I am learning a Spring Security.
Now I want to customize login page, but I can't to change default page to custom. In the security config I written loginPage("/login"), created controller, but this steps didn't help me.
My configuration class:
package configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/templates/**", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.disable();
}
}
Controller:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class AuthController {
@GetMapping("/login")
public String getLoginPage() {
return "login";
}
}
Path to login.html: resources/templates/login.html
login.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" type="text/css" media="all"
href="../static/style.css" th:href="@{style.css}"/>
</head>
<body>
<header align="center">
<ul>
<a class="logo" href="home.html"><span>L</span><span>O</span><span>G</span><span>O</span></a>
</ul>
</header>
<div class="content_div">
<div class="div_c">
<div class="content_reg">
<form method="post">
<h1 class="form_title">Registration</h1>
<div class="form_group">
<input type="email" class="form_input" placeholder=" ">
<label class="form_label">E-mail</label>
</div>
<div class="form_group">
<input type="text" class="form_input" placeholder=" ">
<label class="form_label">Login</label>
</div>
<div class="form_group">
<input type="password" class="form_input" placeholder=" ">
<label class="form_label">Password</label>
</div>
<button type="submit" class="form_button">Register</button>
</form>
</div>
</div>
<div class="div_c">
<form action="/login" class="login_form" method="post">
<h1 class="form_title">Log in</h1>
<div class="form_group">
<input type="text" class="form_input" placeholder=" ">
<label class="form_label">Login</label>
</div>
<div class="form_group">
<input type="password" class="form_input" placeholder=" ">
<label class="form_label">Password</label>
</div>
<button type="submit" class="form_button">Log in</button>
</form>
</div>
</div>
</body>
</html>
If you help me with this problem - I'll be very thankful.
CodePudding user response:
Your login page seems to be working fine on path "/login", if you want unauthenticated requests to be redirected to /login you should delete disable() from WebSecurityConfig
CodePudding user response:
You need to tell Spring where to look for your static files:
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/resources/templates/
You can read more about this at https://www.baeldung.com/spring-mvc-static-resources#2-custom-directories.
