Suresh Payankannur

Wednesday, August 27, 2014

Spring, Spring Data, RestEasy, Apache Shiro and LDAP

Here are the basic configurations needed to integrate Spring, Spring Data, RestEasy, Apache Shiro and LDAP authentication.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=
    "http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

    <!-- LogBack Configuration File  -->
    <context-param>
      <param-name>logbackConfigLocation</param-name>
      <param-value>/WEB-INF/logback.xml</param-value>
    </context-param>

    <!-- Define the spring context locations -->
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/test-service-config.xml</param-value>
    </context-param>

    <!--
        Filters
    -->

    <filter>
      <filter-name>jpaFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetBean</param-name>
      <param-value>jpaFilter</param-value>
    </init-param>
    </filter>

    <filter-mapping>
      <filter-name>jpaFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetBean</param-name>
      <param-value>shiroFilter</param-value>
    </init-param>

    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>

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

 
  <listener>
    <listener-class>
      ch.qos.logback.ext.spring.web.LogbackConfigListener
    </listener-class>
  </listener>

  <!-- 
     Servlet
   -->

  <servlet>
    <servlet-name>testServlet</servlet-name>
    <display-name>Test Servlet</display-name>

    <description>
      A Test Servlet
    </description>

    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
 
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/test-web-config.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>

test-service-config.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:repository="http://www.springframework.org/schema/data/repository"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/data/repository
        http://www.springframework.org/schema/data/repository/spring-repository.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

  <context:annotation-config/>
  <context:component-scan base-package="org.suresh.api.rest.v1"/>
  <context:component-scan base-package="org.suresh.service"/>
  <context:component-scan base-package="org.suresh.controllers"/>

  <jpa:repositories base-package="org.suresh.repo"/>
  <jdbc:embedded-database id="testDb" type="HSQL"/>

  <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="dataSource" ref="testDb"/>
    <property name="persistenceUnitName" value="test"/>
    <property name="packagesToScan">
      <list>
        <value>org.suresh.model</value>
      </list>
    </property>

    <property name="jpaVendorAdapter">
      <bean
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="true"/>
        <property name="database" value="HSQL"/>
      </bean>
    </property>
  </bean>

  <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <property name="dataSource" ref="testDb"/>
  </bean>

  <!-- Security -->
  <security:ldap-server
      root="dc=suresh,dc=org"
      ldif="classpath:resources/test-users.ldif" port="33389"/>

  <bean id="shiroSecurityManager"
        class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="ldapRealm"/>
  </bean>

  <bean id="ldapRealm" class="org.apache.shiro.realm.ldap.JndiLdapRealm">
    <property name="userDnTemplate"
              value="uid={0},ou=people,dc=suresh,dc=org"/>
    <property name="contextFactory" ref="ldapContextFactory"/>
  </bean>

  <bean id="ldapContextFactory"
        class="org.apache.shiro.realm.ldap.JndiLdapContextFactory">
    <property name="url"
              value="ldap://localhost:33389/dc=suresh,dc=org"/>
  </bean>

  <bean id="shiroFilter"
        class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="shiroSecurityManager"/>
    <property name="loginUrl"        value="/views/login.jsp"/>
    <property name="successUrl"      value="/views/home.jsp"/>

    <property name="filterChainDefinitions">
      <value>
        /api/**   = authc
        /views/** = authc
      </value>
    </property>

  </bean>

  <!-- enable shiro annotations -->
  <bean id="lifecycleBeanPostProcessor"
        class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

  <bean
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
      <property name="securityManager" ref="shiroSecurityManager"/>
    </bean>

  <!-- Filters -->
  <bean id="jpaFilter"
        class="org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter"/>
</beans>

test-web-config.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation=
       "http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <import resource="classpath:springmvc-resteasy.xml"/>

  <context:annotation-config/>
  <context:component-scan base-package="org.suresh.api.rest.v1"/>

  <mvc:annotation-driven/>


  <mvc:resources mapping="/images/**" location="/public/images/" order="-1"/>
  <mvc:resources mapping="/stylesheets/**"
                 location="/public/stylesheets/"    order="-1"/>
  <mvc:resources mapping="/javascripts/**"
                 location="/public/javascripts/"     order="-1"/>
  <mvc:default-servlet-handler/>


  <!-- RESTEasy prefix -->
  <bean id="resteasy.handlerMapping" parent="abstract.resteasy.handlerMapping">
    <property name="prefix" value="/api" />
  </bean>
</beans>

0 comments:

Post a Comment

Blog Archive

Scroll To Top