Class SecurityService

java.lang.Object
de.bytestore.hostinger.security.service.SecurityService

@Service @Component public class SecurityService extends Object
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 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 query
      com.maxmind.geoip2.exception.GeoIp2Exception - if an error occurs while retrieving the GeoIP information
    • addLog

      public SecurityLog addLog(String keyIO, com.google.gson.JsonObject valueIO)
      Adds a security log entry with the specified key and value.
      Parameters:
      keyIO - the key of the log entry
      valueIO - the value of the log entry
      Returns:
      the created SecurityLog entry
    • addLog

      public SecurityLog addLog(User userIO, String keyIO, com.google.gson.JsonObject valueIO)
      Adds a security log entry with the specified user, key, and value.
      Parameters:
      userIO - the user associated with the log entry
      keyIO - the key of the log entry
      valueIO - the value of the log entry
      Returns:
      the created SecurityLog entry
    • getIPAddress

      public String 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:

      1. Loads the GeoIP database by calling the loadGeoIPDatabase() method.
      2. 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();
       
    • purgeOldLogs

      public void purgeOldLogs(Integer maxAge)
      Purges old logs from the security log database.
      Parameters:
      maxAge - the maximum age in days of the logs to be purged
    • getSecurityGrade

      public SecurityGrade getSecurityGrade(String keyIO)
      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

      public String getGradeColorClass(SecurityGrade gradeIO)
      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

      public com.vaadin.flow.component.Component getGradeBadge(String key)
      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 email
      keyIO - the key of the log entry
      templateIO - the email template to use for sending the email
      dataIO - the data to include in both the log entry and the email
    • loginFailed

      public void loginFailed(User user, String typeIO)
      Handles the event when a login attempt fails.
      Parameters:
      user - the user who attempted the login
      typeIO - the type of I/O that failed during login
    • addLog

      public void addLog(Class userDetailViewClass, SecurityLevel securityLevel, User user, String s, String username, String usernameIO)
    • addLog

      public void addLog(Class<UserDetailView> userDetailViewClass, SecurityLevel securityLevel, User user, String s, String username)