Saturday, April 22, 2023

Generating Access key ID and Secret Access Key in AWS

Access key ID and Secret Access Key are used to access AWS environments programmatically or via CLI

In order to generate them one needs to follow the below steps

  • Go to the AWS console and search for service IAM
  • Search and select the User for which key and secret needs to be created
  • Go to the tab security credentials and then choose Create access key.
  • Access key ID and Secret Access Key will get created for the above user

Thursday, April 20, 2023

How to prevent Distributed Denial of Services (DDOS) attack in your application

DDoS stands for Distributed Denial of Service and it's a situation where cyber criminals flood a network with so much malicious traffic that the impacted system cannot operate or communicate as it normally would.

Prevention of DDOS Attacks

  • Implement a lockout i.e. prevent an IP from making a login request for X minutes if they fail to log in N times. Tomcat has LockOutRealm configuration for the same

  • Implement progressive delay by adding a longer and longer delay to processing each bad login request.

  • Ensure that a user has a limit to the number of concurrent sessions (to prevent a hacked account logging on a million times)

  • Apply rate limits or use throttling mechanisms to prevent large numbers of requests

  • Have different database application users for different services (e.g. transactional use vs. reporting use) and use database resource management to prevent one type of web request from overwhelming all others.

  • Have a log format from which you can easily identify 

    • The IP of the requesting server

    • The URI of the request

    • The URI failing the most

    • User using the service

    • IPs of the users

    • URIs called by anonymous users

    • Arguments passed to a service

    • Audit a specific user actions

  • Use CDNs to distribute static resources to different locations and IP addresses. 

  • Install a firewall to reject incoming connections that violate rules that you define.

  • Update and patch all the resources at regular intervals

  • Run vulnerability scans quite oftenly

  • Harden applications e.g. adding captcha during login

  • Block unused ports on servers and firewalls

    • DNS port 53 should be blocked if organization is not using DNS server

    • P2P port 4662 and 4672 should be blocked

    • ICMP or ping should be blocked

  • Overprovision infrastructure by 

    • Moving to some cloud based scalable solution.

    • Designing it to 200-500% of the baseline needs.

    • Applying load balancing to route the traffic.

  • Place resources behind the firewall

  • Use container level configurations to reject requests

    • Tomcat Valve to reject incoming requests by their User-Agents (or any other criterion) as a last line of defense.


In AWS Cloud, AWS Shield can help to prevent DDOS Attacks. This service is provided automatically to all AWS customers at no additional charge.


Monday, February 6, 2023

How to view certificates present inside a keystore

 How to view certificates present inside a keystore?

The certificates inside a keystore or a trustore file can be viewed using keytool command present inside JDK. 

Prerequisites: 

  • JDK should already be installed and present in the path environment variable so that it can be accessed from anywhere using terminal on linux or command prompt on windows machine.
  • The paasword for keystore should be known

Command:

The command to get the list of certificates will be

keytool -list -v -keystore keystore.jks 

Sunday, February 5, 2023

How to add favicon to your html page

How to add favicon to your html page

How to add favicon to your html page

  • Create a favicon file say myfav.ico and add it to some location in project say assets/myfav.ico
  • Import the above in the head section of your html page
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="icon" type="image/x-icon" href="assets/myfav.ico">
</head>
<body>
<div>Demo'ing the Favicon!</div>
</body
  • The favicon is now ready to be displayed on the browser.




  • Please do like and follow our facebook page here
  • For more queries, feel free to e-mail us here
  • Just to let you know, we are an Amazon Associate. To help and support us, you can shop for different products available on Amazon from here

Tuesday, November 24, 2020

How to create and consume a SOAP based web service in JAVA

  • To create a SOAP webservice, you need to add the JAX-WS dependency to your project
                <dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxws-rt.version}</version>
</dependency>
  • Create an interface to expose the web methods e.g.

package com.arsoft.projects.artutorial.learning.soap.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorldService {
@WebMethod
public String sayHello(String name);
}

  • Annotate the interface with @WebService and @SOAPBinding and the exposed method with @WebMethod
  • Create the implementation class e.g.

package com.arsoft.projects.artutorial.learning.soap.server;
import javax.jws.WebService;
@WebService(endpointInterface = "com.arsoft.projects.artutorial.learning.soap.server.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
public String sayHello(String name) {
return "Hello " + name + "!!";
}

  • Annotate the class with @WebService providing the fully qualified name of the interface as endpointInterface 
  • Publish the webservice as below

package com.arsoft.projects.artutorial.learning.soap.server;
import javax.xml.ws.Endpoint;
import com.arsoft.projects.artutorial.learning.soap.Constant;
public class SoapServer {
public static void main(String[] args) {
Endpoint.publish(Constant.helloWorldSoapServiceUrl, new HelloWorldServiceImpl());
}
}
                  •  To consume the above webservice, create the stubs from the wsdl by adding below plugin to the pom.xml file of the client application
                                       <plugin>
                  <groupId>com.sun.xml.ws</groupId>
                  <artifactId>jaxws-maven-plugin</artifactId>
                  <version>2.3.2</version>
                  <executions>
                  <execution>
                  <id>generate-java-sources</id>
                  <phase>process-sources</phase>
                  <goals>
                  <goal>wsimport</goal>
                  </goals>
                  <configuration>
                  <extension>true</extension>
                  <wsdlUrls>
                  <wsdlUrl>http://localhost:8888/ws/hello?wsdl</wsdlUrl>
                  </wsdlUrls>
                  </configuration>
                  </execution>
                  </executions>
                  </plugin>
                  • Add the JAX-WS dependency to the client project
                  •                 <dependency>
                    <groupId>com.sun.xml.ws</groupId>
                    <artifactId>jaxws-rt</artifactId>
                    <version>${jaxws-rt.version}</version>
                    </dependency>

                  • Generate the stub classes by running maven goal 'mvn process-sources. The stub classes will be generated inside folder target/generated-sources. Copy the file to src directory
                  • Create the consumer class as below
                  package com.odigo.bre.reporting;
                  import java.net.MalformedURLException;
                  import java.net.URL;
                  import javax.xml.namespace.QName;
                  import javax.xml.ws.Service;
                  import com.arsoft.projects.artutorial.learning.soap.server.HelloWorldService;
                  public class SoapClient {
                      public static void main(String[] args) throws MalformedURLException {
                          URL webserviceURL = new URL("http://localhost:8888/ws/hello?wsdl");
                          QName qname = new QName("http://server.soap.learning.artutorial.projects.arsoft.com/",
                                  "HelloWorldServiceImplService");
                          Service service = Service.create(webserviceURL, qname);
                          HelloWorldService helloWorldService = service.getPort(HelloWorldService.class);
                          String message = helloWorldService.sayHello("Anshul Sood");
                          System.out.println(message);
                      }
                  }

                   where QName takes targetNamespace and name from wsdl as its arguments. 

                                      Thursday, May 4, 2017

                                      Git

                                      Set up the initial global configuration
                                      git config --global user.name "Anshul Sood"
                                      git config --global user.email "anshulsood2006@gmail.com

                                      Get all the configurations
                                      git config --list

                                      Get all the global configurations
                                      git config --global --list

                                      To get the help related to any/all commands
                                      git help config
                                      git help --all

                                      To ignore a file in the git operations add them in .gitignore file e.g.
                                      # ignore all bin directories matches "bin" in any subfolder
                                      bin/
                                      # ignore all target directories
                                      target/
                                      # ignore all files ending with ~
                                      *~

                                      To create a global gitignore to exclude bin folder
                                      cd ~/
                                      touch .gitignore
                                      echo "bin" >> .gitignore
                                      git config --global core.excludesfile ~/.gitignore

                                      Create a git repository
                                      Go to the folder
                                      git init


                                      Get the current status of git repository
                                      git status

                                      Add the file to staging area
                                      git add file1 file2 file3
                                      git all .

                                      Commit files to local repository
                                      git commit -m "Commit Message"

                                      To get history
                                      git log

                                      To show the changes
                                      git show

                                      To revert the changes to a file
                                      git checkout file1

                                      To add to the remote repository
                                      Create a repository on github and copy the URL
                                      git remote add origin "https://github.com/anshulsood2006/Documentation.git"
                                      git push origin {branchName}

                                      If getting error
                                      remote origin already exists
                                      git remote rm origin
                                      Updates were rejected because the tip of your current branch is behind
                                         git pull https://github.com/anshulsood2006/Documentation.git {branchName}
                                      refusing to merge unrelated histories
                                         git merge origin/{branchName} --allow-unrelated-histories

                                      To create a branch
                                      git branch {branchName}

                                      To go to the branch
                                      git checkout {branchName}

                                      To add brancb to remote
                                      git push origin testing

                                      To delete a branch
                                      git branch -D {branchName}

                                      To get the difference from the commit
                                      git diff {fileName}

                                      Sunday, November 13, 2016

                                      ClassLoader

                                      ClassLoader:
                                      Java class loader is a class which used to load classes at runtime. ClassLoader in Java works on three principle:
                                      • 1.       Delegation principle: Responsibility of loading class is of parent classloader. A classloader will load class only if parent is not able to find or load class.
                                      • 2.       Visibility principle: Child class loader can see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child.
                                      • 3.       Uniqueness principle: A class should be loaded exactly once. This is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.


                                      Types of Classloader:
                                      There are three default class loader used in Java:
                                      • 1.       Bootstrap ClassLoader/Primordial ClassLoader is responsible for loading standard JDK class files from rt.jar and it is parent of all class loaders in Java. Bootstrap class loader don't have any parents, if you call String.class.getClassLoader() it will return null and any code based on that may throw NullPointerException in Java.
                                      • 2.       Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form jre/lib/ext directory or any other directory pointed by java.ext.dirs system property. Extension ClassLoader in JVM is implemented by sun.misc.Launcher$ExtClassLoader.
                                      • 3.       Application class loader is responsible for loading application specific classes from CLASSPATH environment variable, -classpath or -cp command line option, Class-Path attribute of Manifest file inside JAR. Application class loader is a child of Extension ClassLoader and its implemented by sun.misc.Launcher$AppClassLoader class.


                                      Except Bootstrap class loader, which is implemented in native language mostly in C, all Java class loaders are implemented using java.lang.ClassLoader.

                                      Explicitly load a class:
                                      Java provides API to explicitly load a class by
                                      • 1.       Class.forName(classname)
                                      • 2.       Class.forName(classname, initialized, classloader)


                                      Uses:
                                      • 1.       J2EE uses multiple class loaders to load class from different location e.g. classes from WAR file will be loaded by Web-app ClassLoader while classes bundled in EJB-JAR is loaded by another class loader.
                                      • 2.       Some web server also supports hot deploy functionality which is implemented using ClassLoader.
                                      • 3.       You can also use ClassLoader to load classes from database or any other persistent store.


                                      String.class.getClassLoader() returns null because this class is loaded by bootstrap classLoader which is not implemented in java , it's either implemented in c or c++ so there is no reference for it that's why it returns null.
                                      Why write a Custom ClassLoader in Java
                                      If you are expecting a class at the runtime or from FTP server or via third party web service at the time of loading the class then you have to extend the existing class loader.

                                      How does Java ClassLoader Work
                                      • 1.       When JVM requests for a class, it invokes loadClass function of the ClassLoader by passing the fully classified name of the Class.
                                      • 2.       loadClass function calls for findLoadedClass() method to check that the class has been already loaded or not. It’s required to avoid loading the class multiple times.
                                      • 3.       If the Class is not already loaded then it will delegate the request to parent ClassLoader to load the class.
                                      • 4.       If the parent ClassLoader is not finding the Class then it will invoke findClass() method to look for the classes in the file system.


                                      Custom ClassLoader in Java

                                      By extending ClassLoader class and overriding loadClass(String name) 

                                      Saturday, August 27, 2016

                                      Hibernate

                                      Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities.

                                      It involves two files :-
                                      1. Hibernate configuration file (.cfg.xml)
                                      2. Hibernate mapping file (.hbm.xml) 
                                      Both these files are places in src folder. The configuration file is the main and the mapping file is declared inside it inside <mapping> tag with attribute resource.


                                      Hibernate configuration file :-




                                      <hibernate-configuration>  
                                      <session-factory>  
                                      <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>  
                                      <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> 
                                       <!-- Assume test is the database name -->  
                                      <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property>  
                                      <property name="hibernate.connection.username"> root </property>  
                                      <property name="hibernate.connection.password"> root123  </property>  
                                      <!-- List of XML mapping files -->  
                                      <mapping resource="Employee.hbm.xml"/> 
                                       </session-factory>  
                                      </hibernate-configuration>

                                      Properties :- 
                                      hibernate.dialect  :-

                                      Hibernate Mapping File :-

                                      Monday, June 6, 2016

                                      Eclipse Issues

                                      Issue: 
                                      "Java compiler level does not match the version of the installed Java project facet."

                                      Resolution:
                                      Go to project >> Properties >> Project Facets and change the facets to required java version

                                      Issue:
                                      Unbound classpath container: 'JRE System Library' in project 'XYZ'

                                      Resolution:
                                      Go to Project >> build path >> Configure Buildpath >> Libraries >> Add Library >> JRE System Library >> Select an existing library

                                      Issue:
                                      "Cannot change version of project facet Dynamic Web Module to 3.0"

                                      Resolution:
                                      Change the attributes of web-app tag in web.xml file of the project to point to module 3.0


                                      Issue:
                                      The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

                                      Resolution:
                                      Add the dependency for java servlet api to the classpath

                                      Issue:
                                      How to enable to write mult-line string in eclipse

                                      Resolution:
                                      Go to Windows >> Preferences >> Java >> Editors >> Tying and Make sure that check box "Escape text when pasting into a string literal" is checked

                                      Issue:
                                      Warning message in an XML file "No grammar constraints (DTD or XML Schema) referenced in the document."

                                      Resolution:
                                      Just add <!DOCTYPE xml> below the tags <?xml version="1.0" encoding="UTF-8"?>

                                      Issue:
                                      While compiling maven project getting exception "Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project {project name}: Compilation failure: Compilation failure"

                                      Resolution:
                                      Go to pom.xml and remove <scope>test</scope> all the dependencies.

                                      Issue:

                                      Resolution:



                                      Thursday, June 2, 2016

                                      MAVEN

                                      MAVEN is an innovative software project management tool that provides new concept of a project object model (POM) file to manage project’s build, dependency and documentation. The most powerful feature is able to download the project dependency libraries automatically.

                                      Steps to install MAVEN:-
                                      • Download the  binary file form apache's site
                                      • Unzip the zip file and add to the environment variable.
                                      • To confirm installation use mvn -version on command prompt.
                                      A simple maven project consists of following folder structure
                                      •  project
                                        • src
                                          • main
                                            • java (the source code)
                                            • resources
                                          • test
                                            • java (the unit test code )
                                            • resources ()
                                        • pom.xml (the configuration file containing the information to build the project)
                                      POM.xml stands for Project Object Model and it has the below structure

                                      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                                      <modelVersion>4.0.0</modelVersion>
                                      <groupId>PROJECT_NAME_IN_PACKAGE_NAME_STYLE</groupId>
                                      <artifactId>NAME_OF_JAR</artifactId>
                                      <version>VERSION_OF_JAR</version>
                                      </project>

                                      MVN Install:-
                                      • It is used to install the package to local repository so that it can be referenced by several projects
                                      MVN package:-
                                      •  It is used to build the project
                                      mvn archetype:generate:-

                                      To create a java project in java 
                                      mvn archetype:generate
                                      -DgroupId=com.anshul.ws
                                      -DartifactId=MyWebService
                                      -DarchetypeArtifactId=maven-archetype-quickstart
                                      -DinteractiveMode=false

                                      To support eclipse

                                      mvn eclipse:eclipse -Dwtpversion=2.0
                                      Life cycle of Maven:-
                                      Like we have targets in case of Ant , we have phases in maven

                                      Features of MAVEN:-
                                      • Build process becomes very easy
                                      To create a web-app using maven
                                      1. Using command prompt go to the directory where project needs to be created
                                      2. Type below
                                        mvn archetype:generate -DgroupId=org.arsoft.projects.sood -DartifactId=arsood -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
                                      3. To be able to access the project on eclipse go to the directory and run command
                                        mvn eclipse:eclipse -Dwtpversion=2.0
                                      To create a Java project:

                                      mvn archetype:generate -DgroupId=org.arsoft.projects.sood -DartifactId=arsood -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

                                      To create an EJB project:

                                      mvn archetype:generate -DgroupId=com.arsoft.projects -DartifactId=Common -DarchetypeArtifactId=ejb-javaee6 -DinteractiveMode=false

                                      Understanding POM
                                      1. It stands for Project Object Model and is the fundamental unit of work in maven.

                                      Saturday, January 4, 2014

                                      WeCare

                                      1. All the tables would be under a database named WECARE.
                                      2. To register a hospital to WeCare, the hospital is provided some credentials by WeCare and these will be called WeCareRegisterationCredentials.
                                        1. These will be stored in table called LICENSE.
                                        2. This table will not be accessible to the hospitals
                                        3.  The credentials will be able to register as many hospitals as purchased by the user in license agreement. The maximum number can be controlled by an external property
                                      3. Many members would be able to be registered under the hospital. The main types of the members would be
                                        1. Doctors
                                        2. Clinical Staff (Nurses,Pharmacists)
                                        3. Non Clinical Staff (Clerk, Drivers)\
                                      4.  
                                         

                                      SpringBoot Application Event Listeners

                                      When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...