mirror of
https://gitee.com/dromara/MaxKey.git
synced 2026-06-10 03:07:33 +08:00
ip2region & GeoLite2 ip地址转换,支持国家、省、市、地域
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.dromara.maxkey.autoconfigure.IpLocationAutoConfiguration
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -59,8 +59,7 @@ import com.nimbusds.jose.JOSEException;
|
||||
|
||||
@AutoConfiguration
|
||||
public class ApplicationAutoConfiguration implements InitializingBean {
|
||||
private static final Logger _logger =
|
||||
LoggerFactory.getLogger(ApplicationAutoConfiguration.class);
|
||||
static final Logger _logger = LoggerFactory.getLogger(ApplicationAutoConfiguration.class);
|
||||
|
||||
@Bean
|
||||
public PasswordReciprocal passwordReciprocal() {
|
||||
|
||||
@@ -64,7 +64,7 @@ import jakarta.servlet.Filter;
|
||||
|
||||
@AutoConfiguration
|
||||
public class MvcAutoConfiguration implements InitializingBean , WebMvcConfigurer {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(MvcAutoConfiguration.class);
|
||||
static final Logger _logger = LoggerFactory.getLogger(MvcAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* 消息处理,可以直接使用properties的key值,返回的是对应的value值
|
||||
|
||||
@@ -28,7 +28,7 @@ import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
@AutoConfiguration
|
||||
public class RedisAutoConfiguration implements InitializingBean {
|
||||
private static final Logger _logger = LoggerFactory.getLogger(RedisAutoConfiguration.class);
|
||||
static final Logger _logger = LoggerFactory.getLogger(RedisAutoConfiguration.class);
|
||||
|
||||
/**
|
||||
* RedisConnectionFactory.
|
||||
|
||||
@@ -18,7 +18,7 @@ import io.swagger.v3.oas.models.info.License;
|
||||
|
||||
@AutoConfiguration
|
||||
public class SwaggerConfig {
|
||||
final static Logger _logger = LoggerFactory.getLogger(SwaggerConfig.class);
|
||||
static final Logger _logger = LoggerFactory.getLogger(SwaggerConfig.class);
|
||||
|
||||
@Value("${maxkey.swagger.title}")
|
||||
String title;
|
||||
|
||||
@@ -64,9 +64,13 @@ public class HistoryLogin extends JpaEntity implements Serializable{
|
||||
@Column
|
||||
String sourceIp;
|
||||
@Column
|
||||
String ipRegion;
|
||||
String country;
|
||||
@Column
|
||||
String ipLocation;
|
||||
String province;
|
||||
@Column
|
||||
String city;
|
||||
@Column
|
||||
String location;
|
||||
@Column
|
||||
String browser;
|
||||
@Column
|
||||
@@ -173,20 +177,36 @@ public class HistoryLogin extends JpaEntity implements Serializable{
|
||||
this.sourceIp = sourceIp;
|
||||
}
|
||||
|
||||
public String getIpRegion() {
|
||||
return ipRegion;
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setIpRegion(String ipRegion) {
|
||||
this.ipRegion = ipRegion;
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getIpLocation() {
|
||||
return ipLocation;
|
||||
public String getProvince() {
|
||||
return province;
|
||||
}
|
||||
|
||||
public void setIpLocation(String ipLocation) {
|
||||
this.ipLocation = ipLocation;
|
||||
public void setProvince(String province) {
|
||||
this.province = province;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public String getBrowser() {
|
||||
@@ -300,10 +320,14 @@ public class HistoryLogin extends JpaEntity implements Serializable{
|
||||
builder.append(provider);
|
||||
builder.append(", sourceIp=");
|
||||
builder.append(sourceIp);
|
||||
builder.append(", ipRegion=");
|
||||
builder.append(ipRegion);
|
||||
builder.append(", ipLocation=");
|
||||
builder.append(ipLocation);
|
||||
builder.append(", country=");
|
||||
builder.append(country);
|
||||
builder.append(", province=");
|
||||
builder.append(province);
|
||||
builder.append(", city=");
|
||||
builder.append(city);
|
||||
builder.append(", location=");
|
||||
builder.append(location);
|
||||
builder.append(", browser=");
|
||||
builder.append(browser);
|
||||
builder.append(", platform=");
|
||||
|
||||
@@ -28,7 +28,30 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
public class LoginHistoryRepository {
|
||||
private static Logger logger = LoggerFactory.getLogger(LoginHistoryRepository.class);
|
||||
|
||||
private static final String HISTORY_LOGIN_INSERT_STATEMENT = "insert into mxk_history_login (id , sessionid , userid , username , displayname , logintype , message , code , provider , sourceip , ipregion , iplocation, browser , platform , application , loginurl , sessionstatus ,instid)values( ? , ? , ? , ? , ? , ? , ? , ? , ?, ? , ? , ?, ? , ? , ?, ? , ? , ?)";
|
||||
private static final String HISTORY_LOGIN_INSERT_STATEMENT = """
|
||||
insert into mxk_history_login
|
||||
( id ,
|
||||
sessionid ,
|
||||
userid ,
|
||||
username ,
|
||||
displayname ,
|
||||
logintype ,
|
||||
message ,
|
||||
code ,
|
||||
provider ,
|
||||
sourceip ,
|
||||
country ,
|
||||
province ,
|
||||
city ,
|
||||
location ,
|
||||
browser ,
|
||||
platform ,
|
||||
application ,
|
||||
loginurl ,
|
||||
sessionstatus ,
|
||||
instid)
|
||||
values( ? , ? , ? , ? , ? , ? , ? , ? , ?, ? , ? , ? , ?, ?, ? , ? , ?, ? , ? , ?)
|
||||
""";
|
||||
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
|
||||
@@ -71,8 +94,10 @@ public class LoginHistoryRepository {
|
||||
historyLogin.getCode(),
|
||||
historyLogin.getProvider(),
|
||||
historyLogin.getSourceIp(),
|
||||
historyLogin.getIpRegion(),
|
||||
historyLogin.getIpLocation(),
|
||||
historyLogin.getCountry(),
|
||||
historyLogin.getProvince(),
|
||||
historyLogin.getCity(),
|
||||
historyLogin.getLocation(),
|
||||
historyLogin.getBrowser(),
|
||||
historyLogin.getPlatform(),
|
||||
"Browser",
|
||||
@@ -97,6 +122,8 @@ public class LoginHistoryRepository {
|
||||
Types.VARCHAR,
|
||||
Types.VARCHAR,
|
||||
Types.VARCHAR,
|
||||
Types.VARCHAR,
|
||||
Types.VARCHAR,
|
||||
Types.INTEGER,
|
||||
Types.VARCHAR
|
||||
});
|
||||
|
||||
@@ -9,11 +9,12 @@ dependencies {
|
||||
implementation project(":maxkey-persistence")
|
||||
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-core")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-social")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-captcha")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-ip2location")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-otp")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-provider")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-sms")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-social")
|
||||
|
||||
implementation project(":maxkey-protocols:maxkey-protocol-authorize")
|
||||
implementation project(":maxkey-protocols:maxkey-protocol-cas")
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.dromara.maxkey.authn.support.kerberos.KerberosProxy;
|
||||
import org.dromara.maxkey.authn.support.kerberos.RemoteKerberosService;
|
||||
import org.dromara.maxkey.configuration.EmailConfig;
|
||||
import org.dromara.maxkey.constants.ConstsPersistence;
|
||||
import org.dromara.maxkey.ip2location.IpLocationParser;
|
||||
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
|
||||
import org.dromara.maxkey.password.onetimepwd.MailOtpAuthnService;
|
||||
import org.dromara.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat;
|
||||
@@ -103,6 +104,7 @@ public class MaxKeyConfig implements InitializingBean {
|
||||
LoginRepository loginService,
|
||||
LoginHistoryRepository loginHistoryService,
|
||||
UserInfoService userInfoService,
|
||||
IpLocationParser ipLocationParser,
|
||||
JdbcTemplate jdbcTemplate,
|
||||
MailOtpAuthnService otpAuthnService,
|
||||
LdapContextService ldapContextService) {
|
||||
@@ -113,6 +115,7 @@ public class MaxKeyConfig implements InitializingBean {
|
||||
loginService,
|
||||
loginHistoryService,
|
||||
userInfoService,
|
||||
ipLocationParser,
|
||||
jdbcTemplate,
|
||||
ldapRealmService
|
||||
);
|
||||
|
||||
@@ -10,6 +10,7 @@ dependencies {
|
||||
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-core")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-captcha")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-ip2location")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-otp")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-provider")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-sms")
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package org.dromara.maxkey.autoconfigure;
|
||||
|
||||
import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
|
||||
import org.dromara.maxkey.ip2location.IpLocationParser;
|
||||
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
|
||||
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
|
||||
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
|
||||
@@ -44,6 +45,7 @@ public class MaxKeyMgtConfig implements InitializingBean {
|
||||
LoginRepository loginRepository,
|
||||
LoginHistoryRepository loginHistoryRepository,
|
||||
UserInfoService userInfoService,
|
||||
IpLocationParser ipLocationParser,
|
||||
JdbcTemplate jdbcTemplate) {
|
||||
|
||||
JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
|
||||
@@ -52,6 +54,7 @@ public class MaxKeyMgtConfig implements InitializingBean {
|
||||
loginRepository,
|
||||
loginHistoryRepository,
|
||||
userInfoService,
|
||||
ipLocationParser,
|
||||
jdbcTemplate);
|
||||
|
||||
logger.debug("JdbcAuthenticationRealm inited.");
|
||||
|
||||
@@ -10,6 +10,7 @@ dependencies {
|
||||
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-core")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-captcha")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-ip2location")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-otp")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-provider")
|
||||
implementation project(":maxkey-authentications:maxkey-authentication-sms")
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package org.dromara.maxkey.autoconfigure;
|
||||
|
||||
import org.dromara.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
|
||||
import org.dromara.maxkey.ip2location.IpLocationParser;
|
||||
import org.dromara.maxkey.password.onetimepwd.AbstractOtpAuthn;
|
||||
import org.dromara.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
|
||||
import org.dromara.maxkey.persistence.repository.LoginHistoryRepository;
|
||||
@@ -44,6 +45,7 @@ public class MaxKeyOpenApiConfig implements InitializingBean {
|
||||
LoginRepository loginRepository,
|
||||
LoginHistoryRepository loginHistoryRepository,
|
||||
UserInfoService userInfoService,
|
||||
IpLocationParser ipLocationParser,
|
||||
JdbcTemplate jdbcTemplate) {
|
||||
|
||||
JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
|
||||
@@ -52,6 +54,7 @@ public class MaxKeyOpenApiConfig implements InitializingBean {
|
||||
loginRepository,
|
||||
loginHistoryRepository,
|
||||
userInfoService,
|
||||
ipLocationParser,
|
||||
jdbcTemplate);
|
||||
|
||||
logger.debug("JdbcAuthenticationRealm inited.");
|
||||
|
||||
Reference in New Issue
Block a user