提交 5e66ece8 authored 作者: 吴德鹏's avatar 吴德鹏

识别注册用户的国家

上级 c53e0533
...@@ -15,10 +15,12 @@ import com.example.afrishop_v3.repository.UserRepository; ...@@ -15,10 +15,12 @@ import com.example.afrishop_v3.repository.UserRepository;
import com.example.afrishop_v3.security.jwt.JwtUtils; import com.example.afrishop_v3.security.jwt.JwtUtils;
import com.example.afrishop_v3.security.services.UserDetailsImpl; import com.example.afrishop_v3.security.services.UserDetailsImpl;
import com.example.afrishop_v3.util.*; import com.example.afrishop_v3.util.*;
import net.sf.json.JSONObject;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.hashids.Hashids; import org.hashids.Hashids;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
...@@ -26,9 +28,12 @@ import org.springframework.security.core.Authentication; ...@@ -26,9 +28,12 @@ import org.springframework.security.core.Authentication;
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.*;
import org.springframework.web.client.RestTemplate;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.Query; import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
...@@ -46,7 +51,7 @@ public class AuthController extends Controller { ...@@ -46,7 +51,7 @@ public class AuthController extends Controller {
private final TbCfCouponRepository couponRepository; private final TbCfCouponRepository couponRepository;
private final TbCfToicouponRepository toicouponRepository; private final TbCfToicouponRepository toicouponRepository;
private final NetworkRepository networkRepository; private final NetworkRepository networkRepository;
// @PersistenceContext(unitName = "afrishop_v3") // @PersistenceContext(unitName = "afrishop_v3")
private final EntityManager entityManager; private final EntityManager entityManager;
// private final TbCfOrderRepository cfOrderRepository; // private final TbCfOrderRepository cfOrderRepository;
// private final SecondOrderRepository secondOrderRepository; // private final SecondOrderRepository secondOrderRepository;
...@@ -60,7 +65,7 @@ public class AuthController extends Controller { ...@@ -60,7 +65,7 @@ public class AuthController extends Controller {
private final JwtUtils jwtUtils; private final JwtUtils jwtUtils;
private static Logger logger = LoggerFactory.getLogger(AuthController.class); private static Logger logger = LoggerFactory.getLogger(AuthController.class);
public AuthController(DomainProperties domainProperties,PasswordEncoder encoder,JwtUtils jwtUtils,AuthenticationManager authenticationManager, UserRepository userRepository, EmailHelper emailHelper, TbCfCouponRepository couponRepository, TbCfToicouponRepository toicouponRepository, NetworkRepository networkRepository, EntityManager entityManager) { public AuthController(DomainProperties domainProperties, PasswordEncoder encoder, JwtUtils jwtUtils, AuthenticationManager authenticationManager, UserRepository userRepository, EmailHelper emailHelper, TbCfCouponRepository couponRepository, TbCfToicouponRepository toicouponRepository, NetworkRepository networkRepository, EntityManager entityManager) {
this.authenticationManager = authenticationManager; this.authenticationManager = authenticationManager;
this.userRepository = userRepository; this.userRepository = userRepository;
this.emailHelper = emailHelper; this.emailHelper = emailHelper;
...@@ -68,13 +73,14 @@ public class AuthController extends Controller { ...@@ -68,13 +73,14 @@ public class AuthController extends Controller {
this.toicouponRepository = toicouponRepository; this.toicouponRepository = toicouponRepository;
this.networkRepository = networkRepository; this.networkRepository = networkRepository;
//this.secondAddressRepository = secondAddressRepository; //this.secondAddressRepository = secondAddressRepository;
this.entityManager = entityManager; this.entityManager = entityManager;
this.encoder = encoder; this.encoder = encoder;
this.domainProperties = domainProperties; this.domainProperties = domainProperties;
this.jwtUtils = jwtUtils; this.jwtUtils = jwtUtils;
// this.cfOrderRepository = cfOrderRepository; // this.cfOrderRepository = cfOrderRepository;
// this.secondOrderRepository = secondOrderRepository; // this.secondOrderRepository = secondOrderRepository;
// this.secondUserRepository = secondUserRepository; // this.secondUserRepository = secondUserRepository;
} }
// @GetMapping("count") // @GetMapping("count")
...@@ -143,6 +149,12 @@ public class AuthController extends Controller { ...@@ -143,6 +149,12 @@ public class AuthController extends Controller {
// return notFound; // return notFound;
// } // }
@GetMapping("/getRealIp")
public Result getRealIp(HttpServletRequest request) {
String ip = this.getRealIpAddress(request);
return new Result().setData(ip);
}
//Sign in using email/phone and password //Sign in using email/phone and password
@PostMapping("/signin") @PostMapping("/signin")
public Result authenticateUser(@RequestBody LoginRequest loginRequest) { public Result authenticateUser(@RequestBody LoginRequest loginRequest) {
...@@ -234,13 +246,12 @@ public class AuthController extends Controller { ...@@ -234,13 +246,12 @@ public class AuthController extends Controller {
//Register new user with email //Register new user with email
@PostMapping("/signup") @PostMapping("/signup")
public Result<?> registerUser(@RequestBody TbCfUserInfo signUpRequest) { public Result<?> registerUser(@RequestBody TbCfUserInfo signUpRequest, HttpServletRequest request) {
try { try {
// boolean byAccount = userRepository.existsByAccount(signUpRequest.getEmail()); // boolean byAccount = userRepository.existsByAccount(signUpRequest.getEmail());
// if ( byAccount ) { // if ( byAccount ) {
// return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Username is already taken!"); // return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Error: Username is already taken!");
// } // }
String email = signUpRequest.getEmail(); String email = signUpRequest.getEmail();
String password = signUpRequest.getPassword(); String password = signUpRequest.getPassword();
...@@ -276,7 +287,7 @@ public class AuthController extends Controller { ...@@ -276,7 +287,7 @@ public class AuthController extends Controller {
signUpRequest.setUserId(IdUtil.createIdbyUUID()); signUpRequest.setUserId(IdUtil.createIdbyUUID());
fillUserNecessayInfo(signUpRequest); fillUserNecessayInfo(signUpRequest, request);
signUpRequest.setPassword(encoder.encode(password)); signUpRequest.setPassword(encoder.encode(password));
...@@ -332,7 +343,7 @@ public class AuthController extends Controller { ...@@ -332,7 +343,7 @@ public class AuthController extends Controller {
} }
@PostMapping("/signup/phone") @PostMapping("/signup/phone")
public Result<?> registerUserPhone(@RequestBody TbCfUserInfo signUpRequest) { public Result<?> registerUserPhone(@RequestBody TbCfUserInfo signUpRequest, HttpServletRequest request) {
try { try {
// boolean byAccount = userRepository.existsByAccount(signUpRequest.getEmail()); // boolean byAccount = userRepository.existsByAccount(signUpRequest.getEmail());
// if ( byAccount ) { // if ( byAccount ) {
...@@ -374,7 +385,7 @@ public class AuthController extends Controller { ...@@ -374,7 +385,7 @@ public class AuthController extends Controller {
signUpRequest.setUserId(IdUtil.createIdbyUUID()); signUpRequest.setUserId(IdUtil.createIdbyUUID());
fillUserNecessayInfo(signUpRequest); fillUserNecessayInfo(signUpRequest, request);
signUpRequest.setPassword(encoder.encode(password)); signUpRequest.setPassword(encoder.encode(password));
...@@ -402,7 +413,7 @@ public class AuthController extends Controller { ...@@ -402,7 +413,7 @@ public class AuthController extends Controller {
@PostMapping(value = "/register/user") @PostMapping(value = "/register/user")
public Result checkFirebase(@RequestBody TbCfUserInfo user) { public Result checkFirebase(@RequestBody TbCfUserInfo user, HttpServletRequest request) throws IOException {
//Data to be userInfoVo //Data to be userInfoVo
// { // {
// "firebaseUid":"firebaseUid", // "firebaseUid":"firebaseUid",
...@@ -441,7 +452,7 @@ public class AuthController extends Controller { ...@@ -441,7 +452,7 @@ public class AuthController extends Controller {
// user.setPassword(encoder.encode(firebaseUid)); // user.setPassword(encoder.encode(firebaseUid));
user.setUserId(userid); user.setUserId(userid);
fillUserNecessayInfo(user); fillUserNecessayInfo(user, request);
user = userRepository.save(user); user = userRepository.save(user);
...@@ -614,8 +625,19 @@ public class AuthController extends Controller { ...@@ -614,8 +625,19 @@ public class AuthController extends Controller {
} }
//fill basic information for new user //fill basic information for new user
private void fillUserNecessayInfo(TbCfUserInfo tbCfUserInfoVo) { private void fillUserNecessayInfo(TbCfUserInfo tbCfUserInfoVo, HttpServletRequest request) throws IOException {
RestTemplate restTemplate = new RestTemplate();
if (tbCfUserInfoVo.getAvatar() == null) tbCfUserInfoVo.setAvatar(domainProperties.getProperty("user.avatar")); if (tbCfUserInfoVo.getAvatar() == null) tbCfUserInfoVo.setAvatar(domainProperties.getProperty("user.avatar"));
// ResponseEntity forEntity2 = restTemplate.getForEntity("http://admin.afrieshop.com/api/upload/getRealIp", String.class);
// System.err.println(forEntity2.getBody());
// String body = (String) forEntity2.getBody();
// String ip = JSONObject.fromObject(body).getString("ip");
// System.out.println(ip);
String ip = getIpAddr(request);
String country = HttpsUtil.useIpFindCountry(ip);
// String country = HttpsUtil.ipToAdress(ip);
tbCfUserInfoVo.setCountry(country);
tbCfUserInfoVo.setUserNo(IdUtil.createIdByDate()); tbCfUserInfoVo.setUserNo(IdUtil.createIdByDate());
tbCfUserInfoVo.setPhoneFlag(StateConstant.INVALID); tbCfUserInfoVo.setPhoneFlag(StateConstant.INVALID);
tbCfUserInfoVo.setLoginCount(0); tbCfUserInfoVo.setLoginCount(0);
......
...@@ -260,4 +260,31 @@ abstract class Controller { ...@@ -260,4 +260,31 @@ abstract class Controller {
} }
return ip; return ip;
} }
/**
* 获取IP地址
* @param request
* @return
*/
protected String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个IP值,第一个为真实IP。
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
}
else {
return ip;
}
}
else {
return request.getRemoteAddr();
}
}
} }
...@@ -81,7 +81,7 @@ public class FlutterWaveController extends Controller { ...@@ -81,7 +81,7 @@ public class FlutterWaveController extends Controller {
public ResponseEntity<String> payForOrderByCard(@RequestParam("orderId") String orderId, @RequestBody FlutterWaveCard flutterWaveCard) { public ResponseEntity<String> payForOrderByCard(@RequestParam("orderId") String orderId, @RequestBody FlutterWaveCard flutterWaveCard) {
RaveConstant.PUBLIC_KEY = PUBLIC_KEY; RaveConstant.PUBLIC_KEY = PUBLIC_KEY;
RaveConstant.SECRET_KEY = SECRET_KEY; RaveConstant.SECRET_KEY = SECRET_KEY;
RaveConstant.ENVIRONMENT = Environment.LIVE; //or live RaveConstant.ENVIRONMENT = Environment.STAGING; //or live
// Result result = new Result(); // Result result = new Result();
Optional<TbCfOrder> byId = repository.findById(orderId); Optional<TbCfOrder> byId = repository.findById(orderId);
......
...@@ -187,6 +187,16 @@ public class TbCfUserInfo { ...@@ -187,6 +187,16 @@ public class TbCfUserInfo {
this.source = source; this.source = source;
} }
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void setCodeSentTime(Date codeSentTime) { public void setCodeSentTime(Date codeSentTime) {
this.codeSentTime = codeSentTime; this.codeSentTime = codeSentTime;
} }
......
server.servlet.context-path=/zion server.servlet.context-path=/zion
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
server.port=8083 server.port=8083
spring.profiles.active=prod spring.profiles.active=test
#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/afrishop_test?useUnicode=true&connectionCollation=utf8mb4_general_ci&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC #spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/afrishop_test?useUnicode=true&connectionCollation=utf8mb4_general_ci&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#spring.datasource.username=root #spring.datasource.username=root
#spring.datasource.password=Diaoyunnuli.8 #spring.datasource.password=Diaoyunnuli.8
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论