Tuesday, 15 March 2016

Spring Project Setup in Eclipse

Step 1:

create workspace in your drive.

Step 2:

add jdk in windows -> preferences -> java -> installed jres

Step 3:

create new dynamic web project.

goto file -> new -> dynamic web project

give module version as 2.5

Step 4:

add tomcat in windows -> preferences -> server -> runtime environments -> add

Step 5:

add spring jars

right click on project -> Build path -> Configure Build Path -> Libraries -> Add external jars -> add spring jar from your local computer

Step 6:

set deployment assembly

right click on project -> Build path -> Configure Build Path -> select deployment assembly -> add -> java build path entries -> add -> apply -> finish

Step 7:

set web.xml:

copy below code within web-apps in web.xml

<!-- project name here -->
<display-name>RestaurantApp</display-name>
 
  <servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

<!-- what files you allow in this project  -->
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

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

<!-- 1 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdnext-*.xml,</param-value>
</context-param>

<!-- starting page here -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<!-- 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>


Step 8:

place sdnext-security.xml and sdnext-servlet.xml files in WEB-INF

sdnext-servlet.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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<!-- <context:property-placeholder location="classpath:resources/database.properties" /> -->

<!-- Base Package start--> <!-- give base packages here -->
<context:component-scan base-package="com.gb" />
<!-- Base Package end-->

<!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> -->

<!-- view(JSP) setup start-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- view(JSP) setup End-->

<!-- database setup start -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springsecuritydbs" /> <!-- DB name here -->
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- database setup end -->

<!-- Model creation in database -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list> <!-- Domains Here -->
<!-- <value>com.gb.model.Login</value>
<value>com.gb.model.Signup</value>
<value>com.gb.model.Employee</value>
<value>com.gb.setups.model.Charges</value>
<value>com.gb.setups.model.SpecialitySetup</value>
<value>com.gb.setups.model.DoctorSetup</value>
<value>com.gb.setups.model.OutPatient</value>  -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Model creation in database -->


<!-- Hibernate Template start -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"> <ref bean="sessionFactory" /> </property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Hibernate Template end -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
</beans>

Here you give


  • base package
  • jsp pages path & extentions
  • database name & credentials
  • domains in sessionFactory
sdnext-security.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:security="http://www.springframework.org/schema/security"
       xmlns:p="http://www.springframework.org/schema/p" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

   <security:http auto-config="true">
<security:intercept-url pattern="/index*" access="ROLE_USER" />
<!-- <security:form-login login-page="/login" default-target-url="/index"  
   authentication-failure-url="/fail2login" />  -->  
</security:http>

    <security:authentication-manager>
 <security:authentication-provider>
   <security:user-service>
<security:user name="admin" password="admin" authorities="ROLE_USER" />
   </security:user-service>
 </security:authentication-provider>
</security:authentication-manager>

</beans>

Step 9:

Flow:

JSP -> Controller -> Service -> Dao -> Model

Step 10:

Create packages in src

like com.gb.spring.controller
com.gb.spring.services
com.gb.spring.dao
com.gb.spring.model

create package like com.vamsi.security

create java file like HelloSecurityController

place the given code in controller for view after login authentication

@Controller
@RequestMapping("/index")
public class HelloSecurityController {
@RequestMapping(method = RequestMethod.GET)
public String executeSecurity(ModelMap model) {
System.out.println("Path Recognized.....");
model.addAttribute("message", "Spring Security Hello World");
model.addAttribute("author", "By DineshOnJava.com");
return "welcome";
}
}

in welcome file list we give file as index, based on that request mapping it renders the welcome.jsp. we created in WEB-INF

the files structure as follows





after running the it shows like as follows


after successful login it will show welcome page as follows


No comments:

Post a Comment