2011-02-09 71 views
2

如何在Spring中使用自定義用戶詳細信息服務將角色前綴設置爲""Spring Security角色前綴和自定義用戶詳細信息服務

<beans:bean id="authService" class="com.cisco.badges.business.services.AuthenticationService"/> 

<authentication-manager> 
     <authentication-provider user-service-ref="authService"> 
      <password-encoder ref="passwordEncoder"> 
       <salt-source ref="saltSource" /> 
      </password-encoder> 
     </authentication-provider> 
    </authentication-manager> 

@Service("authService") 
public class AuthenticationService extends BaseService implements UserDetailsService, IAuthenticationService { 

    @Autowired 
    IUserRepository userRepository; 

    @Autowired 
    IAuthorityRepository authorityRepository; 

    public AuthenticationService() { 

    } 

    public UserDetails loadUserByUsername(String username) 
      throws UsernameNotFoundException { 

     User user = userRepository.findByUsername(username); 

     if(user == null) 
      throw new UsernameNotFoundException("No user with username '" + username + "' found!"); 

     List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); 

     for (Role role : user.getRoles()) { 
      authList.add(new GrantedAuthorityImpl(role.getName())); 
     } 

     UserAuthentication userAuthentication = new UserAuthentication(user.getUsername(), user.getPassword(), user.getEnabled() == 0 ? false : true, true, true, true, authList); 

     userAuthentication.setSalt(user.getSalt()); 
     userAuthentication.setId(user.getId()); 

     return (UserDetails)userAuthentication; 
    } 
} 

回答

5
<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> 
    <beans:property name="rolePrefix" value="" /> 
</beans:bean> 

就這樣

相關問題