14

Springで認証サービスを作成しています。

フォーム変数を取得するために UserDetailsS​​ervice を使用していますが、loadUserByUsername には userName という変数が 1 つしかないことがわかりました。

パスワードを取得するには?

public class userAuthentication implements UserDetailsService{

    private @Autowired
    ASPWebServicesUtils aspWebServicesUtils;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {

        //how to get password ?

        User user = new User("test", "test", true, true, true, true, getAuthorities(true));

        return user;  

    }

    private List<GrantedAuthority> getAuthorities(boolean isAdmin){

        List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(2);
        authorityList.add(new SimpleGrantedAuthority("USER_ROLE"));
        if(isAdmin){
            authorityList.add(new SimpleGrantedAuthority("ADMIN_ROLE"));
        }
        return authorityList;

    }
//...
}

ありがとう

4

6 に答える 6

20

Userオブジェクトを見ると、コンストラクターの 2 番目のパラメーターはパスワードです。

UserDetailsS​​erviceは、データベースのようなバックエンド構造からユーザーをロードするために使用されます。ユーザーがユーザー名とパスワードでログインしようとすると、loadUserByUsername メソッドが呼び出されます。ユーザー定義をロードしてセキュリティ フレームワークに返すのは、サービスの役割です必要な詳細には、、、、、、などのデータusernameが含まれます。passwordaccountNonExpiredcredentialsNonExpiredaccountNonLockedauthorities

スプリング セキュリティがユーザー オブジェクトを受け取ると、ユーザーが入力したパスワードと、ユーザー アカウントのステータス (accountNonExpired、credentialNonExpired など) などのその他のデータに対してユーザーを検証します。

于 2013-02-18T14:56:18.543 に答える
17

ユーザー情報を取得して認証情報を提供するための標準的な (すぐに使える) メカニズムには、次のものがあります。

  • インメモリ認証
  • jdbc認証
  • ldap認証
  • ユーザー詳細サービス

上記が目的に合わず、カスタム ソリューションが必要な場合は、次のように新しい認証プロバイダーを作成して構成できます。

セキュリティ構成:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(new CustomAuthenticationProvider());
    }

    ....
}

認証プロバイダー:

public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        String name = authentication.getName();
        // You can get the password here
        String password = authentication.getCredentials().toString();

        // Your custom authentication logic here
        if (name.equals("admin") && password.equals("pwd")) {
            Authentication auth = new UsernamePasswordAuthenticationToken(name,
                    password);

            return auth;
        }

        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

}
于 2015-09-28T20:03:21.257 に答える
3

aUserDetailsServiceは、バックエンドストレージ、データベース、フラットファイルなどからオブジェクトを取得するために使用されることになっていると思います。それを取得しUserDetailsたらUserDetails、Spring Security(またはあなた)はそれをユーザー名(または他のプリンシパル)およびパスワードと比較する必要があります(資格情報)そのユーザーを認証するためにユーザーによって提供されます。

意図したとおりに使っているとは思いません。

于 2013-02-18T14:50:32.433 に答える
0

これloadUserByUsername(String name)は、サービスが実装するインターフェース(私が思うにuserServicedetails)で定義されたメソッドです。実装を書く必要があります。

getPassword() などの実装を作成する必要があるのと同じように... spring はそれを提供しません。パスワードはユーザーオブジェクトに保存されていると思いますが、あなたはそれを書いています...getPassword()メソッドを作成しましたか?

于 2013-02-18T14:46:14.450 に答える