복붙노트

[SPRING] Spring 보안 이미 로그인 한 사용자의 리다이렉션 문제

SPRING

Spring 보안 이미 로그인 한 사용자의 리다이렉션 문제

내 GWT 기반 웹 응용 프로그램으로 봄 보안을 구현하는 동안. 나는 그것을 발견했다. 아래의 사실을 제외하고 모든 것이 예상대로 잘 작동합니다.

login.jsp를 열고 유효한 사용자 로그인 자격 증명을 받았습니다. 제출 한 후 홈 페이지로 성공적으로 리디렉션됩니다. 이제 주소 표시 줄의 login.jsp에 대한 URL을 편집 할 때 ... 놀라 울 정도로 login.jsp를 열 수 있지만 멀리는 이해할 수 없습니다. login.jsp로 돌아갈 수 있도록 허용해서는 안됩니다. 로그인 한 상태입니다.

내 security-context.xml 파일이 올바르게 구성되지 않았을 수 있습니다.

아래는 my security-application-context.xml입니다.

<?xml version="1.0" encoding="UTF-8"?>

<!-- - Sample namespace-based configuration - -->

<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-2.5.xsd
                        http://www.springframework.org/schema/security
                        http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

    <global-method-security secured-annotations="enabled">
    </global-method-security>

    <beans:bean id="customAuthenticationProcessingFilter"
        class="edu.authentication.CustomAuthenticationProcessingFilter">
        <custom-filter position="AUTHENTICATION_PROCESSING_FILTER" />
        <beans:property name="defaultTargetUrl" value="/Home.html?gwt.codesvr=127.0.0.1:9997" />
        <beans:property name="authenticationFailureUrl" value="/login.jsp?login_error=1" /> 
        <beans:property name="authenticationManager" ref="authenticationManager" />
    </beans:bean>

    <beans:bean id="authenticationProcessingFilterEntryPoint"
        class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
        <beans:property name="loginFormUrl" value="/login.jsp" />
        <beans:property name="forceHttps" value="false" />
    </beans:bean>

    <beans:bean id="customUserDetailsService"
        class="edu.authentication.CustomUserDetailsService">
        <beans:property name="urmService" ref="urmService" />
    </beans:bean>

    <http auto-config="false" entry-point-ref="authenticationProcessingFilterEntryPoint">

    <intercept-url pattern="/login.jsp*" filters="none" />
        <intercept-url pattern="/forgot_password.jsp*" filters="none" />
        <intercept-url pattern="/forgotPasswordServlet.do*" filters="none" />

    <intercept-url pattern="/myApp/**" access="IS_AUTHENTICATED_FULLY"/>
        <intercept-url pattern="/gwt/**" access="IS_AUTHENTICATED_FULLY"/>
        <intercept-url pattern="/*.html" access="IS_AUTHENTICATED_FULLY"/>

    <logout logout-url="/j_spring_security_logout"
            invalidate-session="true" logout-success-url="/login.jsp?loggedout=true"/>
    </http>

    <authentication-manager alias="authenticationManager" />

    <authentication-provider user-service-ref="customUserDetailsService">
        <password-encoder hash="md5" />
    </authentication-provider>

</beans:beans>

모든 도움 / 제안은 매우 appriciable 것입니다 ..

해결법

  1. ==============================

    1.로그인 후 로그인 페이지를 볼 수 없도록 Spring Security에 내장 된 것은 없습니다. 로그인 페이지 상단에 다음 코드를 추가하여 로그인 한 사용자의 로그인 페이지를 차단할 수 있습니다.

    로그인 후 로그인 페이지를 볼 수 없도록 Spring Security에 내장 된 것은 없습니다. 로그인 페이지 상단에 다음 코드를 추가하여 로그인 한 사용자의 로그인 페이지를 차단할 수 있습니다.

    <%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags' %>
    <sec:authorize ifNotGranted="ROLE_ANONYMOUS">
      <% response.sendRedirect("/mainpage.jsp"); %>
    </sec:authorize>
    

    사용자가 로그인하지 않으면 보안은 익명 인증 객체를 생성하고 ROLE_ANONYMOUS 역할을 제공합니다. 따라서 사용자에게 해당 역할이 있는지 확인하고, 사용자가 로그인하지 않았다면 응용 프로그램의 기본 페이지로 리디렉션한다고 가정 할 수 있습니다.

  2. ==============================

    2.또는 Servlet Filter를 만들 수 있습니다.

    또는 Servlet Filter를 만들 수 있습니다.

    public class LoginPageFilter implements Filter
    {
       public void init(FilterConfig filterConfig) throws ServletException   {
       }
    
       public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,   FilterChain filterChain) throws IOException, ServletException
       {
           HttpServletRequest request = (HttpServletRequest) servletRequest;
           HttpServletResponse response = (HttpServletResponse) servletResponse;
    
           if(request.getUserPrincipal() != null){ //If user is already authenticated
               response.sendRedirect("");// or, forward using RequestDispatcher
           } else{
               filterChain.doFilter(servletRequest, servletResponse);
           }
       }
    
       public void destroy() {
       }
    }
    

    веб.хмл :

    LoginPageFilter                      com.xxx.xx.LoginPageFilter

    <filter-mapping>
        <filter-name>LoginPageFilter</filter-name>
        <url-pattern>/login</url-pattern>
    </filter-mapping>
    
  3. from https://stackoverflow.com/questions/12637915/spring-security-redirection-issue-for-already-logged-in-users by cc-by-sa and MIT license