|
| 1 | +package com.example.demo; |
| 2 | + |
| 3 | +import javax.sql.DataSource; |
| 4 | + |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 9 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 10 | +import org.springframework.security.config.annotation.web.builders.WebSecurity; |
| 11 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 12 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 13 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 14 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 15 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 16 | + |
| 17 | + |
| 18 | +@EnableWebSecurity |
| 19 | +@Configuration |
| 20 | +public class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 21 | + |
| 22 | + // パスワードエンコーダーBeanの定義 |
| 23 | + @Bean |
| 24 | + public PasswordEncoder passwordEncoder() { |
| 25 | + return new BCryptPasswordEncoder(); |
| 26 | + } |
| 27 | + |
| 28 | + // データソース |
| 29 | + @Autowired |
| 30 | + private DataSource dataSource; |
| 31 | + |
| 32 | + // ユーザーIDとパスワードを取得するSQL |
| 33 | + private static final String USER_SQL = "SELECT" |
| 34 | + + " user_id," |
| 35 | + + " password," |
| 36 | + + " true" |
| 37 | + + " FROM" |
| 38 | + + " m_user" |
| 39 | + + " WHERE" |
| 40 | + + " user_id = ?"; |
| 41 | + |
| 42 | + // ユーザーのロールを取得するSQL |
| 43 | + private static final String ROLE_SQL = "SELECT" |
| 44 | + + " user_id," |
| 45 | + + " role" |
| 46 | + + " FROM" |
| 47 | + + " m_user" |
| 48 | + + " WHERE" |
| 49 | + + " user_id = ?"; |
| 50 | + |
| 51 | + @Override |
| 52 | + public void configure(WebSecurity web) throws Exception { |
| 53 | + // 静的リソースへのアクセスには、セキュリティを適用しない |
| 54 | + web.ignoring().antMatchers("/webjars/**", "/css/**"); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void configure(HttpSecurity http) throws Exception { |
| 59 | + // ログイン不要ページの設定 |
| 60 | + http.authorizeRequests() |
| 61 | + .antMatchers("/webjars/**").permitAll() // webjarsへのアクセス許可 |
| 62 | + .antMatchers("/css/**").permitAll() // cssへのアクセス許可 |
| 63 | + .antMatchers("/login").permitAll() // ログインページは直リンク許可 |
| 64 | + .antMatchers("/signup").permitAll() // ユーザー登録画面は直リンク許可 |
| 65 | + .antMatchers("/admin").hasAuthority("ROLE_ADMIN") // 権限の設定 |
| 66 | + .anyRequest().authenticated(); // それ以外は直リンク禁止 |
| 67 | + |
| 68 | + // ログイン処理 |
| 69 | + http.formLogin() |
| 70 | + .loginProcessingUrl("/login") // ログイン処理のパス |
| 71 | + .loginPage("/login") // ログインページの指定 |
| 72 | + .failureUrl("/login") // ログイン失敗時の遷移先 |
| 73 | + .usernameParameter("userId") // ログインページのユーザーID |
| 74 | + .passwordParameter("password") // ログインページのパスワード |
| 75 | + .defaultSuccessUrl("/home", true); // ログイン成功後の遷移先 |
| 76 | + |
| 77 | + // ログアウト処理 |
| 78 | + http.logout() |
| 79 | + .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) |
| 80 | + .logoutUrl("/logout") |
| 81 | + .logoutSuccessUrl("/login"); |
| 82 | + |
| 83 | + // CSRF対策を無効に設定(一時的) |
| 84 | + // http.csrf().disable(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 89 | + // ログイン処理時のユーザー情報を、DBから取得 |
| 90 | + auth.jdbcAuthentication() |
| 91 | + .dataSource(dataSource) |
| 92 | + .usersByUsernameQuery(USER_SQL) |
| 93 | + .authoritiesByUsernameQuery(ROLE_SQL) |
| 94 | + .passwordEncoder(passwordEncoder()); |
| 95 | + } |
| 96 | +} |
0 commit comments