どうしたらログインすることができるのでしょうか?
■ 確認したこと
- DBは起動しているか
- コントローラーの遷移先があっているか
HTML
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title>Insert title here</title> 6</head> 7<body> 8 <p th:if="${param.error}"> 9 Error! 10 </p> 11 <h1>Login画面</h1> 12 <form name="form" action="./gettingLocation.html" th:action="@{/login}" method="POST"> 13 <table> 14 <tr> 15 <td>Username:</td> 16 <td><input type="text" name="username" placeholder="Username" /></td> 17 </tr> 18 <tr> 19 <td>Password:</td> 20 <td><input type="password" name="password" placeholder="Password" /></td> 21 </tr> 22 </table> 23 <input type="submit" value="Login" /> 24 </form> 25</body> 26</html> 27
WebSecurityConfig
Java
1package co.jp.yuki.koike.sample.config; 2 3/** 4 * セキュリティー設定クラス 5 */ 6@Configuration 7@EnableWebSecurity 8public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 9 10 /** 11 * UserDetailsService 12 */ 13 @Autowired 14 UserDetailsService userDetailsService; 15 16 17 /** 18 * BCryptPasswordEncoderオブジェクトを返す 19 */ 20 @Bean 21 PasswordEncoder passwordEncoder() { 22 return new BCryptPasswordEncoder(); 23 } 24 25 /* 26 * (非 Javadoc) 27 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity) 28 */ 29 @Override 30 protected void configure(HttpSecurity httpSecurity) throws Exception { 31 httpSecurity.authorizeRequests() 32 .antMatchers("/js/**", "/css/**").permitAll() 33 .antMatchers("/**").authenticated() 34 .and() 35 .formLogin() 36 .loginPage("/loginForm") 37 .loginProcessingUrl("/login") 38 .usernameParameter("username") 39 .passwordParameter("password") 40 .defaultSuccessUrl("/gettingLocation", true) 41 .failureUrl("/loginForm?error=true").permitAll(); 42 } 43 44 /* 45 * (非 Javadoc) 46 * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) 47 */ 48 @Override 49 protected void configure(AuthenticationManagerBuilder authenticationManageruilder) throws Exception { 50 authenticationManageruilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 51 } 52} 53
UserDetailsServiceImpl
Java
1package co.jp.yuki.koike.sample.domain.service.user; 2 3/** 4 * UserDetails実装クラス 5 */ 6public class UserDetailsServiceImpl implements UserDetailsService { 7 8 /** 9 * Logger 10 */ 11 private final static Logger logger = 12 LoggerFactory.getLogger("co.jp.yuki.koike.sample.app.controller.UserDetailsServiceImpl"); 13 14 /** 15 * UserRepository 16 */ 17 @Autowired 18 UserRepository userRepository; 19 20 /* 21 * (非 Javadoc) 22 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String) 23 */ 24 @Override 25 public UserDetails loadUserByUsername(String username) 26 throws UsernameNotFoundException { 27 User user = Optional.ofNullable(userRepository.findOne(username)) 28 .orElseThrow(() -> new UsernameNotFoundException(username + " is not found.")); 29 logger.debug("userId = " + user.getUserId() + "%n" + "password = " + user.getPassword()); 30 return new UserDetailsImpl(user); 31 } 32} 33
Java
1package co.jp.yuki.koike.sample.app.controller; 2 3/** 4 * 位置情報取得画面コントローラー 5 */ 6@Controller 7public class GettingLocationController { 8 9 /** 10 * Logger 11 */ 12 private final static Logger logger = 13 LoggerFactory.getLogger("co.jp.yuki.koike.sample.app.controller.LoginFormController"); 14 15 /** 16 * 位置情報取得画面のView名を返す 17 * 18 * @return 位置情報取得画面のView名 19 */ 20 @RequestMapping("/gettingLocation") 21 String gettingLocation() { 22 logger.info("enter GettingLocationController."); 23 logger.info("exit GettingLocationController."); 24 return "gettingLocation"; 25 } 26} 27
回答1件
あなたの回答
tips
プレビュー