12 Dec, 2024
Repository link: https://github.com/ProgrammerPratik/java-spring-security
This is a simple Spring Boot MVC application project demonstrating basic web security implementation using Spring Security, Thymeleaf, and authentication mechanisms. Looks kinda simple, but demonstrates security principles like Route protection, Authentication, Login/Logout mechanisms, User role management very well.
git clone https://github.com/ProgrammerPratik/java-spring-security cd java-spring-security
mvnw spring-boot:run OR you can use any IDE like intellij to build and run projecthttp://localhost:8080WebSecurityConfig.java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((requests) ->
requests
.requestMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
)
.formLogin((form) ->
form
.loginPage("/login")
.permitAll()
)
.logout((logout) ->
logout.permitAll()
);
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}