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 Developer Guideには、 a を使用して a をグローバルGlobalAuthenticationManagerConfigurer
に構成するように記載されています。UserDetailsService
ただし、これらの名前をグーグルで調べても、役立つ結果は得られません。
また、内部スプリング セキュリティ 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
他に何を試すことができますか?