update role spring security

// Step 1: Define a custom enum for roles (if not already defined)
public enum CustomRole {
    // Add other roles as needed

// Step 2: Create a UserDetailsServiceImpl implementing UserDetailsService
public class UserDetailsServiceImpl implements UserDetailsService {

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // Implement logic to load user details from your data source (e.g., database)
        // Include user roles in the returned UserDetails object
        // Use authorities to represent roles (e.g., new SimpleGrantedAuthority("ROLE_USER"))

// Step 3: Configure authentication and authorization in SecurityConfig class
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsServiceImpl userDetailsService;

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    protected void configure(HttpSecurity http) throws Exception {

    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();

// Step 4: Update the User entity to include roles
public class User {
    // ... existing fields ...

    @ElementCollection(targetClass = CustomRole.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
    private Set<CustomRole> roles;

    // ... getters and setters ...

// Step 5: Update the UserRepository to find users by username
public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);