0 Replies - 319 Views - Last Post: 16 March 2015 - 07:15 AM

#1 14th_Doctor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Implementing a Spring Security Login Page to An Existing Project

Posted 16 March 2015 - 07:15 AM

OK, so I have been working on this project, and want to implement a Spring Security Login Page. It is being implemented within a project that already exist so, I only need the login page to redirect to the projects index page (in my example I've added the logout button to the index).

I started off using this tutorial...

http://codehustler.o...ial-form-login/

But got all the stay to Step 4.4 tried running it and got a 404 error that said my resource was not available(). I went back all the way and found out the problem was from within the web.xml Any change I make within the web.xml triggers that error.

I have been working on this for days, and everything seems to check out according to what I'm seeing in various tutorials.

Below is the web.xml

    <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">
       
       
      <display-name>My Web Page</display-name>
  
       <!-- Spring Config Files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:servlet-page.xml
            classpath:security-applicationContext.xml
        </param-value>
    </context-param>

    <!-- Spring Listeners -->
    <listener>
       
                                                                             <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  <servlet>
       <servlet-name>page</servlet-name>
       <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
       </servlet-class>
       <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/servlet-page.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
  </servlet>
  
      <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>.jsp</url-pattern>
       </servlet-mapping>
    
    <!-- Security Filter -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
             <filter-  class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  
  

    </web-app>





This is my controller for both index and login

    @RequestMapping( value = "/", method = RequestMethod.GET )
    public String index()
	{
	    return "index";
	}
	

	
	@RequestMapping( value = "/login", method = RequestMethod.GET )
	public ModelAndView login(
			@RequestParam(value = "error", required = false) String error,
			@RequestParam(value = "logout", required = false) String logout) {
	 
			ModelAndView model = new ModelAndView();
			if (error != null) {
				model.addObject("error", "Invalid username and password!");
			}
	 
			if (logout != null) {
				model.addObject("msg", "You've been logged out successfully.");
			}
			model.setViewName("login");
	 
			return model;
	 
		}





Here is my spring-security.xml page

       <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.2.xsd">
 
	<http auto-config="true">
		<intercept-url pattern="/admin**" access="ROLE_USER" />
		<form-login 
		    login-page="/login" 
		    default-target-url="/index" 
			authentication-failure-url="/login?error" 
			username-parameter="username"
			password-parameter="password" />
		<logout logout-success-url="/login?logout" />
		<csrf/>
	</http>
	
    <authentication-manager>

	


Is This A Good Question/Topic? 0
  • +

Page 1 of 1