/* * (non-Javadoc) * * @see * org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername * (java.lang.String) */ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = roleResourcesService.getUserByUsername(username); if (user == null) { throw new UsernameNotFoundException("Could not find user " + username); } Set<UserRole> roles = roleResourcesService.findUserRoles(user.getId()); user.setRoles(roles); // 用户权限 Collection<GrantedAuthority> grantedAuths = obtionGrantedAuthorities(user); return new CustomUserDetails(user, grantedAuths); }
// 加载用户权限 private Set<GrantedAuthority> obtionGrantedAuthorities(User user) { Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>(); Set<UserRole> roles = user.getRoles(); for (UserRole role : roles) { // 用户角色对应的资源 Set<Resource> tempRes = P2PFilterInvocationSecurityMetadataSource.roleResouces.get(role.getRoleId()); // 认证用户角色 for (Resource res : tempRes) { authSet.add(new GrantedAuthorityImpl(res.getName())); } } return authSet; }