SpringBoot入門


5-2 掲示板への認証機能の追加

⑦認証関係の環境ファイルを作成します

SecurityConfig.java

package com.example.demo; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders .HttpSecurity; import org.springframework.security.config.annotation.web.builders .WebSecurity; import org.springframework.security.config.annotation.web.configuration .EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration .WebSecurityConfigurerAdapter; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password .Pbkdf2PasswordEncoder; @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/webjars/**", "/css/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/loginForm").permitAll() .anyRequest().authenticated() .and() .formLogin().loginProcessingUrl("/login") .loginPage("/loginForm") .failureUrl("/loginForm?error") .defaultSuccessUrl("/", true) .usernameParameter("username") .passwordParameter("password") .and() .logout() .logoutSuccessUrl("/loginForm"); } @Bean PasswordEncoder passwordEncoder() { return new Pbkdf2PasswordEncoder(); } }