提交 93e97256 authored 作者: Whispa's avatar Whispa

commit commit

上级 a256c10e
...@@ -24,6 +24,7 @@ import org.springframework.security.authentication.BadCredentialsException; ...@@ -24,6 +24,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -117,10 +118,11 @@ public class AuthController extends Controller { ...@@ -117,10 +118,11 @@ public class AuthController extends Controller {
userInfo.setFcm(loginRequest.getFcm()); userInfo.setFcm(loginRequest.getFcm());
} }
if( loginRequest.getVeryOldPassword() != null ){ if (loginRequest.getVeryOldPassword() != null) {
userInfo.setPassword(loginRequest.getVeryOldPassword()); userInfo.setPassword(loginRequest.getVeryOldPassword());
} }
userInfo.setLastLoginTime(new Date()); userInfo.setLastLoginTime(new Date());
userInfo.setUserType(UserTypeEnum.EMAIL.getCode()); userInfo.setUserType(UserTypeEnum.EMAIL.getCode());
userRepository.save(userInfo); userRepository.save(userInfo);
...@@ -245,12 +247,10 @@ public class AuthController extends Controller { ...@@ -245,12 +247,10 @@ public class AuthController extends Controller {
if (!optional.isPresent() && user.getEmail() != null && !user.getEmail().isEmpty() && userRepository.existsByAccount(user.getEmail())) { if (!optional.isPresent() && user.getEmail() != null && !user.getEmail().isEmpty() && userRepository.existsByAccount(user.getEmail())) {
optional = userRepository.findFirstByAccount(user.getEmail()); optional = userRepository.findFirstByAccount(user.getEmail());
gotFromOld = optional.isPresent();
} }
if (!optional.isPresent() && user.getPhone() != null && !user.getPhone().isEmpty() && userRepository.existsByAccount(user.getPhone().replace("+", ""))) { if (!optional.isPresent() && user.getPhone() != null && !user.getPhone().isEmpty() && userRepository.existsByAccount(user.getPhone().replace("+", ""))) {
optional = userRepository.findFirstByAccount(user.getPhone().replace("+", "")); optional = userRepository.findFirstByAccount(user.getPhone().replace("+", ""));
gotFromOld = optional.isPresent();
} }
if (!optional.isPresent()) { if (!optional.isPresent()) {
...@@ -311,14 +311,12 @@ public class AuthController extends Controller { ...@@ -311,14 +311,12 @@ public class AuthController extends Controller {
} }
// // generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
String very = gotFromOld ? user.getPassword() : null; // // user exist we have to generate token also
// generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if // if (user.getPassword() == null || gotFromOld) {
// user exist we have to generate token also // user.setFirebaseUid(firebaseUid);// Assign user from database to the user we have to return back to request
if (user.getPassword() == null || gotFromOld) { // // user.setPassword(encoder.encode(firebaseUid));// Assign user from database to the user we have to return back to request
user.setFirebaseUid(firebaseUid);// Assign user from database to the user we have to return back to request // }
user.setPassword(encoder.encode(firebaseUid));// Assign user from database to the user we have to return back to request
}
fixCode(user); fixCode(user);
...@@ -329,11 +327,21 @@ public class AuthController extends Controller { ...@@ -329,11 +327,21 @@ public class AuthController extends Controller {
userRepository.save(user); userRepository.save(user);
//注册成功 创建token //注册成功 创建token
LoginRequest loginRequest = new LoginRequest(firebaseUid, firebaseUid);
loginRequest.setVeryOldPassword(very); Authentication authentication;
try {
authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(firebaseUid, null, AuthorityUtils.createAuthorityList("ROLE_USER")));
} catch (BadCredentialsException e) {
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Invalid username or password");
}
SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtUtils.generateJwtToken(authentication);
user.setToken(jwt);
return authenticateUser(loginRequest); return new Result<>(user);
} else { } else {
...@@ -373,7 +381,7 @@ public class AuthController extends Controller { ...@@ -373,7 +381,7 @@ public class AuthController extends Controller {
private void fixCode(TbCfUserInfo user) { private void fixCode(TbCfUserInfo user) {
if( user.getCode() == null ) { if (user.getCode() == null) {
try { try {
Query nativeQuery = entityManager.createNativeQuery("select b.code as data FROM user_info b where b.external_id=:user limit 1"); Query nativeQuery = entityManager.createNativeQuery("select b.code as data FROM user_info b where b.external_id=:user limit 1");
...@@ -382,7 +390,7 @@ public class AuthController extends Controller { ...@@ -382,7 +390,7 @@ public class AuthController extends Controller {
if (nativeQuery.getSingleResult() != null) { if (nativeQuery.getSingleResult() != null) {
user.setCode(nativeQuery.getSingleResult().toString()); user.setCode(nativeQuery.getSingleResult().toString());
} }
}catch (Exception e){ } catch (Exception e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
} }
......
...@@ -7,6 +7,7 @@ public class LoginRequest { ...@@ -7,6 +7,7 @@ public class LoginRequest {
private String password; private String password;
private String veryOldPassword; private String veryOldPassword;
private String fcm; private String fcm;
private boolean fromUser = false;
public LoginRequest(){ public LoginRequest(){
...@@ -18,7 +19,15 @@ public class LoginRequest { ...@@ -18,7 +19,15 @@ public class LoginRequest {
this.password = password; this.password = password;
} }
public LoginRequest(String account,String password,String fcm){ public void setFromUser(boolean fromUser) {
this.fromUser = fromUser;
}
public boolean isFromUser() {
return fromUser;
}
public LoginRequest(String account, String password, String fcm){
this.account = account; this.account = account;
this.password = password; this.password = password;
this.fcm = fcm; this.fcm = fcm;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论