メールアドレスとパスワードでログインしようとすると、
Table '(データベース名).authorities' doesn't exist
というエラーが出た。
この時のテーブル名はuserだったが、
mysql > alter table user rename to authorities;
でauthoritiesに変更。
次に
Unknown column username
やUnknown column authority
といったエラーが出た。
SecurityConfig.java
package com.example.demo;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
private static final String USER_SQL = "SELECT"
+ " email,"
+ " password,"
+" true"
+" FROM"
+" authorities"
+" WHERE"
+" email = ?";
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/webjars/**", "/css/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/css/**").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/signup").permitAll()
.anyRequest().authenticated();
http.formLogin()
.loginProcessingUrl("/login")
.loginPage("/login")
.failureUrl("/login")
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/home", true);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(USER_SQL)
.passwordEncoder(passwordEncoder());
}
}
上記の、.usernameParameter("email")
.passwordParameter("password")
のところで、column名がusernameとauthorityに固定されてしまうらしい。
カラム名をemail→username、password→authorityに変更
mysql > alter table authorities change email username varchar(50);
mysql > alter table authorities change password authority varchar(100);
SecurityConfig.javaの該当部分も変更。
その後ログイン成功。