Check Text for SQL Injection Attacks in Java
SQL (Structured Query Language) injection is a code injection technique that is used to target data-driven applications; the SQL statements are input into an entry field for execution and cause harm from there. These threats seek and attack existing security vulnerabilities within websites or other databases to acquire access to confidential information. The following API can provide protection against SQL injection attacks by automatically detecting them from a text input.
Let’s begin installing Maven by adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add a reference to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
Now we are ready to call the function, which we can do by inputting the user-facing text into the below example code:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.TextInputApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");TextInputApi apiInstance = new TextInputApi();
String value = "value_example"; // String | User-facing text input.
String detectionLevel = "detectionLevel_example"; // String | Set to Normal to target a high-security SQL Injection detection level with a very low false positive rate; select High to target a very-high security SQL Injection detection level with higher false positives. Default is Normal (recommended).
try {
SqlInjectionDetectionResult result = apiInstance.textInputCheckSqlInjection(value, detectionLevel);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling TextInputApi#textInputCheckSqlInjection");
e.printStackTrace();
}
Done! If a SQL injection attack is located within the text, it will be indicated in the result.