Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.itn.services; import com.itn.entities.Users; import java.util.ArrayList; import java.util.List; 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.User; 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; import org.springframework.transaction.annotation.Transactional; /** * * This class is needed for password encryption */ @Service("customUserDetailsService") public class CustomUserDetailsService implements UserDetailsService { @Autowired private UserService userService; @Transactional(readOnly = true) @Override public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { Users user = userService.findByUserName(userName); System.out.println("User : " + user); if (user == null) { System.out.println("User not found"); throw new UsernameNotFoundException("Username not found"); } // This new User is default class made my spring, not to be confused with entity name "Users" return new User(user.getUserName(), user.getPassword(), user.getState().equals("Active"), true, true, true, getGrantedAuthorities(user)); } private List<GrantedAuthority> getGrantedAuthorities(Users user) { List<GrantedAuthority> authorities = new ArrayList<>(); user.getUserProfiles().stream().map((userProfile) -> { System.out.println("UserProfile : " + userProfile); return userProfile; }).forEach((userProfile) -> { authorities.add(new SimpleGrantedAuthority("ROLE_" + userProfile.getType())); }); System.out.print("authorities :" + authorities); return authorities; } }