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

commit commit

上级 a256c10e
......@@ -24,6 +24,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
......@@ -117,10 +118,11 @@ public class AuthController extends Controller {
userInfo.setFcm(loginRequest.getFcm());
}
if( loginRequest.getVeryOldPassword() != null ){
if (loginRequest.getVeryOldPassword() != null) {
userInfo.setPassword(loginRequest.getVeryOldPassword());
}
userInfo.setLastLoginTime(new Date());
userInfo.setUserType(UserTypeEnum.EMAIL.getCode());
userRepository.save(userInfo);
......@@ -245,12 +247,10 @@ public class AuthController extends Controller {
if (!optional.isPresent() && user.getEmail() != null && !user.getEmail().isEmpty() && userRepository.existsByAccount(user.getEmail())) {
optional = userRepository.findFirstByAccount(user.getEmail());
gotFromOld = optional.isPresent();
}
if (!optional.isPresent() && user.getPhone() != null && !user.getPhone().isEmpty() && userRepository.existsByAccount(user.getPhone().replace("+", ""))) {
optional = userRepository.findFirstByAccount(user.getPhone().replace("+", ""));
gotFromOld = optional.isPresent();
}
if (!optional.isPresent()) {
......@@ -311,14 +311,12 @@ public class AuthController extends Controller {
}
String very = gotFromOld ? user.getPassword() : null;
// generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// user exist we have to generate token also
if (user.getPassword() == null || gotFromOld) {
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
}
// // generate token codes has been moved downwards from if condition of checking if user doesn't exist in database, because even if
// // user exist we have to generate token also
// if (user.getPassword() == null || gotFromOld) {
// 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);
......@@ -329,11 +327,21 @@ public class AuthController extends Controller {
userRepository.save(user);
//注册成功 创建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 {
......@@ -373,7 +381,7 @@ public class AuthController extends Controller {
private void fixCode(TbCfUserInfo user) {
if( user.getCode() == null ) {
if (user.getCode() == null) {
try {
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 {
if (nativeQuery.getSingleResult() != null) {
user.setCode(nativeQuery.getSingleResult().toString());
}
}catch (Exception e){
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
......
......@@ -7,6 +7,7 @@ public class LoginRequest {
private String password;
private String veryOldPassword;
private String fcm;
private boolean fromUser = false;
public LoginRequest(){
......@@ -18,7 +19,15 @@ public class LoginRequest {
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.password = password;
this.fcm = fcm;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论