|
|
@@ -11,6 +11,7 @@ import jp.yamoto.farm.common.core.service.TokenService;
|
|
|
import jp.yamoto.farm.common.enums.AuthStatusEnum;
|
|
|
import jp.yamoto.farm.common.enums.UserStatus;
|
|
|
import jp.yamoto.farm.common.exception.ServiceException;
|
|
|
+import jp.yamoto.farm.common.exception.user.AuthVerifyCodeNotMatchException;
|
|
|
import jp.yamoto.farm.common.exception.user.UserNotExistsException;
|
|
|
import jp.yamoto.farm.common.exception.user.UserPasswordNotMatchException;
|
|
|
import jp.yamoto.farm.common.manager.AsyncManager;
|
|
|
@@ -18,10 +19,7 @@ import jp.yamoto.farm.common.manager.factory.AsyncFactory;
|
|
|
import jp.yamoto.farm.common.security.context.AuthenticationContextHolder;
|
|
|
import jp.yamoto.farm.common.utils.*;
|
|
|
import jp.yamoto.farm.common.utils.ip.IpUtils;
|
|
|
-import jp.yamoto.farm.sankin.biz.auth.domain.AuthReSendVerifyCodeBo;
|
|
|
-import jp.yamoto.farm.sankin.biz.auth.domain.AuthResetPwdBo;
|
|
|
-import jp.yamoto.farm.sankin.biz.auth.domain.AuthSecondVerifyBo;
|
|
|
-import jp.yamoto.farm.sankin.biz.auth.domain.AuthSendVerifyCodeBo;
|
|
|
+import jp.yamoto.farm.sankin.biz.auth.domain.*;
|
|
|
import jp.yamoto.farm.sankin.biz.mapper.FarmerUserMapper;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -36,6 +34,9 @@ import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
/**
|
|
|
* 農家 認証
|
|
|
*
|
|
|
@@ -74,8 +75,10 @@ public class AuthService {
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
}
|
|
|
|
|
|
- // email
|
|
|
- return userDetails.getMailAddress();
|
|
|
+ // 認証コードを送信する
|
|
|
+ String sendId = "123";
|
|
|
+
|
|
|
+ return this.createTempToken(sendId, userDetails.getMailAddress());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -91,8 +94,9 @@ public class AuthService {
|
|
|
|
|
|
try
|
|
|
{
|
|
|
+ AuthTempTokenBo authTempTokenBo = getTempToken(authSecondBo.getTempToken());
|
|
|
// ユーザー
|
|
|
- userDetails = this.selectUserByMailAddress(authSecondBo.getMailAddress());
|
|
|
+ userDetails = this.selectUserByMailAddress(authTempTokenBo.getMailAddess());
|
|
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails.getFarmerId(), authSecondBo.getVerifyCode());
|
|
|
AuthenticationContextHolder.setContext(authenticationToken);
|
|
|
authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
@@ -101,8 +105,8 @@ public class AuthService {
|
|
|
{
|
|
|
if (e instanceof BadCredentialsException)
|
|
|
{
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(appConfig.getName(), userDetails.getFarmerId(), Constants.LOGIN_FAIL, MessageUtils.message("E0004")));
|
|
|
- throw new UserPasswordNotMatchException();
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(appConfig.getName(), userDetails.getFarmerId(), Constants.LOGIN_FAIL, MessageUtils.message("E0063")));
|
|
|
+ throw new AuthVerifyCodeNotMatchException();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -114,7 +118,8 @@ public class AuthService {
|
|
|
{
|
|
|
AuthenticationContextHolder.clearContext();
|
|
|
}
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(appConfig.getName(), userDetails.getFarmerId(), Constants.LOGIN_SUCCESS, MessageUtils.message("I0008")));
|
|
|
+ // I0011=二次認証に成功しました。
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(appConfig.getName(), userDetails.getFarmerId(), Constants.LOGIN_SUCCESS, MessageUtils.message("I0011")));
|
|
|
|
|
|
LoginUser<FarmerUserInfo> loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
|
|
|
@@ -127,13 +132,18 @@ public class AuthService {
|
|
|
/**
|
|
|
* 認証コードを再送信する
|
|
|
*/
|
|
|
- public void resendAuthCode(AuthReSendVerifyCodeBo authReSendVerifyCodeBo) throws Exception {
|
|
|
+ public String resendAuthCode(AuthReSendVerifyCodeBo authReSendVerifyCodeBo) throws Exception {
|
|
|
+
|
|
|
+ AuthTempTokenBo authTempTokenBo = getTempToken(authReSendVerifyCodeBo.getTempToken());
|
|
|
|
|
|
// ユーザー
|
|
|
- FarmerUserInfo userDetails = this.selectUserByMailAddress(authReSendVerifyCodeBo.getEmailAddress());
|
|
|
+ FarmerUserInfo userDetails = this.selectUserByMailAddress(authTempTokenBo.getMailAddess());
|
|
|
|
|
|
// TODO 認証コードを再送信する
|
|
|
// emailService.generateAndSendVerificationCode(user.getEmail());
|
|
|
+ String sendId = "5698";
|
|
|
+
|
|
|
+ return this.createTempToken(sendId, userDetails.getMailAddress());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -223,8 +233,60 @@ public class AuthService {
|
|
|
*
|
|
|
* @param user ユーザ
|
|
|
*/
|
|
|
- public void recordLoginInfo(FarmerUserInfo user)
|
|
|
+ private void recordLoginInfo(FarmerUserInfo user)
|
|
|
{
|
|
|
farmerMapper.updateLoginInfo(user);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 認証トークンの生成
|
|
|
+ *
|
|
|
+ * @param sendAuthVerifyCodeId セッション管理ID
|
|
|
+ * @param email メールアドレス
|
|
|
+ * @return 認証トークン
|
|
|
+ */
|
|
|
+ private String createTempToken(String sendAuthVerifyCodeId, String email) {
|
|
|
+ String resultToken = StringUtils.EMPTY;
|
|
|
+ try {
|
|
|
+ Map<String, Object> encryptMap = Map.of(
|
|
|
+ "sendId", sendAuthVerifyCodeId,
|
|
|
+ "email", email);
|
|
|
+ String tokenFormat = JsonUtil.toJsonStr(encryptMap);
|
|
|
+ resultToken = DecryptUtils.decryptAES(tokenFormat);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("【認証ユーザー異常】一時トークンの生成に失敗しました。", ex);
|
|
|
+ throw new ServiceException("一時トークン生成失敗しますので、管理者を連絡してください。");
|
|
|
+ }
|
|
|
+ return resultToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ private AuthTempTokenBo getTempToken(String tempToken){
|
|
|
+ AuthTempTokenBo result = new AuthTempTokenBo();
|
|
|
+ String decryptTokenStr;
|
|
|
+ try {
|
|
|
+ // 認証トークンの復号化
|
|
|
+ decryptTokenStr = DecryptUtils.decryptAES(tempToken);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("【認証ユーザー異常】認証トークンの復号化に失敗しました。 {}", e);
|
|
|
+ throw new ServiceException( "認証トークンが無効です。");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(decryptTokenStr)) {
|
|
|
+ log.error("【認証ユーザー異常】認証ユーザー情報を取得する。復号化情報は空白");
|
|
|
+ throw new ServiceException("認証トークンが無効です。");
|
|
|
+ } else if (!JsonUtil.isJsonStr(decryptTokenStr)) {
|
|
|
+ log.error("【認証ユーザー異常】認証ユーザー情報を取得する。復号化情報はJSON形式以外です。");
|
|
|
+ throw new ServiceException("認証トークンが無効です。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 復号化のトーケン情報
|
|
|
+ Map<String, Object> tokenMap = JsonUtil.parseObject(decryptTokenStr, Map.class);
|
|
|
+
|
|
|
+ if (null != tokenMap && tokenMap.containsKey("sendId") && StringUtils.isNotNull(tokenMap.get("sendId"))) {
|
|
|
+ result.setSendVerifyCodeId(String.valueOf(tokenMap.get("sendId")));
|
|
|
+ result.setMailAddess(String.valueOf(tokenMap.get("email")));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|