0
votes

I am having issues implementing spring security for static web project.

my web.xml

<?xml version="1.0"?>
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" id="WebApp_ID">
    <display-name>Spring Security Application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> /WEB-INF/spring-security.xml </param-value>
    </context-param>
    <!-- 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>
</web-app>

and my spring-security.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.xsd">

<security:http auto-config="true" use-expressions="true">

    <security:intercept-url pattern="/security/html/folder1/**" access="isAnonymous()" />

    <security:intercept-url pattern="/security/html/folder2/**" access="isAuthenticated()" />

    <security:intercept-url pattern="/security/html/folder3/**" access="hasRole('ROLE_MANAGER')" />
    <security:intercept-url pattern="/security/html/folder4/**" access="hasRole('ROLE_ADMIN','ROLE_USER','ROLE_MANAGER')" />

    <security:form-login password-parameter="password" username-parameter="username" authentication-failure-url="/login?error" login-page="/login" />
    <security:logout logout-success-url="/login?logout" /> 

</security:http>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="user1" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
            <security:user name="user2" password="password" authorities="ROLE_USER" />
            <security:user name="user3" password="password" authorities="ROLE_MANAGER" />
            <security:user name="user4" password="password" authorities="ROLE_ADMIN" />
            <security:user name="user5" password="password" authorities="ROLE_USER, ROLE_MANAGER" />
            <security:user name="user6" password="password" authorities="ROLE_ADMIN, ROLE_MANAGER" />
            <security:user name="user7" password="password" authorities="ROLE_ADMIN, ROLE_USER, ROLE_MANAGER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

My folder structure are src/main/webapp/WEB-INF/web.xml

src/main/webapp/WEB-INF/spring-security.xml

src/main/webapp/WEB-INF/html/folder1/*.html

src/main/webapp/WEB-INF/html/folder2/*.html

src/main/webapp/WEB-INF/html/folder3/*.html

src/main/webapp/WEB-INF/html/folder4/*.html

I do not have any welcome page or any other java files in my project. I need this to deployed in Tomcat server.

Can any one help me out if I am missing anything in config files

1
Can you provide more information about your problem? What issues are you talking about?Dherik

1 Answers

1
votes

I found the answer for my question.

Two mistakes.

  1. I had to remove /security from intercept-url as security is my context root
  2. Move html folder out of WEB-INF