April 1, 2015

How to implement Web Application Basic Authentication in Weblogic

You can easily setup the Basic Authentication in Weblogic by using this post. I assume you are having basic knowledge about JEE. If yes, then you just need to configure Group and User in Weblogic Admin console, and modify the web.xml and weblogic.xml files in WEB-INF folder. That's it. You are done and your application resources are secured.

Lets perform the two activities - Weblogic Configuration and Web Application Configuration:

Weblogic Configuration
(You may use existing users and groups and move to next activity)
Use the following steps:
    1. Login to Weblogic Admin console and go to Security Realms > [myrealm] >Users and Groups (tab) 
    2. Select Groups tab in second tab row
    3. Click on new button
    4. Fill the required fields (Group: testGroup etc) and click on Ok to create the group. (Keep the DefaultAuthenticator as provider)
    5. Now similarly, create the test user by clicking the new button on Users tab. (Keep the DefaultAuthenticator as provider)
    6. Now we need to associate the test user with newly created group testGroup. Go to the users list on Users tab.
    7. Click on the newly created user test
    8. Click on Groups tab.
    9. Select the testGroup and click on Save to complete the steps.
Web ApplicationConfiguration
    • In web.xml

<security-constraint>
   <web-resource-collection>
      <web-resource-name>Secure Page</web-resource-name>
      <url-pattern>/securepages/*</url-pattern>
   </web-resource-collection>
   <auth-constraint>
      <role-name>xAdmin</role-name>
   </auth-constraint>
</security-constraint>
 
<login-config>
   <auth-method>BASIC</auth-method>
   <realm-name>default</realm-name>
</login-config>
 
<security-role>
    <role-name>xAdmin</role-name>
</security-role>

    
      • security-constraint tag contains the resource information like Name and URL patterns and which role can access that information using auth-constraint tag. As you can see I have given name Secure Page and secured all the pages under /securepages directory/context. You can give any name to the role that would have the access to resources defined in URL patterns. This name is an identifier only nothing to do with group we created in above activity. But later we need to map it with actual weblogic group.
      • login-config tag describes what method we are going to use for authentication or how the login form would appear. Here we are using the BASIC, similarly for form-authentication you can use FORM. In Admin console you can check or config which realm you want to use. Same we need to set in realm-name tag
      • You know we have given role in auth-constaint, same we need to declare in web.xml. So in short, we use security-role tag for role declaration.
    • In weblogic.xml

<security-role-assignment>
   <role-name>xAdmin</role-name>
   <principal-name>testGroup</principal-name>
</security-role-assignment>



      • weblogic.xml is also part of WEB-INF directory. Since we have defined the resources to be secured, role that can access them, BASIC authentication method and role declaration the only part remains to map the declared role with weblogic group or individual user. That is done using setting weblogic group in principal-name and declared role in role-name tag.

Now, after performing all the above mentioned activities, basic authentication are up and running. You can test it by accessing the secure page on browser.

Note: Above steps will setup the basic authentication and the browser would provide the login window and it cannot be customized. If you want a custom Login page then please refer the Form Authentication.







Script automate the updates of web.xml and weblogic.xml

Use this script to automate the updating of web.xml and weblogic.xml in any war file
#!/bin/bash

war_file=
ws_name=
ROLE_NAME=

while getopts f:n:r: option
do
  case "${option}" in
    f) war_file=${OPTARG};;
    n) ws_name=${OPTARG};;
    r) ROLE_NAME=${OPTARG};;
  esac
done

OLD_TXT="<\/web-app>"
NEW_TEXT="\n  <security-constraint>\n    <web-resource-collection>\n      <web-resource-name>Secured-Pages<\/web-resource-name>\n      <url-pattern>\/*<\/url-pattern>\n    <\/web-resource-collection>\n    <auth-constraint>\n      <role-name>$ROLE_NAME<\/role-name>\n    <\/auth-constraint>\n  <\/security-constraint>\n  \n  <login-config>\n    <auth-method>BASIC<\/auth-method>\n    <realm-name>default<\/realm-name>\n  <\/login-config>\n  \n  <security-role>\n    <role-name>$ROLE_NAME<\/role-name>\n  <\/security-role>\n<\/web-app>\n"

cp $war_file $war_file.original
jar xvf $war_file
cd WEB-INF/
sed -i -r "s/$OLD_TXT/$NEW_TEXT/g" web.xml

echo "<weblogic-web-app xmlns="'"http://www.bea.com/ns/weblogic/weblogic-web-app"'">
  <context-root>"BanksESB_$ws_name"</context-root>
  <security-role-assignment>
    <role-name>$ROLE_NAME</role-name>
    <principal-name>$ROLE_NAME</principal-name>
  </security-role-assignment>
</weblogic-web-app>" > weblogic.xml

cd ..
jar cvfM $war_file WEB-INF/*
#zip war_file WEB-INF/*
rm -r WEB-INF/

March 23, 2015

Invoke Java Applet from JSF page

I have a Java Applet that will handle some operations on the client machine.
This Applet should be called upon client's request from a JSF page.

Development steps:
1. Managed Bean

1.1 boolean value to enable/disable calling Java Applet

private boolean showApplet = false;

1.2 return the Java Applet HTML invoker

public String getAppletHtml() {
    if (showApplet) {
        return "<object type=\"application/x-java-applet\" id=\"the applet\""
                + "code=\"ics.jvm.setup.JVMSetupApplet.class\" codebase=\"./\""
                + "archive=\"./the-applet.jar\" width=\"0\" height=\"0\">" + "<param name=\"force\" value=\"true\" />";
    }
    return "";
}

1.3 enable/disable action method

public String showApplet() {
    showApplet = true;
    return null;
}

2. JSF page
2.1 Write the calling tags
Note: You have to set the escape attribute of your tag to false. Because as default it's set to true and the HTML related character are escaped.

<h:commandLink value="Reset Configuration" action="#{bean.showApplet}" style="color: white; text-decoration: blink;">
    <h:outputText value="#{bean.appletHtml}" escape="false" />
</h:commandLink>


Cannot Un-deploy a web-app completely in Tomcat

Hi,

I have been using Maven for deploy and redeploy to Tomcat, and it was great cause you don't need always to do it manually.
But in Windows environment I have encounter an issue that is "I cannot undeploy the web application completely from the Tomcat".
I have found a solution and its easy:

Open context.xml from /tomcat/conf folder and modify the context to match this :

<Context antiJARLocking="true" antiResourceLocking="true">

March 10, 2015

Maven: Create "version.properties" file inside the generated Jar file

Sometimes when you create a Jar file and send it to the Client, you need to keep tracking it.

You can append the version with the Jar file name, but what if the client change it?
So, I have found that the best way is to inject a file inside the Jar file contains all necessary attributes as Version Number, Build Number and Build date.

How to do that?
Simply, create a file inside /src/main/resources/ (I called it version.properties).
In this file write the following:

version = ${project.version}
build.no = ${build.version}
build.date = ${timestamp}

Go to pom.xml and set the following properties:


<properties>
  <build.version>2.1.91</build.version>
  <timestamp>${maven.build.timestamp}</timestamp>
  <maven.build.timestamp.format>HH:mm dd.MM.yyyy</maven.build.timestamp.format>
</properties>


in the same file go to and add the following lines:


<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Finished ?! Now when you generate the Jar file you will find the version.properties file in the root of the jar file as follow:

version = project version
build.no = 1.2.3
build.date = 10:20 1.2.2015

Maven: Copy file to Linux from Windows and Execute Remote Bash file

I have been working on a project that requires to run on a remote Linux machine.
Every time I make changes, I build the jar -> sign it -> rename the jar -> copy to Linux Server -> run Bash file.

It was exhausting steps, so I have search for I way that Maven can handle all these steps.
What I have are the followings:

1. Add custom build number


1
2
3
4
5
<properties>
  <build.version>2.1.90</build.version>
  <timestamp>${maven.build.timestamp}</timestamp>
  <maven.build.timestamp.format>HH:mm dd.MM.yyyy</maven.build.timestamp.format>
</properties>

2. Building a Jar file contains all dependencies


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<artifactId>maven-assembly-plugin</artifactId>
  <configuration>
   <finalName>jarfileName-${build.version}</finalName>
   <archive>
     <manifest>
       <addClasspath>false</addClasspath>
       <mainClass>sawalha.MainClassName</mainClass>
     </manifest>
     <manifestEntries>
       <Implementation-Version>${build.version}</Implementation-Version>
       <Permissions>all-permissions</Permissions>
       <Codebase>*</Codebase>
       <Implementation-Build>${project.version}</Implementation-Build>
       <Build-Date>${maven.build.timestamp}</Build-Date>
     </manifestEntries>
   </archive>
   <descriptorRefs>
     <descriptorRef>jar-with-dependencies</descriptorRef>
   </descriptorRefs>
   <appendAssemblyId>false</appendAssemblyId>
   <finalName>jarfileName-${build.version}-dep</finalName>
  </configuration>
  <executions>
   <execution>
     <id>make-assembly</id>
     <phase>package</phase>
     <goals>
       <goal>single</goal>
     </goals>
   </execution>
  </executions>
</plugin>

3. Sign the Jar file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jarsigner-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <id>sign</id>
      <goals>
        <goal>sign</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <keystore>sign-dir\file.p12</keystore>
    <alias>"signature alias"</alias>
    <storepass>storepass</storepass>
    <keypass>keypass</keypass>
    <storetype>pkcs12</storetype>
  </configuration>
</plugin>

4. Setup Apache Ant

    1. Download and Install ANT
    2. Download the optional Ant libraries for FTP (ant-commons-net.jar & commons-net-3.3.jar)
    3. Download the optional Ant libraries for SSH (ant-jsch-1.8.1.jar & jsch-0.1.44.jar)
    4. Create directory under the maven project (I called it ant-lib)

5. Copy the Jar file into Linux machine & Execute the Bash file


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.5</version>
  <configuration>
    <target>
      <echo message="hello ant, from Maven!" />
      <echo>Maybe this will work?</echo>
      <taskdef name="ftp"
        classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
        <classpath>
          <pathelement
            location="/ant-lib/ant-commons-net.jar" />
          <pathelement
            location="/ant-lib/commons-net-3.3.jar" />
        </classpath>
      </taskdef>
      <taskdef name="sshexec"
        classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
        <classpath>
          <pathelement
            location="/ant-lib/ant-jsch-1.8.1.jar" />
          <pathelement
            location="/ant-lib/jsch-0.1.44.jar" />
        </classpath>
      </taskdef>
      <ftp action="send" server="10.20.30.40" remotedir="/deployment/java"
        userid="userName" password="password" dir="${project.build.directory}">
          <include name="jarfileName-${build.version}-dep.jar" />
        </fileset>
      </ftp>
      <sshexec host="10.20.30.40" username="userName" password="password"
        command="/home/scripts/update.fp.sh ${build.version}"
        trust="true" />
    </target>
  </configuration>
</plugin>

To execute the goal, run the following command:
>mvn antrun:run

Waiting for your questions... Have a nice day.

March 4, 2015

Java Swing Designer for eclipse

I have found a great tool for designing and developing Java Swing Applications using eclipse.