質問編集履歴

2

誤字

2022/05/29 01:03

投稿

teratail937645
teratail937645

スコア10

test CHANGED
File without changes
test CHANGED
@@ -2,11 +2,6 @@
2
2
  会員登録機能で、キャッシュ化した値をDBに登録までは完了していますが、
3
3
  その値でログインしようとしても、
4
4
  エラーページに飛び、原因が分からずにいます。
5
-
6
-
7
- <変更した点があるので追記します>
8
- - EntityにUserDetailsをインポート
9
- <変化した点>正しいID・PASSでログインすると404のエラー、不正なログインID・PASSでログインはログインページに戻るように変化。"/index"はあるのになぜだか悩んでいます
10
5
 
11
6
 
12
7
  ```ここに言語を入力

1

EntityにUserDetailsをインポート

2022/05/29 00:38

投稿

teratail937645
teratail937645

スコア10

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,12 @@
2
2
  会員登録機能で、キャッシュ化した値をDBに登録までは完了していますが、
3
3
  その値でログインしようとしても、
4
4
  エラーページに飛び、原因が分からずにいます。
5
+
6
+
7
+ <変更した点があるので追記します>
8
+ - EntityにUserDetailsをインポート
9
+ <変化した点>正しいID・PASSでログインすると404のエラー、不正なログインID・PASSでログインはログインページに戻るように変化。"/index"はあるのになぜだか悩んでいます
10
+
5
11
 
6
12
  ```ここに言語を入力
7
13
  package com.example.domain.config;
@@ -137,21 +143,78 @@
137
143
  ```
138
144
 
139
145
  ```ここに言語を入力
140
- package com.example.domain.repository;
146
+ package com.example.domain.entity;
141
-
147
+
142
- import org.springframework.data.jpa.repository.JpaRepository;
148
+ import lombok.AllArgsConstructor;
149
+ import lombok.Data;
150
+ import lombok.NoArgsConstructor;
143
- import org.springframework.stereotype.Repository;
151
+ import org.springframework.security.core.GrantedAuthority;
144
- import com.example.domain.entity.UserEntity;
145
- import org.springframework.data.jpa.repository.Query;
152
+ import org.springframework.security.core.userdetails.UserDetails;
153
+
146
-
154
+ import javax.persistence.*;
155
+ import java.util.Collection;
156
+
147
- /**
157
+ /**
148
- * UserEntity登録・検索するクラス
158
+ * DBに入れる値格納するクラス
149
- */
159
+ */
150
- @Repository
160
+ @Entity
161
+ @Data
162
+ @NoArgsConstructor
163
+ @AllArgsConstructor
164
+ @Table(name = "accountUser")
151
- public interface UserRepository extends JpaRepository<UserEntity, Long> {
165
+ public class UserEntity implements UserDetails{
166
+
152
-
167
+ @Id
168
+ @GeneratedValue
169
+ private int id;
170
+
153
- @Query(value = "SELECT * FROM account_user WHERE account_user.loginid = ?1", nativeQuery = true)
171
+ @Column(nullable = false)
154
- UserEntity findUser(String loginid);
172
+ private String loginid;
173
+
174
+ @Column(nullable = false)
175
+ private String password;
176
+
177
+ /* (非 Javadoc)
178
+ * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
179
+ */
180
+ @Override
181
+ public Collection<? extends GrantedAuthority> getAuthorities() {
182
+ return null;
183
+ }
184
+
185
+ /* (非 Javadoc) ##############ログインID###########
186
+ * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
187
+ */
188
+ @Override
189
+ public String getUsername() {
190
+ return this.loginid;
191
+ }
192
+
193
+ /* (非 Javadoc)
194
+ * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
195
+ */
196
+ @Override
197
+ public boolean isAccountNonExpired() {
198
+ return true;
199
+ }
200
+
201
+ /* (非 Javadoc)
202
+ * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
203
+ */
204
+ @Override
205
+ public boolean isAccountNonLocked() {
206
+ return true;
207
+ }
208
+
209
+ @Override
210
+ public boolean isCredentialsNonExpired() {
211
+ return true;
212
+ }
213
+
214
+ @Override
215
+ public boolean isEnabled() {
216
+ return true;
217
+ }
155
218
  }
156
219
  ```
157
220
 
@@ -198,21 +261,11 @@
198
261
  if (userEntity == null) {
199
262
  throw new UsernameNotFoundException("User" + loginid + "was not found in the database");
200
263
  }
201
- //権限のリスト
202
- //AdminやUserなどが存在するが、今回は利用しないのでUSERのみを仮で設定
203
- //権限を利用する場合は、DB上で権限テーブル、ユーザ権限テーブルを作成し管理が必要
204
- List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();
205
- GrantedAuthority authority = new SimpleGrantedAuthority("USER");
206
- grantList.add(authority);
207
-
208
- //rawDataのパスワードは渡すことができないので、暗号化
209
- BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
210
-
211
- //UserDetailsはインタフェースなのでUserクラスのコンストラクタで生成したユーザオブジェクトを
212
- UserDetails userDetails = (UserDetails)new User(userEntity.getLoginid(), encoder.encode(userEntity.getPassword()),grantList);
213
-
214
- return userDetails;
264
+ return userEntity;
215
265
  }
216
266
 
217
267
  }
268
+
269
+ ```ここに言語を入力
270
+ コード
218
- ```
271
+ ```