Spring Security認証提供プログラム
3938 ワード
1.概要
本教程では、Spring Securityにアイデンティティ検証提供プログラムを設定する方法を紹介します.簡単なUserDetails Serviceを使用する標準案と比較して、追加の柔軟性を提供します.
2.The Authentication Provider
Spring Securityは、様々なアイデンティティ検証を実行するためのオプションを提供しています.これらはすべて簡単な仕様に従っています.認証要求はAuthentication Providerによって処理され、完全な証拠を有する完全なアイデンティティ検証の対象を返します.
標準および最も一般的な実装は、DaoAuthenticationProvider-それは簡単な読み取り専用ユーザDAOからユーザの詳細情報を検索する-UserDetails Serviceです.このUserDetails Serviceはユーザー名のみにアクセスできます.完全なユーザーエンティティを検索するために使用されます.多くの場合、これで十分です.
より多くの一般的なシーンは、まだ完全な識別認証要求にアクセスする必要があります.例えば、ある外部サードパーティサービス(例えばCrowd)に対してアイデンティティ認証を行う場合、アイデンティティ認証要求からのユーザ名とパスワードが必要となります.
これらのより高度なスキームに対しては、カスタム認証提供プログラムを定義する必要があります.
3.Authentication Providerを登録する
ID認証提供プログラムを定義した上は、利用可能な名前空間サポートを使ってXMLセキュリティ設定で指定する必要があります.
次に、対応するJavaの構成を見てみましょう.
バックエンドでこのカスタム識別認証提供プログラムを使用するかどうかに関わらず、クライアントからの認証要求は基本的に同じです.簡単なcurlコマンドを使用して認証要求を送信することができます.
私達はサーバーから予想の200 OKに戻ります.
本論文では、Spring Securityのカスタムアイデンティティ検証提供プログラムの例を議論した.
GitHubプロジェクトの中で本教程の完全な実現を見つけることができます.これはMavenに基づくプロジェクトですので、簡単に導入して実行するべきです.
本教程では、Spring Securityにアイデンティティ検証提供プログラムを設定する方法を紹介します.簡単なUserDetails Serviceを使用する標準案と比較して、追加の柔軟性を提供します.
2.The Authentication Provider
Spring Securityは、様々なアイデンティティ検証を実行するためのオプションを提供しています.これらはすべて簡単な仕様に従っています.認証要求はAuthentication Providerによって処理され、完全な証拠を有する完全なアイデンティティ検証の対象を返します.
標準および最も一般的な実装は、DaoAuthenticationProvider-それは簡単な読み取り専用ユーザDAOからユーザの詳細情報を検索する-UserDetails Serviceです.このUserDetails Serviceはユーザー名のみにアクセスできます.完全なユーザーエンティティを検索するために使用されます.多くの場合、これで十分です.
より多くの一般的なシーンは、まだ完全な識別認証要求にアクセスする必要があります.例えば、ある外部サードパーティサービス(例えばCrowd)に対してアイデンティティ認証を行う場合、アイデンティティ認証要求からのユーザ名とパスワードが必要となります.
これらのより高度なスキームに対しては、カスタム認証提供プログラムを定義する必要があります.
@Component
public class CustomAuthenticationProvider
implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if (shouldAuthenticateAgainstThirdPartySystem()) {
// use the credentials
// and authenticate against the third-party system
return new UsernamePasswordAuthenticationToken(
name, password, new ArrayList<>());
} else {
return null;
}
}
@Override
public boolean supports(Class> authentication) {
return authentication.equals(
UsernamePasswordAuthenticationToken.class);
}
}
リターンされたAuthenticationオブジェクトに設定されている付与権限は空です.これは当然、権限がアプリケーションに特定されているからです.3.Authentication Providerを登録する
ID認証提供プログラムを定義した上は、利用可能な名前空間サポートを使ってXMLセキュリティ設定で指定する必要があります.
4.Java Configration次に、対応するJavaの構成を見てみましょう.
@Configuration
@EnableWebSecurity
@ComponentScan("org.baeldung.security")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider authProvider;
@Override
protected void configure(
AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic();
}
}
5.試験認証バックエンドでこのカスタム識別認証提供プログラムを使用するかどうかに関わらず、クライアントからの認証要求は基本的に同じです.簡単なcurlコマンドを使用して認証要求を送信することができます.
curl --header "Accept:application/json" -i --user user1:user1Pass
http://localhost:8080/spring-security-custom/api/foo/1
本例の目的のために、基本的なIDを使ってREST APIを保護しました.私達はサーバーから予想の200 OKに戻ります.
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B8F0EFA81B78DE968088EBB9AFD85A60; Path=/spring-security-custom/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 02 Jun 2013 17:50:40 GMT
まとめます本論文では、Spring Securityのカスタムアイデンティティ検証提供プログラムの例を議論した.
GitHubプロジェクトの中で本教程の完全な実現を見つけることができます.これはMavenに基づくプロジェクトですので、簡単に導入して実行するべきです.