2015-01-15 2 views
5

Questo è il mio servizio UserDetails nella sicurezza primavera:GrantedAuthority Null per ottenere UserDetails su Spring Security

public class UserDetail implements UserDetailsService { 

    @Autowired 
    User_service user_service; 

    @Override 
    public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException { 
     boolean enabled = true; 
     boolean accountNonExpired = true; 
     boolean credentialsNonExpired = true; 
     boolean accountNonLocked = true; 
     com.domain.User user = user_service.findByUserName(userId); 
     return new User(user.getUserId(), user.getPassword(), enabled, 
          accountNonExpired, credentialsNonExpired, 
          accountNonLocked, this.getGrantedAuthorities(user)); 
    } 

    public List<GrantedAuthority> getGrantedAuthorities(com.domain.User user) { 
     List<GrantedAuthority> role_name = null; 
     List<Role> roles = user.getRoles(); 
     try { 
      for(Role role : roles) { 
       role_name.add(new GrantedAuthorityImpl(role.getName())); 
      } 
     } catch(Exception ex) { 
      System.out.println(ex.toString()); 
     } 
     return role_name; 
    } 

} 

E sto ottenendo questa eccezione:

org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot pass a null GrantedAuthority collection 
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:110) 
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132) 
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156) 
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177) 
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) 
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
at  org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
at java.lang.Thread.run(Thread.java:724) 
Caused by: java.lang.IllegalArgumentException: Cannot pass a null GrantedAuthority collection 
at org.springframework.util.Assert.notNull(Assert.java:112) 
at org.springframework.security.core.userdetails.User.sortAuthorities(User.java:146) 
at org.springframework.security.core.userdetails.User.<init>(User.java:108) 

In getGrantedAuthorities metodo viene trovato oggetto utente, ho testarlo, ma quando voglio aggiungere il nome del ruolo all'elenco GrantedAuthority, genera un'eccezione "Impossibile passare una raccolta GrantedAuthority null". Perché GrantedAuthority return null?

Sono quasi sicuro che l'oggetto "utente" non è nullo, l'ho controllato.

risposta

4

Il tuo role_name è nullo. Prova a cambiare List<GrantedAuthority>role_name=null; entro il List<GrantedAuthority>role_name=new ArrayList<>();.

Inoltre, user.getRoles() deve restituire una raccolta vuota.

+0

No.user.getRoles() non restituisce una raccolta vuota. Ne sono sicuro perché lo stampo. – user3726079

+0

Bene, l'errore è 'Impossibile passare una raccolta GrantedAuthority null', quindi' getGrantedAuthorities' restituisce un valore nullo. Prova almeno a inizializzare 'role_name' e testare. Inoltre, 'role_name.add (new GrantedAuthorityImpl (role.getName()));' genererà un NPE, poiché 'role_name' non è inizializzato, o no? –

+0

funziona per me, grazie. –