Integrating Struts2 and spring security -with apache ds
Pre-requisites:
- Apache directory studio-1.5.3
- Apache- Ds server 1.5
- Jboss5.1ga
- Eclipse
- Java 1.6
Add a application-security-ldap.xml to web-inf folder.
The application-secuirty-ldap.xml file contains 2 parts that needs to be configured: http and the authentication manager
The http tag-contains url that needs to be authenticated and and authorised with role.
If a user is authenticated and not authorised -then the app redirects the user to access denied page.This configuration is done in web.xml file.
The second part is the <authentication-manager> this has information about type of authentication provider used, its urls and credentials.In the below example I have used ldap-server which connects with apache ds which is listening at port:10389 and the partition that needs to be searched is "sevenSeas".
The ldap-authentication provider gives input on what attributes needs to be searched for the user name.In the configuration- mail is mapped as username and roles are retrieved from groups the role names are then prefixed with ROLE_
<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-3.1.xsd">
<global-method-security pre-post-annotations="enabled">
<!-- AspectJ pointcut expression that locates our "post" method and applies security that way
<protect-pointcut expression="execution(* bigbank.*Service.post*(..))" access="ROLE_TELLER"/>
-->
</global-method-security>
<http auto-config="true" use-expressions="true" >
<intercept-url pattern="/welcome" access="hasRole('ROLE_ADMIN')"/>
<intercept-url pattern="/common" access="hasRole('ROLE_USER')"/>
<form-login login-page="/login" default-target-url="/secure/common.jsp" authentication-failure-url="/loginfailed?error=true" />
<logout/>
</http>
<!-- <authentication-manager> -->
<!-- <authentication-provider> -->
<!-- <user-service> -->
<!-- <user name="mkyong" password="123456" authorities="ROLE_USER" /> -->
<!-- </user-service> -->
<!-- </authentication-provider> -->
<!-- </authentication-manager> -->
<!-- <authentication-manager> -->
<!-- <authentication-provider> -->
<!-- <jdbc-user-service -->
<!-- data-source-ref="MySqlDatasource" -->
<!-- users-by-username-query="select username,password, enabled from users1 where username=?" -->
<!-- authorities-by-username-query="select u.username, ur.authority from users1 u, user_roles ur where u.user_id = ur.user_id and u.username =? " /> -->
<!-- </authentication-provider> -->
<!-- </authentication-manager> -->
<authentication-manager>
<ldap-authentication-provider
user-search-filter="(mail={0})"
user-search-base="ou=people"
group-search-filter="(uniqueMember={0})"
group-search-base="ou=groups"
group-role-attribute="cn"
role-prefix="ROLE_">
</ldap-authentication-provider>
</authentication-manager>
<ldap-server url="ldap://localhost:10389/o=sevenSeas" manager-dn="uid=admin,ou=system" manager-password="secret" />
</beans:beans>
Struts.xml
1: <!DOCTYPE struts PUBLIC
2: "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
3: "http://struts.apache.org/dtds/struts-2.0.dtd">
4: <struts>
5: <package name="default" namespace="/" extends="struts-default">
6: <action name="helloWorld">
7: <result>success.jsp</result>
8: </action>
9: <action name="login">
10: <result>login.jsp</result>
11: </action>
12: <action name="common" class="com.prdc.spring3.HelloWorld">
13: <result name="SUCCESS">secure/common.jsp</result>
14: </action>
15: <action name="loginfailed">
16: <result>login.jsp?error=true</result>
17: </action>
18: <action name="welcome" class="com.prdc.spring3.HelloWorld">
19: <result name="SUCCESS">secure/hello.jsp</result>
20: </action>
21: <action name="welcome2" class="com.prdc.spring3.HelloWorld2">
22: <result name="SUCCESS">secure/hello1.jsp</result>
23: </action>
24: </package>
25: </struts>
web.xml
1: <?xml version="1.0" encoding="UTF-8"?>
2: <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">
3: <display-name>SpringStruts2Security</display-name>
4: <context-param>
5: <param-name>contextConfigLocation</param-name>
6: <param-value>
7: /WEB-INF/applicationContext-security.xml
8: </param-value>
9: </context-param>
10: <filter>
11: <filter-name>springSecurityFilterChain</filter-name>
12: <filter-class>
13: org.springframework.web.filter.DelegatingFilterProxy
14: </filter-class>
15: </filter>
16: <filter-mapping>
17: <filter-name>springSecurityFilterChain</filter-name>
18: <url-pattern>/*</url-pattern>
19: </filter-mapping>
20: <filter>
21: <filter-name>struts2</filter-name>
22: <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
23: </filter>
24: <listener>
25: <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
26: </listener>
27: <filter-mapping>
28: <filter-name>struts2</filter-name>
29: <url-pattern>/*</url-pattern>
30: </filter-mapping>
31: <error-page>
32: <error-code>403</error-code>
33: <location>/secure/denied.jsp</location>
34: </error-page>
35: <welcome-file-list>
36: <welcome-file>index.jsp</welcome-file>
37: </welcome-file-list>
38: </web-app>
HelloWorld.java
package com.prdc.spring3;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.struts2.ServletActionContext;
public class HelloWorld {
private String message;
private String username;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
/*to get the authenticated username*/
HttpServletRequest request = ServletActionContext.getRequest();
this.setUsername(request.getUserPrincipal().getName());
System.out.println("This is the principal"+request.getUserPrincipal().getName());
this.setMessage("Successful Struts spring secuirty authentication");
return "SUCCESS";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
HelloWorld2.java
package com.prdc.spring3;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.struts2.ServletActionContext;
public class HelloWorld2 {
private String message;
private String username;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
/*to get the authenticated username*/
HttpServletRequest request = ServletActionContext.getRequest();
this.setUsername(request.getUserPrincipal().getName());
this.setMessage("Successful Struts spring secuirty authentication");
return "SUCCESS";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Pom-dependecies
<dependency>
<groupId>antlr-runtime</groupId>
<artifactId>antlr-runtime-3.0</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance-1.0</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>aspectjrt</groupId>
<artifactId>aspectjrt-1.6.8</artifactId>
<version>1.6.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>aspectjweaver</groupId>
<artifactId>aspectjweaver-1.6.8</artifactId>
<version>1.6.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>commons-dbcp</artifactId>
<version>dbcp</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload-1.2.1</artifactId>
<version>1.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io-1.3.2</artifactId>
<version>1.3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang-2.5</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-1.0.4</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool-1.5.4</artifactId>
<version>1.5.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>freemarker</groupId>
<artifactId>freemarker-2.3.13</artifactId>
<version>2.3.13</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jcl-over-slf4j</groupId>
<artifactId>jcl-over-slf4j-1.6.0</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl-1.1.2</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-3.8.1</artifactId>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ldapbp</groupId>
<artifactId>ldapbp-1.0</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j-1.2.14</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j-1.2.9</artifactId>
<version>1.2.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql-connector-java-5.1.17</groupId>
<artifactId>mysql-connector-java-5.1.17-bin</artifactId>
<version>bin</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl-2.6.11</artifactId>
<version>2.6.11</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>slf4j-api</groupId>
<artifactId>slf4j-api-1.6.0</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>slf4j-log4j12</groupId>
<artifactId>slf4j-log4j12-1.6.0</artifactId>
<version>1.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-aop</groupId>
<artifactId>spring-aop-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-asm</groupId>
<artifactId>spring-asm-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-beans</groupId>
<artifactId>spring-beans-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-context</groupId>
<artifactId>spring-context-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-context-support</groupId>
<artifactId>spring-context-support-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-core</groupId>
<artifactId>spring-core-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-expression</groupId>
<artifactId>spring-expression-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-jdbc</groupId>
<artifactId>spring-jdbc-3.0.7.RELEASE</artifactId>
<version>3.0.7.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-core</groupId>
<artifactId>spring-ldap-core-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-core-tiger</groupId>
<artifactId>spring-ldap-core-tiger-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-ldif-batch</groupId>
<artifactId>spring-ldap-ldif-batch-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-ldif-core</groupId>
<artifactId>spring-ldap-ldif-core-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-odm</groupId>
<artifactId>spring-ldap-odm-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-ldap-test</groupId>
<artifactId>spring-ldap-test-1.3.1.RELEASE</artifactId>
<version>1.3.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-acl</groupId>
<artifactId>spring-security-acl-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-aspects</groupId>
<artifactId>spring-security-aspects-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-config</groupId>
<artifactId>spring-security-config-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-core</groupId>
<artifactId>spring-security-core-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-ldap</groupId>
<artifactId>spring-security-ldap-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-taglibs</groupId>
<artifactId>spring-security-taglibs-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-security-web</groupId>
<artifactId>spring-security-web-3.1.1.RELEASE</artifactId>
<version>3.1.1.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-tx</groupId>
<artifactId>spring-tx-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-web</groupId>
<artifactId>spring-web-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>spring-webmvc</groupId>
<artifactId>spring-webmvc-3.0.6.RELEASE</artifactId>
<version>3.0.6.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>standard</groupId>
<artifactId>standard-1.1.2</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>struts2-convention-plugin</groupId>
<artifactId>struts2-convention-plugin-2.1.6</artifactId>
<version>2.1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>struts2-core</groupId>
<artifactId>struts2-core-2.1.6</artifactId>
<version>2.1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>struts2-spring-plugin</groupId>
<artifactId>struts2-spring-plugin-2.1.6</artifactId>
<version>2.1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xwork</groupId>
<artifactId>xwork-2.1.2</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xwork</groupId>
<artifactId>xwork-2.1.2</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
Comments
Post a Comment