前提
質問アプリを作成していて、コントローラーの記述が長くなりそうなので
エンティティ(Userクラス)の業務処理は、エンティティ(Userクラス)でメソッド化し
分かりやすくすることが目的です。
メールアドレス認証とgoogle認証を採用しており
メールアドレス認証を行なった時のエラーになります。
実現したいこと
エンティティクラスで、DBからユーザー情報を取得すること。
試したこと
コントローラーで記述していた時は、DBから問題無く取得できますが
エンティティクラスに移動するとnullが返ってきます。
発生している問題・エラーメッセージ
java.lang.NullPointerException: null at com.example.AskMe.security.auth.User.findByUserEmail(User.java:42) ~[main/:na] at com.example.AskMe.security.web.access.AccessController.root(AccessController.java:32) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] 以下、省略
コントローラーに記述しているソースコード(正常系)
UserRepository.java
1@Mapper 2public interface UserRepository { 3 4 @Select("select id, username, email, password, enabled, authority from users where email = #{email}") 5 Optional<User> findByUserEmail(String email); 6} 7
AccessController.java
1@Controller 2@RequiredArgsConstructor 3public class AccessController { 4 5 private final UserRepository userRepository; 6 7 @GetMapping("/") 8 public String root(@AuthenticationPrincipal OidcUser oidcUser, Model model) { 9 User user = new User(); 10 User loginUserInfo = new User(); 11 12 //メールアドレス認証でログインしたユーザ情報を取得 13 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 14 String email = auth.getName(); 15 Optional<User> getUserInfo = userRepository.findByUserEmail(email); 16 loginUserInfo = getUserInfo.get(); 17 18 model.addAttribute("loginUser", loginUserInfo); 19 return "web/root/root"; 20 } 21} 22
User.java
1@Data 2@RequiredArgsConstructor 3public class User { 4 5 private int id; 6 @NotBlank 7 private String username; 8 @NotBlank 9 private String password; 10 11 private String email; 12 13 private int enabled; 14 15 private Authority authority; 16 17 @NotBlank 18 public enum Authority { 19 ADMIN, 20 USER; 21 } 22}
ユーザークラスで記述しているソースコード(異常系)
UserRepository.java
1@Mapper 2public interface UserRepository { 3 4 @Select("select id, username, email, password, enabled, authority from users where email = #{email}") 5 Optional<User> findByUserEmail(String email); 6} 7
@Controller @RequiredArgsConstructor public class AccessController { private final UserRepository userRepository; @GetMapping("/") public String root(@AuthenticationPrincipal OidcUser oidcUser, Model model) { User user = new User(); User loginUserInfo = new User(); //メールアドレス認証でログインしたユーザ情報を取得 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); loginUserInfo = user.getLoginUserInfo(auth, oidcUser); model.addAttribute("loginUser", loginUserInfo); return "web/root/root"; } }
User.java
1@Data 2@RequiredArgsConstructor 3public class User { 4 5 private int id; 6 @NotBlank 7 private String username; 8 @NotBlank 9 private String password; 10 11 private String email; 12 13 private int enabled; 14 15 private Authority authority; 16 17 @NotBlank 18 public enum Authority { 19 ADMIN, 20 USER; 21 } 22 23//finalにすると、自動的にUserクラスのコンストラクターを作らされ、あと引数にUserRepository = nullを入れられ、初期化したソースコードが作られます。。 24 private UserRepository userRepository; 25 26 public User getLoginUserInfo(Authentication auth, @AuthenticationPrincipal OidcUser user) { 27 User loginUser = new User(); 28 29 //DBから取得したユーザ情報を格納する変数 30 Optional<User> getUserInfo = Optional.ofNullable(new User()); 31 32 33 if (auth != null) { 34 //getNameでログインユーザーのEmailを取得 35 String email = auth.getName(); 36 37 //ここでエラーが発生!!!!!!!!!!!!!!!!!! 38 //emailは取得できていることを確認しています。 39 getUserInfo = userRepository.findByUserEmail(email); 40 41 } else if (user != null) { 42 //google認証でここは通りません。 43 getUserInfo = userRepository.findByUserEmail(user.getEmail()); 44 45 } 46 47 loginUser = getUserInfo.get(); 48 return loginUser; 49 50 } 51 52} 53
以上、ご教授お願いします。

あなたの回答
tips
プレビュー