【发布时间】:2016-08-12 08:53:33
【问题描述】:
如何将下面的自定义UserDetailsService 添加到this Spring OAuth2 sample?
默认user 和默认password 在authserver 应用程序的application.properties 文件中定义。
但是,出于测试目的,我想将以下自定义 UserDetailsService 添加到 the demo package of the authserver app:
package demo;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;
@Service
class Users implements UserDetailsManager {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
String password;
List<GrantedAuthority> auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
if (username.equals("Samwise")) {
auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT");
password = "TheShire";
}
else if (username.equals("Frodo")){
auth = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_HOBBIT");
password = "MyRing";
}
else{throw new UsernameNotFoundException("Username was not found. ");}
return new org.springframework.security.core.userdetails.User(username, password, auth);
}
@Override
public void createUser(UserDetails user) {// TODO Auto-generated method stub
}
@Override
public void updateUser(UserDetails user) {// TODO Auto-generated method stub
}
@Override
public void deleteUser(String username) {// TODO Auto-generated method stub
}
@Override
public void changePassword(String oldPassword, String newPassword) {
// TODO Auto-generated method stub
}
@Override
public boolean userExists(String username) {
// TODO Auto-generated method stub
return false;
}
}
如您所见,这个UserDetailsService 还不是autowired,它故意使用不安全的密码,因为它只是为测试目的而设计的。
需要对the GitHub sample app 进行哪些特定更改,以便用户可以使用password=TheShire 登录为username=Samwise,或者使用username=Frodo 登录password=MyRing?发到AuthserverApplication.java 或其他地方?
建议:
Spring OAuth2 Developer Guide 表示使用GlobalAuthenticationManagerConfigurer 全局配置UserDetailsService。然而,谷歌搜索这些名字产生的结果并不那么有用。
另外,一个使用内部 spring 安全性 INSTEAD OF OAuth 的不同应用程序使用以下语法来连接 UserDetailsService,但我不确定如何将其语法调整为当前 OP:
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
@Autowired
private Users users;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(users);
}
}
我尝试在OAuth2AuthorizationConfig 中使用@Autowire 将Users 连接到AuthorizationServerEndpointsConfigurer,如下所示:
@Autowired//THIS IS A TEST
private Users users;//THIS IS A TEST
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter())
.userDetailsService(users)//DetailsService)//THIS LINE IS A TEST
;
}
但是 Spring Boot 日志显示用户 Samwise 没有找到,这意味着 UserDetailsService 没有成功挂接。以下是 Spring Boot 日志的相关摘录:
2016-04-20 15:34:39.998 DEBUG 5535 --- [nio-9999-exec-9] o.s.s.a.dao.DaoAuthenticationProvider :
User 'Samwise' not found
2016-04-20 15:34:39.998 DEBUG 5535 --- [nio-9999-exec-9]
w.a.UsernamePasswordAuthenticationFilter :
Authentication request failed:
org.springframework.security.authentication.BadCredentialsException:
Bad credentials
我还能尝试什么?
【问题讨论】:
标签: spring spring-security spring-boot spring-security-oauth2 spring-oauth2