Spring Securityを試してみたときのメモ
Spring BootをいろいろイジってみてSpring Securityについて簡単なメモを。
今回つかったもの
まずはpom.xmlに依存関係を追加する
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
次に
WebSecurityConfigurerAdapterを継承してconfigクラスを作成
URLによって権限を設定をするために
configure(HttpSecurity)をOverrideする
@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/loginform").permitAll() .anyRequest().authenticated(); http .formLogin() .loginProcessingUrl("/login") .loginPage("/loginform") .failureUrl("/loginform?error") .usernameParameter("username") .passwordParameter("password") .and(); http .logout() .logoutRequestMatcher(new AntPathRequestMatcher("/logout**")) .logoutSuccessUrl("/loginform"); }
上記にて
・ログインページ(/loginform)は全員がアクセス可能
・それ以外は認証後のみ閲覧可
・ログイン時のパラメータ名はusername、passwordとする
・ログイン失敗時は /loginform?error へ遷移する
・ログアウト処理
これらを設定したことになります。
画面側で静的コンテンツに対しては認証認可から除外設定にするため、
configure(WebSecurity)をOverrideし、設定する
@Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/css/**", "/js/**", "/img/**"); }
後半に続きます。