sp2

/**
*
*/
package com.sample.custom.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.ui.logout.LogoutFilter;
import org.springframework.security.ui.logout.LogoutHandler;
import org.springframework.util.StringUtils;

public class CustomLogoutFilter extends LogoutFilter {

public CustomLogoutFilter(String logoutSuccessUrl, LogoutHandler[] handlers) {
super(logoutSuccessUrl, handlers);
}

/**
* Returns the target URL to redirect to after logout.
* <p>
* By default it will check for a <tt>logoutSuccessUrl</tt> parameter in
* the request and use this. If that isn’t present it will use the configured <tt>logoutSuccessUrl</tt>. If this
* hasn’t been set it will check the Referer header and use the URL from there.
*
*/
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
String targetUrl = request.getParameter(“logoutSuccessUrl”);
Integer orgId = (Integer)request.getAttribute(“sessionOrgId”);

if(!StringUtils.hasLength(targetUrl)) {
targetUrl = getLogoutSuccessUrl();
}

if (!StringUtils.hasLength(targetUrl)) {
targetUrl = request.getHeader(“Referer”);
}

if (!StringUtils.hasLength(targetUrl)) {
targetUrl = “/”;
}
if(orgId != null){
targetUrl = targetUrl + “&orgId=” + orgId;
}
request.removeAttribute(“sessionOrgId”);
return targetUrl;
}

}

sp1

package com.sample.rmcustom.common;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.security.Authentication;
import org.springframework.security.ui.webapp.AuthenticationProcessingFilter;

public class CustomAuthenticationProcessingFilter extends
AuthenticationProcessingFilter {

public static final String LOGIN_HIST_INSERT_SQL = “insert into schema.user_login_hist (login_hist_id,username, ip_address, login_time) values(?,?,?)”;

public static final String LOGIN_SEQ_NEXTVAL_SQL = “select nextval(’schema.user_login_hist_seq’)”;

protected SqlUpdate loginSuccessInsertQry;

protected MappingSqlQuery loginSuccessSeqGenQry;

private DataSource dataSource;

private String schema;

/**
* This method invoked when user logged in with correct credentials.
* <li>Persists user details, ip address along with login time</li>
*/
@Override
protected void onSuccessfulAuthentication(HttpServletRequest request,
HttpServletResponse response, Authentication authResult)
throws IOException {
String schemaName = “sampe”;
setSchema(schemaName);
/*loginSuccessSeqGenQry = new LoginSuccessSeqGenQry(getDataSource());
List ids = (List) loginSuccessSeqGenQry.execute();
loginSuccessInsertQry = new LoginSuccessInsertQry(getDataSource());
Integer loginHistId = (Integer) ids.get(0);
loginSuccessInsertQry = new LoginSuccessInsertQry(getDataSource());
loginSuccessInsertQry.update(new Object[] {loginHistId,
authResult.getName(), request.getRemoteAddr(),
new Date(System.currentTimeMillis()) });*/
}

/**
* Query object to insert login history after successful login.
*/
protected class LoginSuccessInsertQry extends SqlUpdate {

public LoginSuccessInsertQry(DataSource ds) {
super(ds, getQuery(LOGIN_HIST_INSERT_SQL,schema));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.TIMESTAMP));
compile();
}
}

/**
* Query object to get next successful login history sequence val.
*/
protected class LoginSuccessSeqGenQry extends MappingSqlQuery {
protected LoginSuccessSeqGenQry(DataSource ds) {
super(ds, getQuery(LOGIN_SEQ_NEXTVAL_SQL,schema));
compile();
}

protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Integer token = rs.getInt(1);
return token;
}
}

/**
* @return the dataSource
*/
public DataSource getDataSource() {
return dataSource;
}

/**
* @param dataSource the dataSource to set
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

/**
* @return the schema
*/
public String getSchema() {
return schema;
}

/**
* @param schema the schema to set
*/
public void setSchema(String schema) {
this.schema = schema;
}

public static String getQuery( String qry, String schema) {

qry = qry.replace(“schema”, schema);
return qry;
}

}

auth

package com.sample.custom.bo;

import java.io.Serializable;
import java.util.Date;

/**
* Represents a User Role or Authroity and maps to an entry in the AUTHORITIES table
* @hibernate.class table = “AUTHORITIES” lazy = “false”
*/
@SuppressWarnings(“serial”)
public class Authority implements Serializable {

private Integer authId;
private String authority;
private String userName;
private Date createdDate;
private String createdBy;
private Date modifiedDate;
private String modifiedBy;
/**
* @return the authId
* @hibernate.id generator-class = “sequence” column = “AUTH_ID”
* @hibernate.generator-param name=”sequence” value=”AUTHORITIES_AUTH_ID_SEQ”
*/
public Integer getAuthId() {
return authId;
}
/**
* @param authId the authId to set
*/
public void setAuthId(Integer authId) {
this.authId = authId;
}
/**
* @return the authority
* @hibernate.property column = “AUTHORITY”
*/
public String getAuthority() {
return authority;
}
/**
* @param authority the authority to set
*/
public void setAuthority(String authority) {
this.authority = authority;
}
/**
* @return the createdBy
* @hibernate.property column = “CRT_BY”
*/
public String getCreatedBy() {
return createdBy;
}
/**
* @param createdBy the createdBy to set
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
/**
* @return the createdDate
* @hibernate.property column = “CRT_DT”
*/
public Date getCreatedDate() {
return createdDate;
}
/**
* @param createdDate the createdDate to set
*/
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
/**
* @return the modifiedBy
* @hibernate.property column = “MOD_BY”
*/
public String getModifiedBy() {
return modifiedBy;
}
/**
* @param modifiedBy the modifiedBy to set
*/
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
/**
* @return the modifiedDate
* @hibernate.property column = “MOD_DT”
*/
public Date getModifiedDate() {
return modifiedDate;
}
/**
* @param modifiedDate the modifiedDate to set
*/
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
/**
* @return the userName
* @hibernate.property column = “USERNAME”
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
}

db2

– Definition for sequence users_user_id_seq (OID = 63672) :

CREATE SEQUENCE rmcustom.users_user_id_seq
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;

– Definition for sequence authorities_auth_id_seq (OID = 63674) :

CREATE SEQUENCE rmcustom.authorities_auth_id_seq
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;


– Structure for table users (OID = 63713) :

CREATE TABLE rmcustom.users (
user_id integer DEFAULT nextval(‘rmcustom.users_user_id_seq’) NOT NULL,
username character varying(32) NOT NULL,
“password” character varying(128) NOT NULL,
enabled character(1),
appv_stat character(1) DEFAULT ‘n’,
crt_dt date DEFAULT now(),
crt_by character varying(32),
user_type character varying(16),
exp_date date NOT NULL,
first_name character varying(10),
last_name character varying(10),
middle_name character varying(10),
phone_no character varying(20),
email character varying(32),
user_status character varying(3)
) WITHOUT OIDS;


– Structure for table authorities (OID = 63723) :

CREATE TABLE rmcustom.authorities (
username character varying(32) NOT NULL,
authority character varying(32) NOT NULL,
crt_dt date DEFAULT now(),
crt_by character varying(32),
auth_id integer DEFAULT nextval(‘rmcustom.authorities_auth_id_seq’) NOT NULL
) WITHOUT OIDS;


– Data for table lwhp.users (OID = 63713) (LIMIT 0,8)

INSERT INTO rmcustom.users (user_id, username, “password”, enabled, appv_stat, crt_dt, crt_by, user_type, exp_date, first_name, last_name, middle_name, phone_no, email, user_status)
VALUES (2, ‘testuser3′, ‘f9792b837926b07c6a29ec11520d64d1′, ‘1′, ‘n’, ‘2009-09-15′, ‘narendar’, ‘Admin’, ‘2009-10-21′, NULL, NULL, NULL, NULL, NULL, NULL);

INSERT INTO rmcustom.users (user_id, username, “password”, enabled, appv_stat, crt_dt, crt_by, user_type, exp_date, first_name, last_name, middle_name, phone_no, email, user_status)
VALUES (3, ‘testuser4′, ‘f9792b837926b07c6a29ec11520d64d1′, ‘1′, ‘n’, ‘2009-09-15′, ‘narendar’, ‘Admin’, ‘2009-10-21′, NULL, NULL, NULL, NULL, NULL, NULL);

INSERT INTO rmcustom.users (user_id, username, “password”, enabled, appv_stat, crt_dt, crt_by, user_type, exp_date, first_name, last_name, middle_name, phone_no, email, user_status)
VALUES (4, ‘testuser2′, ‘f9792b837926b07c6a29ec11520d64d1′, ‘1′, ‘n’, ‘2009-09-15′, ‘narendar’, ‘Admin’, ‘2009-10-21′, NULL, NULL, NULL, NULL, NULL, NULL);

INSERT INTO rmcustom.users (user_id, username, “password”, enabled, appv_stat, crt_dt, crt_by, user_type, exp_date, first_name, last_name, middle_name, phone_no, email, user_status)
VALUES (1, ‘testuser1′, ‘2a753c085f06c977b6e50bfad2ab34e2′, ‘1′, ‘n’, ‘2009-09-15′, ‘narendar’, ‘Admin’, ‘2010-03-16′, NULL, NULL, NULL, NULL, NULL, NULL);


– Data for table lwhp.authorities (OID = 63723) (LIMIT 0,4)

INSERT INTO rmcustom.authorities (username, authority, crt_dt, crt_by, auth_id)
VALUES (‘testuser1′, ‘SUPER ADMIN’, ‘2009-09-18′, NULL, 1);

INSERT INTO rmcustom.authorities (username, authority, crt_dt, crt_by, auth_id)
VALUES (‘testuser2′, ‘SUPER ADMIN’, ‘2009-09-18′, NULL, 2);

INSERT INTO rmcustom.authorities (username, authority, crt_dt, crt_by, auth_id)
VALUES (‘testuser3′, ‘SUPER ADMIN’, ‘2009-09-18′, NULL, 3);

INSERT INTO rmcustom.authorities (username, authority, crt_dt, crt_by, auth_id)
VALUES (‘testuser4′, ‘SUPER ADMIN’, ‘2009-09-22′, NULL, 4);


– Definition for index users_pkey (OID = 63719) :

ALTER TABLE ONLY rmcustom.users
ADD CONSTRAINT users_pkey PRIMARY KEY (username);

– Definition for index users_user_id_key (OID = 63721) :

ALTER TABLE ONLY rmcustom.users
ADD CONSTRAINT users_user_id_key UNIQUE (user_id);

– Definition for index authorities_pkey (OID = 63728) :

ALTER TABLE ONLY rmcustom.authorities
ADD CONSTRAINT authorities_pkey PRIMARY KEY (username, authority);

– Definition for index authorities_username_fkey (OID = 63735) :

ALTER TABLE ONLY rmcustom.authorities
ADD CONSTRAINT authorities_username_fkey FOREIGN KEY (username) REFERENCES rmcustom.users(username);

hib

<?xml version=”1.0″?>
<project name=”Custom” default=”build-mappings”>

<property name=”context” value=”${basedir}/context” />
<property name=”webinf” value=”${context}/WEB-INF” />
<property name=”webinf.lib” value=”${webinf}/lib” />
<property name=”src.dir” value=”${basedir}/src” />
<!– called proj.* to avoid conflict when called from pepsibuild.xml –>
<property name=”proj.lib.dir” value=”${basedir}/lib” />

<path id=”class.path”>
<fileset dir=”${proj.lib.dir}”>
</fileset>
<fileset dir=”${tomcat.home}/common/lib”>
</fileset>
<fileset dir=”${tomcat.home}/server/lib” />
<fileset dir=”${java.home}/../lib” includes=”tools.jar”/>
</path>

<target name=”build-mappings” >
<echo message=”basedir is ${basedir}”/>
<echo message=”libdir is ${proj.lib.dir}”/>
<path id=”allcontextjars”>
<fileset dir=”${webinf.lib}”>
<include name=”*.jar”/>
</fileset>
<fileset dir=”${proj.lib.dir}”>
<include name=”*.jar”/>
</fileset>
</path>
<taskdef name=”hibernatedoclet”
classname=”xdoclet.modules.hibernate.HibernateDocletTask”
classpathref=”allcontextjars”
/>
<hibernatedoclet
destdir=”${webinf}/classes”
verbose=”true”>
<fileset dir=”${src.dir}”>
<include name=”**/*.java”/>
</fileset>

<hibernate version=”3.0″/>
</hibernatedoclet>
</target>

</project>

struc

structure

stuc

stuc

indx

!
! Redirect to the initial entry action.  Display an informational message
! while loading.
!–>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<META HTTP-EQUIV=”Refresh”
CONTENT=”0;URL=login.action”>
</head>

<body>
<p>Loading…</p>
</body>
</html>

auth

<%@ page contentType=”text/html; charset=UTF-8″ %>
<%@ taglib prefix=”s” uri=”/tags/struts-tags” %>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd”>
<html>
<s:include value=”loginHeader.jsp”/>
<div>User Login</div>
<s:if test=”hasActionErrors()”>
<div id=”errorMessages”>
<ul>
<s:iterator value=”actionErrors”>
<li><s:property/></li>
</s:iterator>
</ul>
</div>
</s:if>

<%– FieldError Messages – usually set by validation rules –%>
<s:if test=”hasFieldErrors()”>
<div id=”errorMessages”>
<ul>
<s:iterator value=”fieldErrors”>
<s:iterator value=”value”>
<li><s:property/></li>
</s:iterator>
</s:iterator>
</ul>
</div>
</s:if>
<div id=”errorMessages1″>
${SPRING_SECURITY_LAST_EXCEPTION.message}
</div>
<s:form id=”loginform” name=”loginform” theme=”newLoginTable” action=”/j_spring_security_check”>
<div style=”text-align: center;”>
<table>
<tr>
<s:textfield id=”j_username” name=”j_username” label=”USERNAME” tabindex=”1″/>
</tr><tr>
<s:password id=”j_password” name=”j_password” label=”PASSWORD” tabindex=”2″/>
</tr><tr>
<td align=”left” valign=”bottom” colspan=”2″>
<div>
<div>
</div>
<div>
<s:submit cssClass=”loginbtn” id=”btnlogin” name=”btnlogin” value=”" tabindex=”3″ theme=”simple”/>
</div>
</div>
</td></tr></table>
</div>
</s:form>

<s:include value=”loginFooter.jsp”/>
</html>

landing

<%@ page contentType=”text/html; charset=UTF-8″ %>
<%@ taglib prefix=”s” uri=”/tags/struts-tags” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>

<title> Registration</title>

<s:include value=”admin-header.jsp”/>
<table>
<tr>
<td>
<s:include value=”admin-menu.jsp”/>
</td>
<td>
<div>
<div>
<h2>Welcome to.</h2>
<p>You logged in as administrator</p>
</div>
</div>
</td>
</tr>
</table>

<s:include value=”admin-footer.jsp”/>
</html>

conf

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!– $Id: struts.xml 3287 2008-08-28 08:42:30Z orpine $ –>
<!DOCTYPE struts PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
“http://struts.apache.org/dtds/struts-2.0.dtd”>

<!– interceptorstacks.xml –>
<struts>
<constant name=”struts.enable.DynamicMethodInvocation” value=”true” />
<constant name=”struts.devMode” value=”false” />

<package name=”custom” namespace=”/” extends=”struts-default”>
<interceptors>
<interceptor-stack name=”defaultStack”>
<interceptor-ref name=”exception”/>
<interceptor-ref name=”servlet-config” />
<interceptor-ref name=”params” />
<interceptor-ref name=”prepare” />
<interceptor-ref name=”chain” />
<interceptor-ref name=”model-driven” />
<interceptor-ref name=”checkbox” />
<interceptor-ref name=”fileUpload”>
<param name=”maximumSize”>2097152</param>
<param name=”allowedTypes”>text/plain,image/jpeg,application/pdf</param>
</interceptor-ref>
<interceptor-ref name=”static-params” />
<interceptor-ref name=”params” />
<interceptor-ref name=”conversionError” />
<interceptor-ref name=”validation”>
<param name=”excludeMethods”>input</param>
</interceptor-ref>
<interceptor-ref name=”workflow”>
<param name=”excludeMethods”>input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>

<interceptor-stack name=”noValidationStack”>
<interceptor-ref name=”exception”>
<param name=”logEnabled”>true</param>
<param name=”logLevel”>WARN</param>
</interceptor-ref>
<interceptor-ref name=”servlet-config” />
<interceptor-ref name=”params” />
<interceptor-ref name=”prepare” />
<interceptor-ref name=”chain” />
<interceptor-ref name=”model-driven” />
<interceptor-ref name=”fileUpload” />
<interceptor-ref name=”static-params” />
<interceptor-ref name=”params” />
<interceptor-ref name=”conversionError” />
<interceptor-ref name=”workflow”>
<param name=”excludeMethods”>input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>

<interceptor-stack name=”defaultLoginStack”>
<interceptor-ref name=”exception”/>
<interceptor-ref name=”servlet-config” />
<interceptor-ref name=”params” />
<interceptor-ref name=”prepare”/>
<interceptor-ref name=”chain” />
<interceptor-ref name=”model-driven” />
<interceptor-ref name=”checkbox” />
<interceptor-ref name=”static-params” />
<interceptor-ref name=”params” />
<interceptor-ref name=”conversionError” />
<interceptor-ref name=”validation”>
<param name=”excludeMethods”>input</param>
</interceptor-ref>
<interceptor-ref name=”workflow”/>
</interceptor-stack>

<interceptor-stack name=”noValidationLoginStack”>
<interceptor-ref name=”exception”/>
<interceptor-ref name=”servlet-config” />
<interceptor-ref name=”params” />
<interceptor-ref name=”prepare”/>
<interceptor-ref name=”chain”/>
<interceptor-ref name=”model-driven”/>
<interceptor-ref name=”static-params”/>
<interceptor-ref name=”params”/>
<interceptor-ref name=”conversionError” />
<interceptor-ref name=”workflow”/>
</interceptor-stack>

</interceptors>

<!– <global-results>
<result name=”accessDenied”>/custom/accessDenied.jsp</result>
<result name=”custom_error”>/custom/customError.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception=”java.lang.Exception” result=”custom_error”/>
</global-exception-mappings>  –>

<!– Security Integration Change Start –>
<action name=”login” class=”com.sample.custom.action.LoginAction”>
<interceptor-ref name=”noValidationLoginStack” />
<result name=”success”>/rmcustom/login.jsp</result>
</action>

<action name=”home”>
<interceptor-ref name=”noValidationLoginStack” />
<result name=”input”>/custom/login.jsp</result>
<result name=”success”>/custom/home.jsp</result>
</action>

</package>

</struts>