44import java .util .Collection ;
55import java .util .List ;
66
7- import org .springframework .security .core .userdetails .UserDetails ;
8- import org .springframework .security .core .userdetails .UserDetailsService ;
9- import org .springframework .security .core .userdetails .UsernameNotFoundException ;
107import org .baeldung .persistence .dao .RoleRepository ;
118import org .baeldung .persistence .dao .UserRepository ;
129import org .baeldung .persistence .model .Privilege ;
1714import org .springframework .context .MessageSource ;
1815import org .springframework .security .core .GrantedAuthority ;
1916import org .springframework .security .core .authority .SimpleGrantedAuthority ;
17+ import org .springframework .security .core .userdetails .UserDetails ;
18+ import org .springframework .security .core .userdetails .UserDetailsService ;
19+ import org .springframework .security .core .userdetails .UsernameNotFoundException ;
2020import org .springframework .stereotype .Service ;
2121import org .springframework .transaction .annotation .Transactional ;
2222
@@ -32,45 +32,45 @@ public class MyUserDetailsService implements UserDetailsService {
3232 private MessageSource messages ;
3333 @ Autowired
3434 private RoleRepository roleRepository ;
35-
36- public MyUserDetailsService () {
3735
36+ public MyUserDetailsService () {
37+ super ();
3838 }
3939
40- public UserDetails loadUserByUsername (String email ) throws UsernameNotFoundException {
41- boolean enabled = true ;
42- boolean accountNonExpired = true ;
43- boolean credentialsNonExpired = true ;
44- boolean accountNonLocked = true ;
40+ // API
41+
42+ @ Override
43+ public UserDetails loadUserByUsername (final String email ) throws UsernameNotFoundException {
4544 try {
46- User user = userRepository .findByEmail (email );
45+ final User user = userRepository .findByEmail (email );
4746 if (user == null ) {
48- return new org .springframework .security .core .userdetails .User (" " , " " , enabled , true , true , true , getAuthorities (roleRepository .findByName ("ROLE_USER" )));
47+ return new org .springframework .security .core .userdetails .User (" " , " " , true , true , true , true , getAuthorities (roleRepository .findByName ("ROLE_USER" )));
4948 }
5049
51- return new org .springframework .security .core .userdetails .User (user .getEmail (), user .getPassword (), user .isEnabled (), accountNonExpired , credentialsNonExpired , accountNonLocked , getAuthorities (user .getRole ()));
52- } catch (Exception e ) {
50+ return new org .springframework .security .core .userdetails .User (user .getEmail (), user .getPassword (), user .isEnabled (), true , true , true , getAuthorities (user .getRole ()));
51+ } catch (final Exception e ) {
5352 throw new RuntimeException (e );
5453 }
5554 }
5655
57- private Collection <? extends GrantedAuthority > getAuthorities (Role roleName ) {
58- List <GrantedAuthority > authList = getGrantedAuthorities (getPrivileges (roleName ));
59- return authList ;
56+ // UTIL
57+
58+ private final Collection <? extends GrantedAuthority > getAuthorities (final Role roleName ) {
59+ return getGrantedAuthorities (getPrivileges (roleName ));
6060 }
6161
62- public List <String > getPrivileges (Role role ) {
63- List <String > privileges = new ArrayList <String >();
64- Collection <Privilege > collection = role .getPrivileges ();
65- for (Privilege item : collection ) {
62+ private final List <String > getPrivileges (final Role role ) {
63+ final List <String > privileges = new ArrayList <String >();
64+ final Collection <Privilege > collection = role .getPrivileges ();
65+ for (final Privilege item : collection ) {
6666 privileges .add (item .getName ());
6767 }
6868 return privileges ;
6969 }
7070
71- private static List <GrantedAuthority > getGrantedAuthorities (List <String > privileges ) {
72- List <GrantedAuthority > authorities = new ArrayList <GrantedAuthority >();
73- for (String privilege : privileges ) {
71+ private final List <GrantedAuthority > getGrantedAuthorities (final List <String > privileges ) {
72+ final List <GrantedAuthority > authorities = new ArrayList <GrantedAuthority >();
73+ for (final String privilege : privileges ) {
7474 authorities .add (new SimpleGrantedAuthority (privilege ));
7575 }
7676 return authorities ;
0 commit comments