/** 获取用户Details信息的回调函数. */ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { User user = accountManager.findUserByLoginName(username); if (user == null) { throw new UsernameNotFoundException("用户" + username + " 不存在"); } Set<GrantedAuthority> grantedAuths = obtainGrantedAuthorities(user); // -- hm示例中无以下属性, 暂时全部设为true. --// boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; UserDetails userdetails = new org.springframework.security.core.userdetails.User( user.getLoginName(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuths); return userdetails; }
/** 获得用户所有角色的权限集合. */ private Set<GrantedAuthority> obtainGrantedAuthorities(User user) { Set<GrantedAuthority> authSet = Sets.newHashSet(); for (Role role : user.getRoleList()) { for (Authority authority : role.getAuthorityList()) { authSet.add(new GrantedAuthorityImpl(authority.getPrefixedName())); } } return authSet; }