I have configured spring security filter as follows -…
- User
I have configured spring security filter as follows -
httpSecurity.authorizeHttpRequests(configurer -> configurer.requestMatchers("/css/**").permitAll().anyRequest().authenticated()) .formLogin(form -> form.loginPage("/loginPage").loginProcessingUrl("/authenticate").permitAll());
And defined my login page as follows -
Assistant
I see you've started configuring Spring Security with a custom login page, but it looks like your message got cut off when you were about to show the login page definition.
Your Spring Security configuration looks good so far - you're allowing public access to CSS resources and requiring authentication for everything else, with a custom login page at "/loginPage" that processes authentication via "/authenticate".
Could you share the rest of your login page configuration? I'd be happy to help review it or troubleshoot any issues you might be experiencing.
- User
I have configured spring security filter as follows -
httpSecurity.authorizeHttpRequests(configurer -> configurer.requestMatchers("/css/**").permitAll().anyRequest().authenticated()) .formLogin(form -> form.loginPage("/loginPage").loginProcessingUrl("/authenticate").permitAll());
And defined my login page as follows -
When I login, why do I get redirected to the login page, I want to go to home page only if the login is successful. If there is some error, I want to display it on the login page.
Here is the controller defined -
@Controller public class LoginController { @GetMapping("/loginPage") public String loginPage() { return "plain-login"; } }
Assistant
The issue is with your form configuration. You're missing the
method="post"attribute in your form tag. Spring Security's default authentication processing expects a POST request, but your form is currently submitting as GET.Here's the corrected form:
<form th:action="@{/authenticate}" method="post"> <label for="username"> Username <input type="text" name="username" id="username"/> </label> <label for="password"> Password <input type="password" name="password" id="password"/> </label> <div th:if="${param.error}"> <i style="color: red;">Invalid username or password.</i> </div> <input type="submit" value="Login"/> </form>Additionally, you should configure success and failure URLs in your Spring Security configuration to have better control over the redirect behavior:
httpSecurity.authorizeHttpRequests(configurer -> configurer.requestMatchers("/css/**").permitAll() .anyRequest().authenticated()) .formLogin(form -> form.loginPage("/loginPage") .loginProcessingUrl("/authenticate") .defaultSuccessUrl("/home", true) // Redirect to home on success .failureUrl("/loginPage?error=true") // Redirect back to login with error .permitAll());