Friday, March 2, 2012

Eclipse plugins

AnyEditTools
CallGraph Viewer
DataHierarchy
Diver
EclEmma Java Code Coverage
Checkstyle - static analysis

FindBugs - static analysis
Groovy-Eclipse
HSQLDB Libraries
JAutodoc - javadoc

Java Dependency Viewer - static analysis
update site: http://svn.codespot.com/a/eclipselabs.org/free-plugins/trunk/site/

Java Examples Search
Java Source Attacher
JDeodrant
JDepend4Eclipse - static analysis
Logging Code Cleanup for Eclipse
update site: http://andrei.gmxhome.de/eclipse/

Metrics plugin for Eclipse
PMD Plug-in - static analysis
SpringSource Tool Suite
Split Editor
StartExplorer
UCDetector (Unnecessary code detector)
Windows Tools
YUI Compressor

Thursday, March 1, 2012

spring rest security using digest authentication

Configure Digest filter as outlined in the link
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity-single.html#digest-processing-filter

Implement UserDetailsService
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity-single.html#d0e1613

<http create-session="never" entry-point-ref="digestEntryPoint" use-expressions="true">
<intercept-url pattern="/<urls>/**" access="isAuthenticated()" />
<custom-filter ref="digestFilter" position="BASIC_AUTH_FILTER" />
</http>

<bean id="userDetailsService" class="CustomUserDetailsService">
<authentication-manager alias="authenticationManager">

<authentication-provider user-service-ref="userDetailsService">

</authentication-provider>

</authentication-manager>
Create user for the application and return with a standard role
example:
class CustomUserDetailsService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if(username.equals("AppUser") {
//load password from database/configuration file
return new
User("AppUser", String password, true, true, true, true, Arrays.asList(
new GrantedAuthorityImpl("AdminRole")))();
}
return null;
}
}

Spring security - pre authentication security mapping - Part 2 configuration


<bean id="siteminderFilter" class=

"org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">

<property name="principalRequestHeader" value="SM_USER"/>

<property name="authenticationManager" ref="authenticationManager" />

<property name="authenticationDetailsSource" ref="authenticationDetailsSource" />

</bean>



<bean id="authenticationDetailsSource" class="CustomAuthenticationDetailsSource">

<property name="mappableRolesRetriever">

<bean class="CustomMappableRolesRetriever">

</bean>

</property>

</bean>



<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">

<property name="preAuthenticatedUserDetailsService">

<bean class="org.springframework.security.web.authentication.preauth.PreAuthenticatedGrantedAuthoritiesUserDetailsService"></bean>

</property>

</bean>

Spring security - pre authentication security mapping

An example for pre-authentication would be site minder which sits between browser and web servers and intercepts requests validating user credentials. On successful validation standard request headers are set with user id.

Here we will discuss about configuring spring security to work with pre authentication mechanism where auhentication and/or authorization (like retrieving roles from LDAP) is performed outside spring security.

Spring security can still be used in such scenarios to implement authorization at a url level, method level or with in jsps for example: to display buttons/tabs based on user roles

Below are the 2 interfaces to be implemented in spring security.

MappableAttributesRetriever
reads standard user roles / user groups from database / configuration file
public abstract Set getMappableAttributes();

AuthenticationDetailsSource
Responsible for returning list of roles associated with user at runtime
AbstractPreAuthenticatedAuthenticationDetailsSource implements AuhtenticationDetailsSource and provides base implementation details and can be used in such scenarios. This class is injected with bean implementing MappableAttributesRetriever, & standard roles/groups can be retrieved from this bean.

protected abstract String[] getUserRoles(Object context,

String[] mappableRoles)

At run time context will be passed, in case of web application context will be HttpServletrequest
If the roles/groups associated with user are stored in user session, a look up can be performed against standard roles/groups configured and matching roles can be returned.

Spring java database configuration


import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
// spring config that loads the properties file
@ImportResource(value = { "classpath:db.xml" })
public class DBConfig {
@Resource
private DataSource oracleDataSource;

@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(oracleDataSource);
}
}


db.xml contains database configuration and looks like

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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">
<bean id="oracleDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@<host>:1521/<database>" />
<property name="username" value="" />
<property name="password" value="" />
<property name="initialSize" value="10" />
<property name="maxActive" value="10" />
</bean>
<bean id="sqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://<host>:1433;DatabaseName=<database>" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
</beans>

Configuration in java

Apache configuration...
http://commons.apache.org/configuration/

Passing/Setting request header using HttpClient


ClientHttpRequestFactory clientHttpRequestFactory = new CommonsClientHttpRequestFactory() {
@Override
protected void postProcessCommonsHttpMethod(HttpMethodBase httpmethodbase) {
httpmethodbase.setRequestHeader("", "");
}
};

Getting weblogic node name in java

String weblogicNodeName = System.getProperty("weblogic.Name");

Getting host name and ip-address in java

InetAddress inetAddress = InetAddress.getLocalHost();
String canonicalHostName = inetAddress.getCanonicalHostName();
String hostName = inetAddress.getHostAddress();
String ipAddress = inetAddress.getHostName();

Thursday, February 16, 2012

dbcp connection pool configuration

<Resource name="jdbc/{ds}DataSource"
auth="Container"
description="Connection Pool Datasource to {ds} Repository"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.driver"
username="{user}"
password="{pwd}"
url="jdbc:mysql:{host}:{port}/{database}"
testOnBorrow="true"
validationQuery="SELECT 1"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"/>