Spring Security Default login page with JPA

Project Structure:

LoginController.java


package com.deepsingh44.login.jpa.controller;
import com.deepsingh44.login.jpa.model.Login;
import com.deepsingh44.login.jpa.repository.LoginRepository;
import com.deepsingh44.login.jpa.service.CustomLoginDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    private LoginRepository loginRepository;
    @Autowired
    private CustomLoginDetailService customLoginDetailService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @GetMapping("/")
    public String home() {
        return "Welcome to Secure web page";
    }

    @PostMapping("/register")
    public Login register(@RequestBody Login login) {
        String encodepassword = bCryptPasswordEncoder.
        encode(login.getPassword());
        login.setPassword(encodepassword);
        return loginRepository.save(login);
    }
}

SecurityConfig.java


package com.deepsingh44.login.jpa.config;
import com.deepsingh44.login.jpa.service.CustomLoginDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.
builders.AuthenticationManagerBuilder;
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.
configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomLoginDetailService customLoginDetailService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) 
    throws Exception {
        auth.userDetailsService(customLoginDetailService).
        passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http)
    throws Exception {
        http.csrf().disable().authorizeRequests().
                antMatchers("/register").
                permitAll().
                anyRequest().
                authenticated().
                and()
                .formLogin()
                .permitAll().
                and().
                logout().
                permitAll();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean()
    throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

CustomLoginDetail.java


package com.deepsingh44.login.jpa.model;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
@Component
public class CustomLoginDetail  implements UserDetails {
    private Login login;

    public CustomLoginDetail(Login login) {
        this.login = login;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new ArrayList();
    }

    @Override
    public String getPassword() {
        return login.getPassword();
    }

    @Override
    public String getUsername() {
        return login.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
 

Login.java


package com.deepsingh44.login.jpa.model;
import org.springframework.stereotype.Component;
import javax.persistence.*;

@Component
@Entity
@Table(name = "login")
public class Login {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
  

LoginRepository.java


package com.deepsingh44.login.jpa.repository;
import com.deepsingh44.login.jpa.model.Login;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LoginRepository extends JpaRepository<Login, Integer> {
    Login getLoginByUsername(String username);
}

CustomLoginDetailService.java


package com.deepsingh44.login.jpa.service;
import com.deepsingh44.login.jpa.model.CustomLoginDetail;
import com.deepsingh44.login.jpa.model.Login;
import com.deepsingh44.login.jpa.repository.LoginRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.
UserDetailsService;
import org.springframework.security.core.userdetails.
UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class CustomLoginDetailService implements UserDetailsService {
    @Autowired
    private LoginRepository loginRepository;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        Login login = loginRepository.getLoginByUsername(username);

        if (login == null) {
            throw 
            new UsernameNotFoundException("Could not find login user");
        }

        return new CustomLoginDetail(login);
    }
}
  

DefaultLoginJpaApplication.java


package com.deepsingh44.login.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DefaultLoginJpaApplication {

	public static void main(String[] args) {
      SpringApplication.run(DefaultLoginJpaApplication.class, args);
	}

}
 

application.properties


spring.datasource.url = jdbc:mysql://localhost:3306/mystudents
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
server.port=8081

spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
  

pom.xml


<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.37</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>
  

Output Screen :






No comments: