質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

2026閲覧

SpringBoot ログイン認証 DBに登録されたPASSとフォームから入力されたPASSの照合が上手くいかない

teratail937645

総合スコア10

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

1クリップ

投稿2022/05/28 23:23

編集2022/05/29 11:53

ログイン機能と会員登録機能を作成しています。
会員登録機能で、キャッシュ化した値を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; } } ```ここに言語を入力 コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

WebSecurityConfigクラスのWebSecurityConfigの.successForwardUrl("/index")の部分を.defaultSuccessUrl("/index")に変えたら遷移した

投稿2022/05/29 02:53

teratail937645

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問