全部書くと非常にながいので、DBの情報と照合するあたりのコードだけ書きます。
ただし、DBに保存するパスワードの暗号化等は含んでいないので、それは別途お調べください。
APIリファレンスも併せてどうぞ。
lang
1import javax.sql.DataSource;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
9
10
11@Configuration
12@EnableWebMvcSecurity
13public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
14
15 @Autowired
16 DataSource dataSource;
17
18 @Override
19 protected void configure(HttpSecurity http) throws Exception {
20 http
21 .formLogin()
22 .loginPage("/login")
23 .usernameParameter("userName")
24 .passwordParameter("password")
25 .permitAll()
26 .and();
27 }
28
29
30
31 @Autowired
32 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
33 auth
34 .jdbcAuthentication().dataSource(dataSource)
35 .authoritiesByUsernameQuery("select user_name, authority from authorities where user_name = ?")
36 .usersByUsernameQuery("select user_name, password, enabled from users where user_name = ?");
37
38 }
39}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。