ログイン機能と会員登録機能を作成しています。
会員登録機能で、キャッシュ化した値をDBに登録までは完了していますが、
その値でログインしようとしても、
エラーページに飛び、原因が分からずにいます。
package com.example.domain.config; 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.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; import org.springframework.security.config.annotation.web.builders.WebSecurity; import com.example.domain.service.UserDetailsServiceImpl; /** * SpringSecurityを利用するための設定クラス * ログイン処理でのパラメータ、画面遷移や認証処理でのデータアクセス先を設定する * @author * */ @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired UserDetailsServiceImpl userDetailsService; @Autowired PasswordEncoder passwordEncoder; /** * パスワードのハッシュ化を行うアルゴリズムを返す */ @Bean public BCryptPasswordEncoder passwordEncoder() { BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); return bCryptPasswordEncoder; } /** * 認可設定を無視するリクエストを設定 * 静的リソース(image,javascript,css)を認可処理の対象から除外する */ @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers( "/images/**", "/css/**", "/javascript/**" ); } /** * 認証・認可の情報を設定する * 画面遷移のURL・パラメータを取得するname属性の値を設定 */ @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/signup") .permitAll() .anyRequest().authenticated(); // ログイン http.formLogin() .loginPage("/login") //ログインページはコントローラを経由しないのでViewNameとの紐付けが必要 .loginProcessingUrl("/login") //フォームのSubmitURL、このURLへリクエストが送られると認証処理が実行される .usernameParameter("loginId") //リクエストパラメータのname属性を明示 .passwordParameter("password") .successForwardUrl("/index") .failureUrl("/login?error") .permitAll(); http .logout() .logoutUrl("/logout") .logoutSuccessUrl("/login?logout") .permitAll(); } /** * 認証時に利用するデータソースを定義する設定メソッド * ここではDBから取得したユーザ情報をuserDetailsServiceへセットすることで認証時の比較情報としている * @param auth * @throws Exception */ @Autowired public void configure(AuthenticationManagerBuilder auth) throws Exception{ auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); /* auth .inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); */ } }
package com.example.domain.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; /** * DBに入れる値を格納するクラス。 */ @Entity @Data @NoArgsConstructor @AllArgsConstructor @Table(name = "accountUser") public class UserEntity{ @Id @GeneratedValue private int id; @Column(nullable = false) private String loginid; @Column(nullable = false) private String password; }
package com.example.domain.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import javax.persistence.*; import java.util.Collection; /** * DBに入れる値を格納するクラス。 */ @Entity @Data @NoArgsConstructor @AllArgsConstructor @Table(name = "accountUser") public class UserEntity implements UserDetails{ @Id @GeneratedValue private int id; @Column(nullable = false) private String loginid; @Column(nullable = false) private String password; /* (非 Javadoc) * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities() */ @Override public Collection<? extends GrantedAuthority> getAuthorities() { return null; } /* (非 Javadoc) ##############ログインID########### * @see org.springframework.security.core.userdetails.UserDetails#getUsername() */ @Override public String getUsername() { return this.loginid; } /* (非 Javadoc) * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired() */ @Override public boolean isAccountNonExpired() { return true; } /* (非 Javadoc) * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked() */ @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return true; } }
package com.example.domain.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import com.example.domain.entity.UserEntity; import com.example.domain.repository.UserRepository; /** * Spring Securityのユーザ検索用のサービスの実装クラス * DataSourceの引数として指定することで認証にDBを利用できるようになる */ @Service public class UserDetailsServiceImpl implements UserDetailsService{ //DBからユーザ情報を検索するメソッドを実装したクラス @Autowired private UserRepository userRepository; /** * UserDetailsServiceインタフェースの実装メソッド * フォームから取得したユーザ名でDBを検索し、合致するものが存在したとき、 * パスワード、権限情報と共にUserDetailsオブジェクトを生成 * コンフィグクラスで上入力値とDBから取得したパスワードと比較し、ログイン判定を行う */ @Override public UserDetails loadUserByUsername(String loginid) throws UsernameNotFoundException { UserEntity userEntity = userRepository.findUser(loginid); if (userEntity == null) { throw new UsernameNotFoundException("User" + loginid + "was not found in the database"); } return userEntity; } } ```ここに言語を入力 コード

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。