spring-security: Custom Form Login is causing too many redirects and cannot get the login page for Spring Security v6.1.0
I defined the SecurityFilterChain bean like below
@Bean
public SecurityFilterChain securityFilterChain (HttpSecurity http) throws Exception {
// We are disabling CSRF so that our forms don't complain about a CSRF token.
// Beware that it can create a security vulnerability
return http.csrf(AbstractHttpConfigurer::disable)
// Here we are configuring our login form
// .formLogin(Customizer.withDefaults())
.formLogin(formLogin -> {
formLogin
.loginPage("/login") // Login page will be accessed through this endpoint. We will create a controller method for this.
.loginProcessingUrl("/login-processing") // This endpoint will be mapped internally. This URL will be our Login form post action.
.usernameParameter("username")
.passwordParameter("password")
.permitAll() // We re permitting all for login page
.defaultSuccessUrl("/") // If the login is successful, user will be redirected to this URL.
.failureUrl("/login?error=true"); // If the user fails to login, application will redirect the user to this endpoint
}
)
.authorizeHttpRequests(authorize ->
authorize
// We are permitting all static resources to be accessed publicly
.requestMatchers("/images/**", "/css/**", "/js/**").permitAll()
// We are restricting endpoints for individual roles.
// Only users with allowed roles will be able to access individual endpoints.
.requestMatchers("/course/add").hasRole("ADMIN")
.requestMatchers("/course/show-all").hasAnyRole("ADMIN", "USER")
.requestMatchers("/course/edit").hasAnyRole("USER")
// Following line denotes that all requests must be authenticated.
// Hence, once a request comes to our application, we will check if the user is authenticated or not.
.anyRequest().authenticated()
)
.logout(logout ->
logout
.logoutUrl("/logout")
.logoutSuccessUrl("/")
)
.build();
}
Unfortunately, this code does not work for custom form login, but works perfectly for form login with default configuration (which is also commented in the code),
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (2 by maintainers)
Commits related to this issue
- fix spring security signin page was causing too many redirects to itself. https://github.com/spring-projects/spring-security/issues/13285 — committed to steve-community/steve by goekay 2 years ago
- fix spring security signin page was causing too many redirects to itself. https://github.com/spring-projects/spring-security/issues/13285 — committed to faculoyarte/steve by goekay 2 years ago
- merge Steve community master 20241105 (#5) * Extract tag authorization in a dedicated service * Provide location for tag status * adding DB migration Baselinescript * Baseline-script: insert... — committed to shizen-connect/steve-forked by dakai-wei-of-shizen 2 years ago
Hello @marcusdacoregio , after enabling the TRACE log for Spring Security, I found the following line in the log.
So I set the authorization of my view page location, “/WEB-INF/views/**” in my case to permitAll()
And it worked like a charm!
Thank you for your kind support 😃
Hey Everyone!! I am stuck with TOO MANY REDIRECTS… Kindly help me with the code
SpringSecurity:
Controller:
demologin.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8” isELIgnored=“false”%>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body>Login
</body> </html>Jsp path:
/springmvcbootdemo/src/main/webapp/views/demologin.jsp
It’s not working when we are using below code with mvc.pattern , which is required in latest version. Any suggestions what could be resolution? @Bean public SecurityFilterChain settingUpSecurityFilterChain() throws Exception {
// httpSecurity.formLogin(Customizer.withDefaults()); httpSecurity.httpBasic(Customizer.withDefaults());
First of all, set
/WEB-INF/views/**to permit all, like the code below in your spring security http configurationrequestMatchers("/images/**", "/css/**", "/js/**", "/WEB-INF/views/**").permitAll()Next, in your controller method, return should be only your view name. Replace
return "redirect:/login.html";toreturn "login";If your view page is in the correct location, it should work.
yes,you are allowing login end point without authentication,but not login.html ,you can also allow it without authentication by : http…authorizeHttpRequests((authorize) -> authorize .requestMatchers(“/login.html”) .permitAll().anyRequest().authenticated())