'spring security including root into security yet exclude from direct to login page

when I started the project, due to spring security, it automatically directed me to login page not the root page. So, I tried to avoid it, setted root

<security:http pattern="/" security="none"/>

it worked. Security does not direct me to login page when I run the project. Problem is that in the root page, I am showing user information and user is able to logout. However, since I exclude root from security, it does not let me log out.

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
    <security:http pattern="/resources/**" security="none"/>
    <security:http>
        <security:intercept-url pattern="/everyone/**" access="permitAll"/>
        <security:intercept-url pattern="/member/**" access="hasRole('ROLE_MEMBER')"/>
        <security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')"/>
        <security:form-login login-page="/everyone/login"
                             login-processing-url="/everyone/login"
                             authentication-failure-handler-ref="loginFailureHandler"
                             authentication-success-handler-ref="loginSuccessHandler"/>
        <!--로그아웃-->
        <security:logout logout-url="/everyone/logout"
                         success-handler-ref="logoutHandler" />
        <!--권한이 없는 사용자가 접근시 이동-->
        <security:access-denied-handler ref="deniedHandler"/>
        <!-- 최대 한 개의 세션만 생성되도록 -->
        <security:session-management invalid-session-url="/everyone/login">
            <security:concurrency-control max-sessions="1"
                                          expired-url="/everyone/login"
                                          error-if-maximum-exceeded="true" />
        </security:session-management>
    </security:http>

    <security:authentication-manager>   
        <security:authentication-provider user-service-ref="customMemService">
            <security:password-encoder hash="bcrypt"/>
        </security:authentication-provider>
    </security:authentication-manager>

    <bean id="bcryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
    <bean id="loginFailureHandler" class="com.group6.shopping.security.LoginFailureHandler"/>
    <bean id="loginSuccessHandler" class="com.group6.shopping.security.LoginSuccessHandler"/>
    <bean id="logoutHandler" class="com.group6.shopping.security.LogoutHandler"/>

    <bean id="deniedHandler" class="com.group6.shopping.security.DeniedHandler"/>
    <context:component-scan base-package="com.group6.shopping.security" />
    <bean id="customMemService" class="com.group6.shopping.security.CustomMemDetailsService" />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/root-context.xml
            /WEB-INF/spring/security/security-context.xml
        </param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
        
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 한글 설정 -->
    <filter> 
        <filter-name>encodingFilter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        
        <init-param> 
           <param-name>encoding</param-name> 
           <param-value>UTF-8</param-value> 
        </init-param> 
        
        <init-param> 
           <param-name>forceEncoding</param-name> 
           <param-value>true</param-value> 
        </init-param> 
    </filter> 
    
    <filter-mapping> 
        <filter-name>encodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    
    <!-- 한글 설정 끝 -->
    
    <!-- spring-security 설정 -->
    
    <!-- 어플리케이션의 모든 요청을 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>

    <!-- spring-security 설정 끝 -->
</web-app>

So, What I am wondering is that is there way I include root into security while excluding root from directed to login page?



Solution 1:[1]

The problem was on the session-management. After the logout, the page think that session was all invalid thus redirected the page to the login page. So, I changed it to the root page so that after logout, it does not redirect to login page but to root.

Solution 2:[2]

Yes, you can add:

<security:intercept-url pattern="/" access="permitAll"/>

This is similar to what you are already doing with:

<security:intercept-url pattern="/everyone/**" access="permitAll"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jiyong Kwag
Solution 2 jzheaux