A Spring Security login-logout module is a common requirement for securing web applications. Here’s an example of how to create a simple Spring Security login and logout module using Spring Boot and Java configuration:
Create a Spring Boot Project:
Start by creating a Spring Boot project using your preferred IDE or Spring Initializer. Make sure to include the “Spring Web” and “Spring Security” dependencies.
Create a Security Configuration Class: Create a Java configuration class that extends WebSecurityConfigurerAdapter. This class will configure Spring Security.
Create a Security Configuration Class: Create a Java configuration class that extends WebSecurityConfigurerAdapter. This class will configure Spring Security.
import org.springframework
.context.annotation.Bean;
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.core
.userdetails.User;
import org.springframework
.security.core
.userdetails.UserDetails;
import org.springframework
.security.core
.userdetails.UserDetailsService;
import org.springframework
.security.provisioning
.InMemoryUserDetailsManager;
import org.springframework
.security.config.annotation
.authentication.builders
.AuthenticationManagerBuilder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends
WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService
userDetailsService() {
UserDetails user = User
.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new
InMemoryUserDetailsManager(user);
}
@Override
protected void configure
(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("
/public/**").permitAll()
.antMatchers("/private/**")
.authenticated()
.and().formLogin()
.loginPage("/login")
.permitAll()
.and().logout()
.permitAll();
}
}
In this example, we’ve defined a simple in-memory user with the username “user” and password “password.” Customize the userDetailsService
method to load users from your preferred data source.
Create Login and Logout Controllers: Create controllers for login and logout functionality. Here’s a basic example:
import org.springframework
.stereotype.Controller;
import org.springframework.web.bind
.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
}
@Controller
public class LogoutController {
@GetMapping("/logout")
public String logout() {
return "redirect:/login?logout";
}
}
In this example, the /login
endpoint renders the login page, and the /logout
endpoint logs the user out and redirects them to the login page with a “logout” parameter.
Create HTML Login Page: Create an HTML login page (e.g., login.html) in your project’s templates directory. You can use Thymeleaf, JSP, or any other templating engine you prefer. Here’s a simple example using Thymeleaf:
Secure Your Application:Add security annotations to your controllers or methods to protect specific parts of your application. For example, you can use @PreAuthorize to restrict access to certain roles or users.
Now, you have a basic Spring Security login-logout module in your Spring Boot application. Users can access the login page, log in with their credentials, and log out when needed. Customize and expand this module according to your application’s requirements.
Now, you have a basic Spring Security login-logout module in your Spring Boot application. Users can access the login page, log in with their credentials, and log out when needed. Customize and expand this module according to your application’s requirements.