題のとおりでDBに登録したユーザーでログインしようとしたらエラーがでます。原因がわかりません。
下のコードの
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import mrs.domain.model.User;
import mrs.domain.repository.user.UserRepository;
@Service
public class ReservationUserDetailsService implements UserDetailsService{
@Autowired
UserRepository userRepository;
@Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{ User user = userRepository.findById(username).orElse(null); if(user == null) { throw new UsernameNotFoundException(username + "is not found"); } return new ReservationUserDetails(user); }
}
### data.sql(一部)とschema.sql(一部) ```ここに言語を入力 data.sql INSERT INTO usr (user_id,first_name,last_name,password,role_name) VALUES ('aaaa','Aaa','Aaa','$2a$10$F4zdLJMMxjJECgmAUpYUYezPVGbbSLdxz2HUcAquJNbiKDAzupF3i','USER'); schema.sql CREATE TABLE IF NOT EXISTS usr ( user_id VARCHAR(255) NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, role_name VARCHAR(255) NOT NULL, PRIMARY KEY (user_id) );
WebSecurityConfig
java
1@Configuration 2//Spring Securityのweb連携機能を有効にする 3@EnableWebSecurity 4public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 5 @Autowired 6 ReservationUserDetailsService userDetailsService; 7 @Bean 8 PasswordEncoder passwordEncoder() { 9 return new BCryptPasswordEncoder(); 10 } 11 @Override 12 protected void configure(HttpSecurity http)throws Exception{ 13 http.authorizeRequests() 14 .antMatchers("/js/**","/css/**").permitAll() 15 .antMatchers("/**").authenticated() 16 .and() 17 .formLogin() 18 .loginPage("/loginForm") 19 .loginProcessingUrl("/login") 20 .usernameParameter("username") 21 .passwordParameter("password") 22 .defaultSuccessUrl("/rooms",true) 23 .failureUrl("/loginForm?error=true").permitAll(); 24 } 25 @Override 26 protected void configure(AuthenticationManagerBuilder auth) throws Exception{ 27 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 28 } 29} 30
loginForm.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title></title> </head> <body> <h3>ログインフォーム</h3> <p th:if="${param.error}"> Error! </p> <form th:action="@{/login}" method="POST"> <table> <tr> <td><label for="username">User:</label></td> <td><input type="text" id="username" name="username" value="aaaa"/></td> </tr> <tr> <td><label for="password">Password:</label></td> <td><input type="password" id="password" name ="password" value="demo"/></td> <tr> <td> </td> <td><button type="submit">ログイン</button></td> </tr> </table> </form> </body> </html>
あなたの回答
tips
プレビュー