I am new To Spring Security ,I use SpringBoot3 . I want when I hit localhost:8080/user it open my home page but in my case when I hit localhost:8080/user this url in my browser it redirect to my login page. I want every body access home page without any authentication , How can I do this . I tried this way for your better understand I submit my code for your reference.
SecurityConfig.java
package io.nilmani.management.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.SecurityFilterChain;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests((authz) -> authz.requestMatchers("/user").permitAll()
.requestMatchers("/user/**")
.authenticated()
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login") // Specify your custom login page URL
.permitAll() // Allow everyone to access the login page
)
.httpBasic(Customizer.withDefaults()).build();
}
}
UserEntityController.ja@Controller
@RequestMapping("/user")
public class UserEntityController {
@Autowired
private UserEntityServiceImpl userServiceImpl;
@GetMapping
public String homePage(){
return "homePage";
}
}
LoginController.java
@Controller
@RequestMapping("/login")
public class LoginController {
@GetMapping
public String loginPage(){
return "LoginPage";
}
}
My Requirement is when I hit localhost:8080/user it open my home page not login page.