前提・実現したいこと
SpringSecurityのPasswordEncoderでハッシュ化したパスワードでログインをすることができません。
やりたいこととしては、現在アカウントのパスワード変更機能を作ってるのですが、あらかじめハッシュ化してDBに登録していたパスワードを、画面から入力されたパスワードをハッシュ化してupdateをかけて新しいパスワードでログインできるようにしたいのですが、パスワードを変更した後、新しいパスワードでログインをすることができません。DBは更新されているのですが、、、
ご教授お願いいたします。
該当のソースコード
String newPassHash = passwordEncoder.encode(newPass);
↑newPassが新しく変更をするパスワードです。
passwordChangeService.passwordChangeGo(passwordChange);
↑でDBにupdateをかける処理をしています。(DBは確実に更新できてるので、記載を省略します。)
java
1 @RequestMapping(value = "password-change-go", method = RequestMethod.POST) 2 public ModelAndView passwordChangeGo(@Valid @ModelAttribute PasswordChange passwordChange, BindingResult bindingResult, 3 ModelAndView mav, Principal principal){ 4 //ログインユーザのメールアドレス取得 5 String address = principal.getName(); 6 //ログインユーザの現在のパスワードを取得 7 PasswordChangeCollation passwordChangeCollation = passwordChangeService.passwordChangeCol(address); 8 String dbPass = passwordChangeCollation.getPassword(); 9 //入力したパスワード 10 String inPass = passwordChange.getNowPassword(); 11 //パスワード照合 12 if (passwordEncoder.matches(inPass, dbPass)) { 13 System.out.println("一致"); 14 //新しいパスワードを取得 15 String newPass = passwordChange.getNewPassword(); 16 //確認用パスワードを取得 17 String conPass = passwordChange.getConfPassword(); 18 //新しいパスワードと確認用パスワードの照合 19 if(newPass.equals(conPass)){ 20 System.out.println("確認OK"); 21 //入力されたパスワードのハッシュ化 22 String newPassHash = passwordEncoder.encode(newPass); 23 passwordChange.setNewPasswordHash(passwordEncoder.encode(newPassHash)); 24 //パスワード変更処理 25 passwordChange.setUsername(address); 26 passwordChangeService.passwordChangeGo(passwordChange); 27 }else{ 28 System.out.println("確認NG"); 29 } 30 }else{ 31 System.out.println("不一致"); 32 } 33 return null; 34 }
ログイン部分?は以下です
java
1package com.kproject01.impl; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.security.core.userdetails.UserDetails; 5import org.springframework.security.core.userdetails.UserDetailsService; 6import org.springframework.security.core.userdetails.UsernameNotFoundException; 7import org.springframework.stereotype.Service; 8 9import com.kproject01.data.LoginUserData; 10import com.kproject01.entity.MUser; 11import com.kproject01.repository.UserRepository; 12 13@Service 14public class LoginUserDetailsServiceImpl implements UserDetailsService { 15 16 @Autowired 17 UserRepository userRepository; 18 19 @Override 20 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 MUser user = userRepository.findByUsername(username); 22 23 if (user == null) { 24 throw new UsernameNotFoundException("not found"); 25 } 26 return new LoginUserData(user); 27 } 28 29} 30
一応コンフィグ部分も記載します。
java
1package com.kproject01.config; 2 3import org.springframework.context.annotation.Bean; 4import org.springframework.context.annotation.Configuration; 5import org.springframework.security.config.annotation.web.builders.HttpSecurity; 6import org.springframework.security.config.annotation.web.builders.WebSecurity; 7import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10import org.springframework.security.crypto.password.PasswordEncoder; 11 12@Configuration 13@EnableWebSecurity 14public class SecurityConfig extends WebSecurityConfigurerAdapter { 15 16 @Override 17 public void configure(WebSecurity web) throws Exception { 18 web.ignoring().antMatchers("/webjars/**", "/favicon.ico", "/css/**", 19 "/js/**", "/img/**"); 20 } 21 22 @Override 23 protected void configure(HttpSecurity http) throws Exception { 24 http.authorizeRequests().antMatchers("/") 25 .permitAll().antMatchers("/admin").hasRole("ADMIN") 26 .anyRequest().authenticated().and().formLogin() 27 .loginProcessingUrl("/authenticate").loginPage("/") 28 .failureUrl("/?error").defaultSuccessUrl("/user-top", true) 29 .usernameParameter("username").passwordParameter("password") 30 .and().logout().logoutSuccessUrl("/").and(); 31 } 32 33 @Bean 34 PasswordEncoder passwordEncoder() { 35 return new BCryptPasswordEncoder(); 36 } 37 38}
補足情報(FW/ツールのバージョンなど)
java8
SpringBoot2.0
回答2件
あなたの回答
tips
プレビュー