Spring Security With BCrypt
Spring Security With BCrypt
Prerequisites
This tutorial assumes that you have already completed the Spring Security videos in
the Spring Boot 3, Spring 6 course. This includes the Spring Security videos for
JDBC authentication for plain-text passwords and encrypted passwords.
Overview of Steps
1. Download and Import the code
2. Run database scripts
3. Review the source code
4. Test the App
MYSQL WORKBENCH
In MySQL workbench, run the following database scripts:
- /sql-scripts/01-employee-directory.sql
- /sql-scripts/02-setup-spring-security-demo-database-
hibernate-bcrypt.sql
The first script adds sample employees to the database which is our sample data for
Employee CRUD.
The second script creates the database tables for security.
The script also creates the user accounts with encrypted passwords. It also includes
the user roles.
File: /src/main/java/com/luv2code/springboot/cruddemo/config/DemoSecurityConfig.java
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
We are assigning the custom user details and password encoder to the
DaoAuthenticationProvider.
import jakarta.persistence.*;
import java.util.Collection;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "username")
private String userName;
@Column(name = "password")
private String password;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
public Role() {
}
import com.luv2code.springboot.cruddemo.entity.User;
import org.springframework.security.core.userdetails.UserDetailsService;
File: /src/main/java/com/luv2code/springboot/cruddemo/service/UserServiceImpl.java
package com.luv2code.springboot.cruddemo.service;
import com.luv2code.springboot.cruddemo.dao.RoleDao;
import com.luv2code.springboot.cruddemo.dao.UserDao;
import com.luv2code.springboot.cruddemo.entity.User;
import com.luv2code.springboot.cruddemo.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.stream.Collectors;
@Service
public class UserServiceImpl implements UserService {
@Autowired
public UserServiceImpl(UserDao userDao, RoleDao roleDao) {
this.userDao = userDao;
this.roleDao = roleDao;
}
@Override
public User findByUserName(String userName) {
// check the database if the user already exists
return userDao.findByUserName(userName);
}
@Override
public UserDetails loadUserByUsername(String userName) throws
UsernameNotFoundException {
User user = userDao.findByUserName(userName);
if (user == null) {
throw new UsernameNotFoundException("Invalid username or password.");
}
return new org.springframework.security.core.userdetails.User(user.getUserName(),
user.getPassword(),
mapRolesToAuthorities(user.getRoles()));
}
import com.luv2code.springboot.cruddemo.entity.User;
UserDaoImpl
File: /src/main/java/com/luv2code/springboot/cruddemo/dao/UserDaoImpl.java
package com.luv2code.springboot.cruddemo.dao;
import com.luv2code.springboot.cruddemo.entity.User;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public User findByUserName(String theUserName) {
return theUser;
}
User Accounts
User Actions