Showing posts with label Security. Show all posts
Showing posts with label Security. Show all posts

Sunday, November 30, 2014

Tomcat JDBC Realm Security

Tomcat JDBC Realm Security Example

In previous article we already had a look on that, how to use the UserDatabase realm security and the web.xml configurations. In this article you able to learn, how to configure the JDBC realm in tomcat server and how to use the realm security in the web application. 

The main benefit of the JDBC realm is that, its possible to update or maintain the users and their relevant roles dynamically than server start up. In the UserDatabase Realm, the users are configured in 'tomcat-users.xml', which is loading when the server start up

The main objective of the tutorial is that, using the tomcat container set JDBC realm for the access the secure resources. In order to achieve the objective mentioned above, we are going to develop an web application with using eclipse and set the security access in application web.xml file. In addition to that, we are going to do small configuration in tomcat 'server.xml' file, in order to support the JDBC realm.

Requirements : 
  • Eclipse Java IEE IDE
  • JDK 1.6
  • Appache tomcat - 6.0.26
  • MySQL server 5.5
Step 1 : Create a table 'users' and 'users_roles' table in MySQL test database with using the following script.

create table users (
  user_name         varchar(15) not null primary key,
  user_pass         varchar(15) not null
);

create table user_roles (
  user_name         varchar(15) not null,
  role_name         varchar(15) not null,
  primary key (user_name, role_name)
);

Insert the test data to created table with using the below scrip. 
INSERT INTO users VALUES (
'test', 'test123'
);

INSERT INTO user_roles VALUES (
'test', 'VIP'
);

INSERT INTO users VALUES (
'john', 'john123'
);

INSERT INTO user_roles VALUES (
'john', 'Admin'
);

INSERT INTO users VALUES (
'ruki', 'ruki123'
);

INSERT INTO user_roles VALUES (
'ruki', 'Member'
);

INSERT INTO users VALUES (
'james', 'james123'
);

INSERT INTO user_roles VALUES (
'james', 'Member'
);

INSERT INTO users VALUES (
'mark', 'mark123'
);

INSERT INTO user_roles VALUES (
'mark', 'Member'
);


INSERT INTO users VALUES (
'manager', 'manager123'
);

INSERT INTO user_roles VALUES (
'manager', 'manager'
);

So far we completed the data setup. The next step we are going to configure the tomcat server for in order to support the JDBC realm security.

Step 2 : In this step we are going to look at the tomcat configuration for support JDBC Realm. In order to do that, first comment the following lines in the server.xml file located in apache-tomcat-6.0.26\conf directory. 

1
2
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

Secondly add the following lines to the 'server.xml' file in which was located in apache-tomcat-6.0.26\conf directory.

1
2
3
4
5
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
   driverName="com.mysql.jdbc.Driver"
   connectionURL="jdbc:mysql://localhost:3306/test?user=root&amp;password=123"
   userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/>

In the above configuration we mentioned that My SQL driver, connection URL with username and password. In addition to that, we configured the users and user_roles table with the columns names. That's all for the tomcat configuration.

Step 3 : Create a Dynamic web project in Eclipse and name it as 'JDBCRealmSecurity'. The way of creating the dynamic web project as below. 
File > New > Dynamic Web Project

Step 4 : Create a 'index.jsp' file under the WebContent folder. In this jsp file contains the link to access secure jsp file name it as 'confidential.jsp'. 

Step 5 : Create the 'confidential.jsp' file under the WebContent\confidential directory. In this 'confidential.jsp' file contains 'This is confidential resource.' message. 

Step 6 : In this example we will use the 'web.xml' security configuration which we defined in the UserDatabase Realm Security article in step 5.

Step 7 : If run the 'JDBCRealmSecurity' project and access the 'confidential.jsp' page with click the URL defined in the 'index.jsp' the browser will ask the username and password. Until specify the VIP role user's username and password you unable to access the secure page.

Sunday, November 16, 2014

Tomcat UserDatabase Realm Security


Tomcat UserDatabase Realm Security example

Authentication can be controlled by application level, or by the container level (for example tomcat or jboss) that the application runs in. In this tutorial we are going to explain the example with using the tomcat container.

In the tomcat's container the application security managed by based on the realm configured. 

In this tutorial you can learn that, how to set up the UserDatabase realm (Which is default active realm in tomcat configuration), and how to define the security details in web.xml file. 

Object : The main objective of the tutorial is that, using the tomcat container set UserDatabase realm for access the secure resources.

Scope : In order to achieve the objective mentioned above, we are going to develop a web application with using eclipse and set the security access details in web.xml file for access the secure resources with using UserDatabase realm.

Requirements : 
  • Eclipse Java IEE IDE
  • JDK 1.6
  • Apache Tomcat - 6.0.26

Step 1 : First we have to set the tomcat users and roles for access the secure resources. In order to achieve that add the following specific roles and user credentials to "tomcat-users.xml" file. You have to specify the same username and password for when access the secure resources.

1
2
3
4
5
6
<tomcat-users>
  <role rolename="VIP"/>
  <role rolename="ADMIN"/>
  <user username="vip" password="vip123" roles="VIP"/>
  <user username="admin" password="admin123" roles="ADMIN"/>
</tomcat-users>

In the above file we specified two roles VIP, ADMIN and their credential details. If user have the VIP privileges they have to specify the username and password are respectively 'vip'  and 'vip123'.
If the user have the ADMIN privileges then the username and password are respectively 'admin' and 'admin123'.

Step 2 : Create a Dynamic web project with using the Eclipse and Name it as "UserDataBaseAuthentication". The path of create the dynamic web project as below.
                   File > New > Dynamic Web Project

Step 3 : Create a "index.jsp" file under the WebContent folder. The index file have link to access the secure jsp file name it as "confidential.jsp"

/UserDataBaseAuthentication/WebContent/index.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>findanidea.blogspot.com</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <a href="confidential/confidential.jsp">Access the connfidential page.</a>
    </body>
</html>

Step 4 :  Create a "confidential.jsp" file under the WebContent\confidential folder. The file have "This is Confidential resource." message.

Step 5 : Now the time to configure the security elements to "web.xml". For the authentication process we are going to add the <security-role>,<login-config> and <security-constraint> tags to web.xml file.

/UserDataBaseAuthentication/WebContent/WEB-INF/web.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 <security-role>
  <role-name>VIP</role-name>
 </security-role>

 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>

 <security-constraint>
  <web-resource-collection>
   <web-resource-name>CONFITEST</web-resource-name>
   <http-method>POST</http-method>
   <http-method>GET</http-method>
   <url-pattern>/confidential/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
   <role-name>VIP</role-name>
  </auth-constraint>
  <user-data-constraint>
   <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
   <transport-guarantee>NONE</transport-guarantee>
  </user-data-constraint>
 </security-constraint>

In this example we define the role name as VIP only have the access the resources inside the folder 'confidential'. In addition to that the secure resources can be access via GET and POST http methods only.

There are four type of authentication types available such as BASIC, FORM, DIGEST and CLIENT-CERT. In this example we used BASIC authentication type.

We can specify three type of transport guarantee parameters such as NONE, CONFIDENTIAL and INTEGRAL in the line number 21. 

Step 6 : When you run the "UserDataBaseAuthentication" project, the "index.jsp" file will display in browser. When try to access the "confidential.jsp" via click the link, the browser prompt will ask the username and password details for authenticate before access the resource. If VIP privilege user enter the correct username and password then the user able to access the confidential page. Otherwise they will get error message.



Tuesday, October 14, 2014

Spring MVC Security Login


Spring MVC Security Login


Spring MVC Security Login

Scope : In this example explained that how to handle the default Spring security with MVC.

Objective : The user able to login successfully if specify the correct password only.

Requirements:
  • Spring 3.0 
  • Eclipse JAVA EE IDE
  • JDK 1.6
  • ApacheTomcat - 6.0.26

Step 1: Create a dynamic web project in eclipse and named as "SpringMVCLogin".
             File à New à Dynamic Web Project

             Lets prepare the project structure as below.



Step 2: Add all the spring and other related jars to WebContent\WEB-INF\lib. Please refer the default set of jars here (in step 3). In addition the following spring security jars added to the same lib directory in this example.
  • spring-security-config-3.0.5.RELEASE.jar
  • spring-security-core-3.0.5.RELEASE.jar
  • spring-security-web-3.0.5.RELEASE.jar
Step 3: The controller "LoginController" class created under the "com.prem.spring.controller" package.

/SpringMVCLogin/src/com/prem/spring/controller/LoginController.java

package com.prem.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

 @RequestMapping("/home")
 public ModelAndView getHome() {
  String welcomeNote = "Welcome to the Spring secure page...";
  return new ModelAndView("home", "welcomenote", welcomeNote);
 }
}

Step 4: In the "web.xml" need to specified the spring security filter. Find below the complete "web.xml" file for the reference. 

/SpringMVCLogin/WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>SpringMVCLogin</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <servlet-name>spring-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring-dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>  
       /WEB-INF/spring-dispatcher-servlet.xml,  
       /WEB-INF/spring-security.xml  
    </param-value>
 </context-param>

 <!-- Spring Security -->
 <filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>


Step 5: For this example there are two spring-xxxxxx.xml's defined.

1. spring-dispatcher-servlet.xml - View resolver configured in the dispatcher file.
2. spring-security.xml - The user access credentials and roles defined.

/SpringMVCLogin/WebContent/WEB-INF/spring-dispatcher-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="  
        http://www.springframework.org/schema/beans       
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.prem.spring.controller" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/WEB-INF/pages/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>

</beans>  

/SpringMVCLogin/WebContent/WEB-INF/spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"  
 xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/security  
           http://www.springframework.org/schema/security/spring-security.xsd">  
  
 <http auto-config="true">  
  <intercept-url pattern="/home*" access="ROLE_ADMIN" />  
 </http>  
  
 <authentication-manager>  
  <authentication-provider>  
   <user-service>  
    <user name="prem" password="premen" authorities="ROLE_ADMIN" />  
   </user-service>  
  </authentication-provider>  
 </authentication-manager>  
  
</beans:beans>



Step 6: Now the time to create the index & home jsp pages.

/SpringMVCLogin/WebContent/index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<% response.sendRedirect("home"); %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVCLogin</title>
</head>
<body>Welcome to Spring Security...
</body>
</html>

/SpringMVCLogin/WebContent/WEB-INF/pages/home.jsp


<html>
<head>
<title>Authenticated User</title>
</head>
<body>
 <center>
  <h1><u>Spring Security</u></h1>
  <h4>${welcomenote}</h4>
  <c:url value="/j_spring_security_logout" var="logoutUrl" />
  <a href="${logoutUrl}">Log Out</a>
 </center>
</body>
</html>

Step 7: Right click on the "SpringMVCLogin" project and go to Run As - Run on Server.

The initial screen looks as below.




Once user enter the wrong username or password, the error screen displayed as below.


If the user enter the correct username & password, the following success screen displayed.