ip2region & GeoLite2 ip地址转换,支持国家、省、市、地域

This commit is contained in:
MaxKey
2023-10-05 10:09:14 +08:00
parent 465ea18cc5
commit baa46a8013
17 changed files with 108 additions and 28 deletions
@@ -116,16 +116,16 @@ public class IpLocationAutoConfiguration implements InitializingBean {
/**
* IP转换区域地址解析
* @param isIplocation 是否转换
* @param onlineProvider 在线转换实现提供商none/Ip138/Ipchaxun
* @param onlineProvider 在线转换实现提供商none/Ip138
* @param offlineProvider 离线转换实现提供商none/Ip2Region/GeoIp2
* @return IpLocationParser
* @throws Exception
*/
@Bean
public IpLocationParser ipLocationParser(
@Value("${maxkey.login.iplocation:false}") boolean isIplocation,
@Value("${maxkey.login.iplocation:true}") boolean isIplocation,
@Value("${maxkey.login.iplocation.online.provider:none}") String onlineProvider,
@Value("${maxkey.login.iplocation.offline.provider:none}") String offlineProvider) throws Exception {
@Value("${maxkey.login.iplocation.offline.provider:Ip2Region}") String offlineProvider) throws Exception {
return new IpLocationParser(
isIplocation,
builderOnlineProvider(onlineProvider),
@@ -10,6 +10,7 @@ dependencies {
implementation project(":maxkey-core")
implementation project(":maxkey-persistence")
implementation project(":maxkey-authentications:maxkey-authentication-core")
implementation project(":maxkey-authentications:maxkey-authentication-ip2location")
implementation project(":maxkey-authentications:maxkey-authentication-otp")
implementation project(":maxkey-authentications:maxkey-authentication-sms")
@@ -26,6 +26,8 @@ import org.dromara.maxkey.authn.realm.ldap.LdapAuthenticationRealmService;
import org.dromara.maxkey.entity.HistoryLogin;
import org.dromara.maxkey.entity.Roles;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.ip2location.Region;
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
@@ -45,7 +47,7 @@ import org.springframework.security.core.GrantedAuthority;
*
*/
public abstract class AbstractAuthenticationRealm {
private static Logger _logger = LoggerFactory.getLogger(AbstractAuthenticationRealm.class);
private static final Logger _logger = LoggerFactory.getLogger(AbstractAuthenticationRealm.class);
protected JdbcTemplate jdbcTemplate;
@@ -58,6 +60,8 @@ public abstract class AbstractAuthenticationRealm {
protected UserInfoService userInfoService;
protected LdapAuthenticationRealmService ldapAuthenticationRealmService;
protected IpLocationParser ipLocationParser;
/**
@@ -147,6 +151,13 @@ public abstract class AbstractAuthenticationRealm {
historyLogin.setDisplayName(userInfo.getDisplayName());
historyLogin.setInstId(userInfo.getInstId());
Region ipRegion =ipLocationParser.region(userInfo.getLastLoginIp());
if(ipRegion != null) {
historyLogin.setCountry(ipRegion.getCountry());
historyLogin.setProvince(ipRegion.getProvince());
historyLogin.setCity(ipRegion.getCity());
historyLogin.setLocation(ipRegion.getAddr());
}
loginHistoryRepository.login(historyLogin);
loginRepository.updateLastLogin(userInfo);
@@ -25,6 +25,7 @@ import org.dromara.maxkey.constants.ConstsStatus;
import org.dromara.maxkey.entity.ChangePassword;
import org.dromara.maxkey.entity.PasswordPolicy;
import org.dromara.maxkey.entity.UserInfo;
import org.dromara.maxkey.ip2location.IpLocationParser;
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
import org.dromara.maxkey.persistence.repository.LoginRepository;
import org.dromara.maxkey.persistence.repository.PasswordPolicyValidator;
@@ -43,7 +44,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
*
*/
public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
private static Logger _logger = LoggerFactory.getLogger(JdbcAuthenticationRealm.class);
private static final Logger _logger = LoggerFactory.getLogger(JdbcAuthenticationRealm.class);
protected PasswordEncoder passwordEncoder;
@@ -61,6 +62,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
LoginRepository loginRepository,
LoginHistoryRepository loginHistoryRepository,
UserInfoService userInfoService,
IpLocationParser ipLocationParser,
JdbcTemplate jdbcTemplate) {
this.passwordEncoder =passwordEncoder;
@@ -68,6 +70,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
this.loginRepository = loginRepository;
this.loginHistoryRepository = loginHistoryRepository;
this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser;
this.jdbcTemplate = jdbcTemplate;
}
@@ -77,6 +80,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
LoginRepository loginRepository,
LoginHistoryRepository loginHistoryRepository,
UserInfoService userInfoService,
IpLocationParser ipLocationParser,
JdbcTemplate jdbcTemplate,
LdapAuthenticationRealmService ldapAuthenticationRealmService) {
this.passwordEncoder = passwordEncoder;
@@ -84,6 +88,7 @@ public class JdbcAuthenticationRealm extends AbstractAuthenticationRealm {
this.loginRepository = loginRepository;
this.loginHistoryRepository = loginHistoryRepository;
this.userInfoService = userInfoService;
this.ipLocationParser = ipLocationParser;
this.jdbcTemplate = jdbcTemplate;
this.ldapAuthenticationRealmService = ldapAuthenticationRealmService;
}