如何将以下自定义添加UserDetailsService
到此Spring OAuth2 示例中?
默认值user
和默认值在应用程序的文件中password
定义。 application.properties
authserver
但是,我想将以下自定义添加UserDetailsService
到应用程序的demo
包中以authserver
进行测试:
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
是,它故意使用不安全的密码,因为它只是为了测试目的而设计的。
需要对GitHub 示例应用程序进行哪些特定更改,以便用户可以username=Samwise
使用password=TheShire
或username=Frodo
使用登录password=MyRing
? 是否需要对AuthserverApplication.java
其他地方或其他地方进行更改?
建议:
Spring OAuth2 开发人员指南说要使用a全局GlobalAuthenticationManagerConfigurer
配置 a UserDetailsService
。然而,谷歌搜索这些名字产生的结果并不那么有用。
此外,使用内部 spring security 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);
}
}
我尝试使用@Autowire
内部OAuth2AuthorizationConfig
连接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
我还能尝试什么?