Class SecurityService
java.lang.Object
de.bytestore.hostinger.security.service.SecurityService
SecurityService provides core security-related services, such as GeoIP handling, logging security activities, and
retrieving client IP addresses. It integrates with various components and services of the application to ensure
secure operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a security log entry with the specified user, key, and value.voidaddLog(Class<UserDetailView> userDetailViewClass, SecurityLevel securityLevel, User user, String s, String username) voidaddLog(Class userDetailViewClass, SecurityLevel securityLevel, User user, String s, String username, String usernameIO) Adds a security log entry with the specified key and value.voidAdds a security log entry with the specified user, key, and data, and sends an email with the specified template and data.com.vaadin.flow.component.ComponentgetGradeBadge(String key) Retrieves a grade badge component associated with the given key.getGradeColorClass(SecurityGrade gradeIO) Returns the CSS class corresponding to the given security grade.Retrieves the IP address of the client making the HTTP request.getSecurityGrade(String keyIO) Retrieves the security grade for a given key.voidloginFailed(User user, String typeIO) Handles the event when a login attempt fails.voidonApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event) voidpurgeOldLogs(Integer maxAge) Purges old logs from the security log database.com.maxmind.geoip2.model.CityResponsequeryGeoIP(InetAddress addressIO) Queries the GeoIP database to retrieve information about the given IP address.voidUpdates the GeoIP settings.
-
Constructor Details
-
SecurityService
public SecurityService()
-
-
Method Details
-
onApplicationStarted
@EventListener public void onApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event) throws Throwable - Throws:
Throwable
-
queryGeoIP
public com.maxmind.geoip2.model.CityResponse queryGeoIP(InetAddress addressIO) throws IOException, com.maxmind.geoip2.exception.GeoIp2Exception Queries the GeoIP database to retrieve information about the given IP address.- Parameters:
addressIO- the IP address to query- Returns:
- the CityResponse containing geographical information for the IP address
- Throws:
IOException- if an I/O error occurs during the querycom.maxmind.geoip2.exception.GeoIp2Exception- if an error occurs while retrieving the GeoIP information
-
addLog
Adds a security log entry with the specified key and value.- Parameters:
keyIO- the key of the log entryvalueIO- the value of the log entry- Returns:
- the created SecurityLog entry
-
addLog
Adds a security log entry with the specified user, key, and value.- Parameters:
userIO- the user associated with the log entrykeyIO- the key of the log entryvalueIO- the value of the log entry- Returns:
- the created SecurityLog entry
-
getIPAddress
Retrieves the IP address of the client making the HTTP request.- Returns:
- the IP address of the client
-
updateGeoIPSettings
public void updateGeoIPSettings()Updates the GeoIP settings.This method is called to update the GeoIP settings. It performs the following steps:
- Loads the GeoIP database by calling the
loadGeoIPDatabase()method. - Loads the GeoIP account by calling the
loadGeoIPAccount()method.
This method does not return any value.
Example usage:
SecurityService securityService = new SecurityService(appSettings); securityService.updateGeoIPSettings(); - Loads the GeoIP database by calling the
-
purgeOldLogs
Purges old logs from the security log database.- Parameters:
maxAge- the maximum age in days of the logs to be purged
-
getSecurityGrade
Retrieves the security grade for a given key.- Parameters:
keyIO- the key to retrieve the security grade for- Returns:
- the security grade associated with the key, or UNKNOWN if the key is not found
-
getGradeColorClass
Returns the CSS class corresponding to the given security grade.- Parameters:
gradeIO- the security grade to get the color class for- Returns:
- the CSS class corresponding to the security grade: - "danger" for HIGH grade - "warning" for MEDIUM grade - "success" for LOW grade - "unknown" for any other grade
-
getGradeBadge
Retrieves a grade badge component associated with the given key.- Parameters:
key- the key used to retrieve the security grade- Returns:
- a grade badge component representing the security grade associated with the key
-
addLogWithMail
public void addLogWithMail(User userIO, String keyIO, String templateIO, HashMap<String, Object> dataIO) Adds a security log entry with the specified user, key, and data, and sends an email with the specified template and data.- Parameters:
userIO- the user associated with the log entry and emailkeyIO- the key of the log entrytemplateIO- the email template to use for sending the emaildataIO- the data to include in both the log entry and the email
-
loginFailed
Handles the event when a login attempt fails.- Parameters:
user- the user who attempted the logintypeIO- the type of I/O that failed during login
-
addLog
-
addLog
public void addLog(Class<UserDetailView> userDetailViewClass, SecurityLevel securityLevel, User user, String s, String username)
-