Thursday, February 3, 2011

Dummy guide for Form based authentication using weblogic10.3

Dummy guide for Form based authentication using weblogic10.3:
Form-based authentication lets developers customize the authentication user interface. The login-config section of web.xml defines the type of authentication mechanism, and the URIs to login and error pages.
These instructions assume a security realm is already created and configured to LDAP active directory.
1. Create Group:
Go to Security Realms -> myrealm (already created realm) -> Users & Groups -> Groups. Create a new group AdminGroup. Select DefaultAuthenticator as provider.
2. Create User:
Go to Security Realms -> myrealm (already created realm) -> Users & Groups -> Users. Create a new user AdminUser. Select DefaultAuthenticator as provider.
3. Associate user with group
Click on the created user and under Groups tab, select the AdminGroup. This will associate the user with the group
4. Associate the user/group security model to the deployment.
Go to Deployments -> myDeployment -> Security. Create New scoped role.
Select the newly created scoped role and Add Conditions. Select group from Predicate List drop down and select the newly created AdminGroup.
Violaa.. you are almost done with the setup in console.
5. On your webapps, add the following into web.xml
<security-constraint>
<display-name></display-name>
<web-resource-collection>
<web-resource-name>AdminGroup</web-resource-name>
<description>Security Constraints for AdminGroup</description>
<url-pattern>/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<description>Security Constraints for AdminGroup</description>
<role-name>AdminGroup</role-name>
</auth-constraint>
<user-data-constraint>
<description>SSL not required</description>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>FORM</auth-method>
<realm-name>MyApplication</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login_error.jsp</form-error-page>
</form-login-config>
</login-config>

<security-role>
<description>Administrators</description>
<role-name>AdminGroup</role-name>
</security-role>

6. Update weblogic.xml with the role assignment
<security-role-assignment>
<role-name>AdminGroup</role-name>
<externally-defined/>
</security-role-assignment>

7. Create login.jsp
<form method="POST" action="j_security_check">
<input type="text" name="j_username">
<input type="password" name="j_password">
</form>
That’s it. That’s all there is for setting up the login.
To logout the user, form based authentication goes by regular HTTP session. So in your logout.jsp, calling session.invalidate(); will invalidate the session and logs the user out.

Wednesday, January 5, 2011

OO Design for ParkingLot

Its a parking lot design day. I am thinking of a simple parking lot where there are finite number of spaces and the space can either be a compact/handicap/regular.
A car can be parked or unparked. The parking lot can be full or empty.

I think below design will do:
ParkingLot: this class can park or unpark and holds finite number of spaces and the spaces are held by two lists one for Parkedspaces and one for unparkedSpaces
ParkingSpace: is another class that represents the actual parking space. It needs a property to specify whether it is a compact/handicap/regular parking space
Car: is the final class that has a property of what kind of car it is and if it is parked, the slot number where it is parked.
public class ParkingLot {
int totalSpaces = 100;
public static List ParkingSpace parkedSpaces = new ArrayList();
public static List ParkingSpace emptySpaces = new ArrayList();

public static void park(Car car) {
//Traverse though the emptySpaces list to find the
//spot for specific car type
//set the car object for that parkingspace
//change it to park. move the object into parkedSpaces
// remove it from emptySpaces
}

public static void unPark(int parkingSpot) {
//get the parkingspot from array
//set the parkingspace to available
}

public static boolean isEmpty() {
if (emptySpaces.size() > 0)
return true;
return false;
}

public static void main(String args[]) {
//build emptySpaces list. Insert 100 parking spaces
//with their type properties
Car car = new Car();
car.setCompact(true);
if(ParkingLot.isEmpty())
ParkingLot.park(car);

ParkingLot.unPark(20);
}
}

public class ParkingSpace {

boolean isCompact;
boolean isRegular;
boolean isHandicap;

//setter and getter calls

}

public class Car {
boolean isCompact;
boolean isRegular;
boolean isHandicap;
int parkingSlotNumber;

public Car() {
}

public void setParkingSlotNumber(int value) {
this.parkingSlotNumber = value;
}
}

The Car object can be further subclassed as CompactCar, HandiCapCar, RegularCar instead of the booleans. Same with the ParkingSpace as well.

But is there any use in that?

Please share your thoughts.

Tuesday, January 4, 2011

In an array find two numbers that add up to a given value X

As part of researching into regular math questions asked in interview, I stumbled upon this simple data structure question of "from an array of integers, given an input x, how do you find two numbers that add up to X.

The basic algorithm to find two numbers that add up to X:
declare hash
for each element in the array
diff = current_element - X
if(!hash.contains(current_element))
hash.put(current_element, diff);
end if
if(hash.contains(diff))
the two numbers are current_element, diff
end if
end for

Here is the tested java code for the same:

public class IntegerArrayTest {
public static final int[] int_array =
{1,2,3,4,5,6,7,8,9,10};
public static java.util.Hashtable intHash =
new java.util.Hashtable();
public static String getResult(int x_number) {
for (int i=0;i int current_element = int_array[i];
int diff = 0;
if( x_number > current_element)
diff = x_number - current_element;
else
diff = current_element - x_number;
if(!intHash.containsKey(current_element)) {
//just add it in
intHash.put(current_element, diff);
}
if(intHash.containsKey(diff)){
//out quest stops here
return new StringBuffer("The numbers are ").
append(current_element).append(" and ").
append(diff).toString();
}
}
return "Did not find mathing number";
}
public static void main(String args[]) {
String x_number = args[0];
String result =
IntegerArrayTest.getResult(
Integer.parseInt(x_number));
System.out.println(result);
}
}

There you go, thats my take on that algorithm. Let me know how it can be enhanced.