【发布时间】:2017-04-05 23:13:16
【问题描述】:
我正在尝试对来自 facebook 的用户进行身份验证并存储其用户名,并向将成为我的应用程序管理员的用户子集提供自定义权限。我的问题是,如何在 Oauth2Client 中向经过身份验证的用户提供“管理员”等自定义角色并对其进行授权。
@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Autowired
UserRepository userRepository;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService());
}
@Bean
UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User account = userRepository.findOne(username);
if (account != null) {
List<String> rolesList = userRepository.getRoles(username);
String[] roles = new String[rolesList.size()];
// userRepository.findRoles List<String> roles =
//account.getUserroles().;
User user = new User(account.getUserssoid(), account.getSecretKey(), true, true, true, true,
AuthorityUtils.createAuthorityList(rolesList.toArray(roles)));
return user;
} else {
throw new UsernameNotFoundException("could not find the user '" + username + "'");
}
}
};
}
}
我想使用 OAuth2 客户端做类似的事情。
谢谢
【问题讨论】:
标签: spring spring-security spring-boot spring-oauth2