2014-10-19 12 views
6

ho costruito mia abitudine Authenticaton Manager per Primavera di sicurezza che va qualcosa come questoCome creare su misura oggetto UserDetail nella Primavera di sicurezza

public class AccountAuthenticationProvider implements AuthenticationProvider{ 

    @Autowired 
    private AuthenticationService authService; 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 

     String userName = authentication.getName(); 
     String password = (String)authentication.getCredentials(); 

     if(authService.isValid(userName,password)){ 
      List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>(); 
      grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER")); 
      SecurityContext securityContext = new SecurityContextImpl(); 
      return new UsernamePasswordAuthenticationToken(userName,password); 
     } 

     return null; 
    } 


    public void setAuthService(AuthenticationService authService) { 
     this.authService = authService; 
    } 

    @Override 
    public boolean supports(Class<?> authentication) { 
     return true; 
    } 

} 

ma come faccio a creare il mio personalizzato UserDetail oggetto? Lo userò per memorizzare i valori relativi all'account

risposta

3

è necessario implementare UserDetailsService e sovrascrivere il metodo loadUserByUsername per restituire la classe UserDetails personalizzato. Mi piace-

public class UserServiceImpl implements UserDetailsService {` 

@Autowired 
UserDaoImpl userDao; 

@Override 
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
    System.out.println(username); 
    Users user = (Users) userDao.findByUserName(username); 
    List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRoles()); 
    System.out.println("after...."); 
    return buildUserForAuthentication(user, authorities); 
} 

private List<GrantedAuthority> buildUserAuthority(Set<UserRole> userRoles) { 
    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>(); 
    for(UserRole userRole : userRoles){ 
     System.out.println("called buildUserAuthority(Set<UserRole> userRoles) method....."); 
     setAuths.add(new SimpleGrantedAuthority(userRole.getRole())); 
    } 

    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(setAuths); 
    return grantedAuthorities; 
} 

private User buildUserForAuthentication(Users user, List<GrantedAuthority> authorities) { 
    //accountNonExpired, credentialsNonExpired, accountNonLocked, authorities properties 
    System.out.println("called buildUserForAuthentication(Users user, List<GrantedAuthority> authorities) method...."); 
    return new User(user.getUsername(), user.getPassword(), user.getEnabled(), true, true, true, authorities); 
}} 
+0

stessa risposta rispetto agli altri. – Patrick

2

Quasi ce l'hai fatta!

if(authService.isValid(userName,password)) { 
    List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>(); 
    grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_USER")); 
    MyObject myObj = new MyObject(userName, password, otherInfo); 
    return new UsernamePasswordAuthenticationToken(mjObj,"", grantedAuthorityList); 
} 

Il primo argomento su UsernamePasswordAuthenticationToken è il principio. Il principio è l'oggetto nel sistema che rappresenta la persona (o cosa) che ha appena effettuato il login.

Prima dell'autenticazione il principio è solo il nome utente (String) perché sono tutte le informazioni che hai a quel punto. Dopo l'accesso, è possibile raccogliere altre informazioni per andare con l'utente.

Primavera offre interfacce: User, UserDetails e UserDetailsService per aiutare a gestire gli utenti e fare cose Springy con gli utenti, quindi se fate MyObject implementare UserDetails quindi è possibile ottenere alcuni benefici aggiuntivi dall'ambiente primavera, ma non è necessario, è può restare solo con il tuo MyObject.

Nel vostro controller (in Spring 4) è possibile utilizzare il @AuthenticationPrincipal per iniettare l'oggetto utente nelle chiamate, ad es .:

@RequestMapping(method = RequestMethod.GET, value = "/foo/{bar}") 
public SomeObject myCommand(@AuthenticationPrincipal MyObject user, @PathVariable String bar); 
Problemi correlati