HomeController .java
- @Controller
- public class HomeController {
- static final Logger log=LoggerFactory.getLogger(HomeController.class.getName());
- @RequestMapping(value="/index",method = RequestMethod.GET)
- public String success(ModelMap map) {
- return "welcome";
- }
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- 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">
- <display-name>SpringTiles</display-name>
- <welcome-file-list>
- <welcome-file>customLogin.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/spring-servlet.xml,
- /WEB-INF/security-config.xml,
- </param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Spring Security Configuration -->
- <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>
- </web-app>
customLogin.jsp
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <html>
- <head>
- <title>Spring Security Login page</title>
- <script type="text/javascript">
- function validation(){
- var a = document.form.j_username.value;
- if(a==""){
- alert("Please Enter User Name");
- document.form.j_username.focus();
- return false;
- }
- if(!isNaN(a)){
- alert("Please Enter Only Characters");
- document.form.j_username.select();
- return false;
- }
- var a = document.form.j_password.value;
- if(a==""){
- alert("Please Enter Your Password");
- document.form.j_password.focus();
- return false;
- }
- if ((a.length < 4) || (a.length > 8)){
- alert("Your Password must be 4 to 8 numbers");
- document.form.j_password.select();
- return false;
- }
- }
- </script>
- </head>
- <body>
- <center>
- <br>
- <br>
- <h2>Custom Login Form</h2>
- <c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION}">
- <font color="red"> Your login attempt was not successful due
- to <br />
- <br /> <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />.
- </font>
- </c:if>
- <form name='form' action='j_spring_security_check' onsubmit="return validation()"
- method='POST'>
- <table>
- <tr>
- <td>User Name:</td>
- <td><input type='text' name='j_username' value=''></td>
- </tr>
- <tr>
- <td>Password:</td>
- <td><input type='password' name='j_password' /></td>
- </tr>
- <tr>
- <td colspan='2'><input name="submit" type="submit"
- value="Login" /></td>
- </tr>
- </table>
- </form>
- </center>
- </body>
- </html>
spring-servlet.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- 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-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/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <context:component-scan base-package="com.hps.platform.portal.controller" />
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass">
- <value>
- org.springframework.web.servlet.view.tiles2.TilesView
- </value>
- </property>
- </bean>
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- max upload size in bytes -->
- <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
- <!-- max size of file in memory (in bytes) -->
- <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
- </bean>
- <bean id="tilesConfigurer"
- class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
- <property name="definitions">
- <list>
- <value>/WEB-INF/tiles.xml</value>
- </list>
- </property>
- </bean>
- </beans>
spring-security.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <http auto-config="true">
- <intercept-url pattern="/customLogin.jsp" access="ROLE_ANONYMOUS" />
- <intercept-url pattern="/**" access="ROLE_USER" />
- <form-login login-page="/customLogin.jsp"
- default-target-url="/index.html"
- authentication-failure-url="/customLogin.jsp"/>
- </http>
- <authentication-manager>
- <authentication-provider>
- <jdbc-user-service data-source-ref="dataSource"
- authorities-by-username-query="SELECT username, authority FROM authorities WHERE username = ?"
- users-by-username-query="SELECT username, password, enabled FROM users WHERE username = ?" />
- </authentication-provider>
- </authentication-manager>
- <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
- <beans:property name="username" value="root" />
- <beans:property name="password" value="root" />
- </beans:bean>
- </beans:beans>