web.xmlの構成
春・クラウド・スターター・セキュリティと春 - セキュリティのOAuth2-自動構成2つの依存関係を追加します。
</ 依存関係> <! - 春の雲スターター:セキュリティ- > <! - 含める:ウェブ、アクチュエータ、セキュリティ、zuulなど- > < 依存> < groupIdを> org.springframework.cloud </ groupIdを> < たartifactId >春・クラウド・スターター・セキュリティ</ たartifactId > </ 依存関係> <! - 春のセキュリティのOAuth2自動設定(2.1の後、バネのクラウド・セキュリティではオプション)- > < 依存> < groupIdを>org.springframework.security.oauth.boot </ groupIdを> < たartifactId >春・セキュリティのOAuth2-自動設定</ たartifactId > </ 依存関係>
また、ユーレカクライアントとコンフィグクライアントであり、どのように前のセクションを参照してくださいユーレカクライアントコンフィグクライアントを設定するには。
設定WebSecurity社
パッケージcom.mytools.config。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.context.annotation.Bean。 輸入org.springframework.context.annotation.Configuration。 輸入org.springframework.security.authentication.AuthenticationManager。 輸入org.springframework.security.config.annotation.web.builders.HttpSecurity。 輸入org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter。 輸入org.springframework.security.core.userdetails.UserDetailsService。 輸入org.springframework.security.crypto.factory.PasswordEncoderFactories。 輸入org.springframework.security.crypto.password.PasswordEncoder。 / ** *春のセキュリティ設定。 * / @Configuration パブリック クラス WebSecurityConfigurationは拡張WebSecurityConfigurerAdapter { @Autowired プライベートUserDetailsService userDetailsServiceを。 / ** *パスワードencodeer * / @Bean 公共PasswordEncoder passwordEncoder(){ 返す(PasswordEncoderFactories.createDelegatingPasswordEncoderを)。 } @Override @Bean 公共 AuthenticationManager authenticationManagerBean()スロー例外{ 戻り スーパー)(.authenticationManagerBean。 } / * (非Javadocの) * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#構成(org.springframework.security.config.annotation.web.builders.HttpSecurity) * / @Override 保護 空 configureが(HttpSecurity HTTP)スロー例外は{ // @formatter:オフ http.authorizeRequests()// のconfigure要求ルールの承認を .antMatchers( "/インデックス" ).permitAllを() //.antMatchers( "/ URL / **")。hasRoleも( "ADMIN")// いくつかのURLが持つアクセスADMIN // .anyRequest()。(認証済み)// 他の要求が認証する必要があり .AND() .formLogin( )// フォームとしてログイン .loginPage( "/ログイン")// ログインURL(デフォルトはフレームワークとログインページです) // .defaultSuccessUrl( "/インデックス")// ログイン成功URL(デフォルトはインデックスである) .failureUrl(」 /ログイン・エラー」)// ログインURL失敗 .ANDを() // .logout()//ログアウト設定 // .logoutUrl( "/ログアウト")// ログアウトURL(デフォルトはログアウトです) // .logoutSuccessUrl( "/インデックス")// ログアウト成功URL(デフォルトはログインです) .rememberMe()// 私を忘れないでください 。キー( "uniqueAndSecret")// トークンの内容を生成 .tokenValiditySecondsを(60 * 60 * 24 * 30)// 30日 .userDetailsService(userDetailsService)// 私に機能覚えのためUserDetailsServiceを登録 // .AND()を // .httpBasic()// アプリケーションのため(ヘッダ)にHTTP基本認証を使用します 。 // 上:@formatter } }
説明:
(1)