0
votes

I am not able to find out my problem in spring security integration. I have spent 2-3 days already.So, please help me.

below is my web.xml file

<?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" 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>cdl</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>startUpServlet</servlet-name>
        <servlet-class>com.qait.cdl.commons.startup.StartUpServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>startUpServlet</servlet-name>
        <url-pattern>/startUpServlet.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>CDL_ENV</param-name>
        <param-value>staging</param-value>
    </context-param>

    <listener>
        <listener-class>com.qait.cdl.commons.startup.CdlContextListner</listener-class>
    </listener>

    <!-- Session timeout -->
    <session-config>
        <session-timeout>600</session-timeout>
    </session-config>

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

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

    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>
     WEB-INF/applicationContext.xml
     WEB-INF/dispatcher-servlet.xml
     </param-value>
    </context-param>


</web-app>

Below is my applicationContext.xml file

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

     <import resource="classapth*:spring/SpringSecurityConfig.xml" />
<!--      <bean name="springSecurityFilterChain" class="org.springframework.web.filter.OncePerRequestFilter"/> -->
</beans>     

Below is my SpringSecurityConfig.xml

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

 <security:http auto-config="true" use-expressions="true">
    <security:intercept-url pattern="/displayAdminPage.htm" access="hasRole('ROLE_ADMIN')" />

    <security:form-login  login-page="/login.htm" authentication-failure-url="/login.htm"/>
    <security:logout logout-url="/logout.htm" logout-success-url="/login.htm"/>
    <security:access-denied-handler error-page="/login.htm" />
</security:http>

<security:authentication-manager>
    <security:authentication-provider user-service-ref="userService" >
    </security:authentication-provider>
</security:authentication-manager>

below is my dispatcher-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!-- Message resource -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>messages</value>
                <value>error</value>
            </list>
        </property>
    </bean>

    <!-- Imports all configuration files -->
    <import resource="classpath*:spring/*.xml" />
    <import resource="classpath*:spring/*/*.xml" />

    <!-- Interceptor mapping -->
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <!-- <property name="interceptors" ref="cdlInterceptor" /> -->
        <property name="interceptors" ref="cdlSessionInterceptor"></property>
    </bean>

    <!-- Tiles view resolver and configuration -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles2.TilesView" />
        <property name="order" value="1" />
    </bean>

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-defs.xml</value>
            </list>
        </property>
    </bean>

    <!-- XmlView Resolver -->
    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location" value="/WEB-INF/spring-Xmlviews.xml" />
        <property name="order" value="0" />
    </bean>

    <!-- MultipartResolver for file upload -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean id="rssViewer" class="com.qait.cdl.rssfeed.view.CustomRssViewer" />

    <!-- Default view resolver mapping <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property 
        name="suffix"> <value>.jsp</value> </property> <property name="order" value="1" 
        /> </bean> -->
</beans>

I have following queries.

  1. Is it necessary to give "filter" tag in web.xml, if yes than why?
  2. In my application, I have two application context(one for spring security and other for dispatcher-servlet), is it possible for springSecurityConfig.xml to access bean definition which is defined in dispatcher-servlet.xml?
  3. what is the flow of spring-security configuration.Upto my knowledge, i have understood that intercept-url tag intercept the request and check appropriate role using expression language.I am not able to understand how it looks appropriate role in DB via authentication-manager i've provided.

below is my userService bean definition in service.xml

    <bean name="userService" class="com.qait.cdl.services.impl.UserServiceImpl">
            <property name="userDao" ref="userDao" />
        </bean> 

below is userService interface

public interface UserService extends UserDetailsService{
}

this UserDetailsService is from springframework

below is UserServiceimpl class

public class UserServiceImpl implements UserService {
 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        UserDetails userDetails = null;
        if(username != null &&  !"".equals(username)){
            User user = userDao.get(username);
            if(user != null){
                UserGroupAuthority groupAuthority = userDao.getUserAuthority(user);
                if(groupAuthority != null){
                    Collection<GrantedAuthority> grantedAuthorities = getGrantedAuthorities(groupAuthority.getAuthority());
                    userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), 
                            true, true, true, true, grantedAuthorities);
                }
            }
        }
        return userDetails;
    }

    @Override
    public Collection<GrantedAuthority> getGrantedAuthorities(String authority) {
        List<GrantedAuthority> grantedAuthorities = new LinkedList<GrantedAuthority>();
        grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_USER"));
        return grantedAuthorities;
    }

    @Override
    public UserGroupAuthority getUserAuthority(User user) {
        return userDao.getUserAuthority(user);
    }
}

Simply the problem is , it is not validating the given intercept-url. Where I am doing mistake?

1
Why your springSecurityFilterChain is deactivated in web.xml?Maksym Demidas
is it necessary to give "springSecurityFilterChain", because I think <url-pattern>/*</url-pattern> intercept all incoming request. But I don't want that. So, i did comment that codeved

1 Answers

0
votes

Activate springSecurityFilterChain in your web.xml. It's an entry point of Spring Security. If springSecurityFilterChain is deactivated then Spring Security will never work.