春のセキュリティ私の基本原則を覚えておいてください:
ログインすると、リクエストがフィルターに送信されます。フィルターが正常に認証されると、RembermMeserviceが呼び出され、トークンが生成され、トークンをブラウザCookieに書き込みます。同時に、TokenMeserviceにはTokenRepositoryがあり、トークンとユーザー情報をデータベースに書き込みます。このようにして、ユーザーが再びシステムにアクセスしてインターフェイスにアクセスすると、rememberMeauthenticationFilterフィルターを通過します。 Cookieのトークンを読み取り、Rememberserviceに引き渡します。 rememberserviceは、tokenRepositoryを使用して、トークンに基づいてデータベースからレコードがあるかどうかを確認します。レコードがある場合、ユーザー名が取り出され、ユーザー名に基づいてユーザー情報を取得してsecuritycontextに配置してユーザー情報を呼び出します。
RememberMeAuthenticationFilter 、Spring Securityにおけるフィルターチェーンの2番目の最後のフィルター位置を認証します。他の認証フィルターを正常に認証できない場合、認証を試すためにrememberMeauthenticationFilterが呼び出されます。
成し遂げる:
1。 <input type="checkbox" name="remember-me" value="true"/>にログインフォームに、 SpringSessionRememberMeServicesクラスで定数を定義し、デフォルト値はremember-meです
2。上記の概略図によると、tokenRepositoryを構成し、生成されたトークンをデータベースに保存する必要があります。これは構成bean構成であり、browsersecurityconfigに配置されています
3.設定で構成
4. browserpropertiesに自動ログイン時間を追加して、設定可能にします
//プライベートint remembermeseconds = 10; qihuo package com.imooc.s@configuration //これは構成であるパブリッククラスBrowsersecurityConfig extends websecurityconfigurerAdapter {//ユーザー構成@Autowired SecuritySurceProperties; //カスタマイズされたプロセッサ@autowired private hunitionsuccesshandler imoocauthenticationsucceshandlerに成功した後; //認証に失敗した後のカスタマイズされたプロセッサ@autowired private private authenticationfailurehandler imoocauthenticationfailurehandler; //データソース@autowired private dataSource DataSource; @autowired private userdetailsservice userdetailsservice; //それはorg.springframework.security.crypto.password.passwordencoder @bean publicsedencoder passworddencoder(){// bcryptpasswordencoderを実装していることに注意してください。 } / *** tokenRepositoryの構成を覚えておいてください。ログイン後に実行することを覚えておいてください*ログイン後にログインした後、データベースにトークンを保存します* @description:tokenRepository構成を覚えておいてください。 PersistentTokenRepository PersistentTokenRepository(){jdbctokenRepositoryimpl jdbctokenRepository = new jdbctokenrepositoryimpl(); jdbctokenRepository.setDataSource(dataSource); //対応するテーブルは、起動時に自動的に生成されます。 jdbctokenrepositoryimplでcreate_table_sqlスクリプトを実行して、テーブルjdbctokenrepository.setcreatetableonstartup(true)を生成できます。 jdbctokenrepositoryを返します。 } //バージョン2:構成可能ログインページ@OverRideプロテクションVoid Configure(httpsecurity http)スロー例外{//検証コードフィルターvalidatecodefilter validatecodefilter = new validatecodefilter(); //検証コードフィルターで独自のエラー処理を使用してくださいvalidAtecodefilter.setAuthenticationFailureHandler(imoocothenticationFailureHandler); //構成検証コードフィルターurl balidatecodefilter.setsecurityproperties(securityproperties); validatecodefilter.afterpropertiesset(); //認証、セキュリティ=認証 +認証が必要なインターフェイスジャンプログイン//http.httpbasic()//これはデフォルトのポップアップボックス認証//ログインフィルターの前に検証コードフィルターをロードする.loginpage( "/authentication/require")//ユーザー認証BrowsersecurityController //ログインフィルターusernamepasswordauthenticationfilterデフォルトのログインURLは「/login」です。 postprocessor.failurehandler(imoocothenticationfailurehandler)//ログイン後の処理障害後に処理してください。 tokenvalidityseconds(securityproperties.getBrowser()。getRememberMeseConds())// seconds.userdetailsservice(userdetailsservice)//覚えておいてください。 SecurityProperties.getBrowser()。getLoginPage():ユーザー構成login page.antmatchers( "/authentication/require"、securityproperties.getBrowser()。 //NANYREQUEST.AUTHENTICTECT()//認証認証が必要です。And().csrf()。disable()// csrf Protection; }} curity.browser;データベースに対処したいので、データソース:Application.Propertiesを注入する必要があります
spring.datasource.datasource.driver-class-name = com.mysql.jdbc.driverspring.datasource.url = jdbc:mysql://127.0.0.1:3306/imooc-demospring.datasource.username = rootspring.datasource.datasource.password
アプリケーションを開始し、LocalHost:8080/userにアクセスしてください。ログインが必要です
正常にログインする:
データベース:persistent_loginsテーブルを生成し、データを保存します
サービスを停止して再度開始します( jdbcTokenRepository.setCreateTableOnStartup(true); )ユーザーログイン情報がセッション中であるため、サービスを再起動した後、ログインページに再起動する必要がありますが、覚えているように設定しているため、直接アクセスしてインターフェイスデータを取得できます。
リクエストヘッダー:
これまでのところ、基本的な記憶は完了しています
完全なコードはgithubに配置されています:https://github.com/lhy1234/spring-security
要約します
上記は、編集者が紹介したSpring Security Construction Restサービスです。私を覚えてますか。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!