복붙노트

[SPRING] Spring 보안에서 authentication-success-handler-ref와 동등한 커스텀 필터 사용법

SPRING

Spring 보안에서 authentication-success-handler-ref와 동등한 커스텀 필터 사용법

일부 아이템 ID와 같은 스프링 보안에 로그인 세부 정보가있는 일부 매개 변수를 전달하려고합니다. 그런 다음 사용자 유형에 따라 페이지로 리디렉션하려고합니다. 이를 위해 사용자 지정 필터를 사용하여 추가 매개 변수를 보내고 있습니다. 그리고 리다이렉션에 나는 authentication-success-handler-ref를 사용하고있다. 내 문제는, 내가 사용자 지정 필터와 함께 사용하고 위치 충돌을 얻고있다. 제 일을 도와주세요.

여기에 내 구성이있다.

<http   use-expressions="true">
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />

       <custom-filter ref="ddAuthenticationFilter" position="FORM_LOGIN_FILTER" />
        <form-login authentication-failure-url="/accessdenied" 
        authentication-success-handler-ref="ddAuthenticationSuccessHandler"/>



    </http>

    <beans:bean id="ddAuthenticationFilter" class="com.dd.security.ExUsernamePasswordAuthenticationFilter"/>

    <beans:bean id="ddAuthenticationSuccessHandler" class="com.dd.security.DDAuthenticationSuccessHandler" />

해결법

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

    1.귀하의 질문을 다음과 같이 이해했습니다 : 리디렉션을위한 로그인 성공 후 사용되는 로그인 형식으로 itemId를 제출하고 싶습니다.

    귀하의 질문을 다음과 같이 이해했습니다 : 리디렉션을위한 로그인 성공 후 사용되는 로그인 형식으로 itemId를 제출하고 싶습니다.

    이러한 과정을 수립하기 위해서는 다음과 같은 일들이 필요합니다.

    구성에서 을 제거하십시오. 당신은해야한다 :

    <http use-expressions="true" entry-point-ref="authenticationEntryPoint">
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />
    
        <custom-filter ref="ddAuthenticationFilter" position="FORM_LOGIN_FILTER" />
        <security:logout />
    </http>
    

    logout에 을 추가하는 것을 잊지 말고 entry-point-ref 속성은 authenticationEntryPoint를 가리 킵니다.

    로그인 페이지를 가리키는 entry-point-ref에 대한 LoginUrlAuthenticationEntryPoint를 추가하십시오.

    <bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <constructor-arg name="loginFormUrl" value="/login" />
    </bean>
    

    다음 구성을 충족하도록 ddAuthenticationFilter를 리펙터링하십시오.

    <bean id="ddAuthenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
        <property name="authenticationManager" ref="authenticationManager" />
        <property name="filterProcessesUrl" value="/j_spring_security_check" />
        <property name="authenticationFailureHandler" ref="authenticationFailureHandler" />
        <property name="authenticationSuccessHandler" ref="ddAuthenticationSuccessHandler" />
        <property name="authenticationDetailsSource">
            <bean class="security.CustomWebAuthenticationDetailsSource" />
        </property>
    </bean>
    
    <bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
        <property name="defaultFailureUrl" value="/accessdenied" />
    </bean>
    

    새 클래스 CustomWebAuthenticationDetailsSource를 만듭니다.

    package security;
    
    import org.springframework.security.authentication.AuthenticationDetailsSource;
    import org.springframework.security.web.authentication.WebAuthenticationDetails;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class CustomWebAuthenticationDetailsSource implements AuthenticationDetailsSource<HttpServletRequest, WebAuthenticationDetails> {
        @Override
        public WebAuthenticationDetails buildDetails(HttpServletRequest context) {
            return new CustomWebAuthenticationDetails(context);
        }
    }
    

    및 관련 CustomWebAuthenticationDetails :

    package security;
    
    import org.springframework.security.web.authentication.WebAuthenticationDetails;
    import javax.servlet.http.HttpServletRequest;
    
    public class CustomWebAuthenticationDetails extends WebAuthenticationDetails {
    
        private final String itemId;
    
        public CustomWebAuthenticationDetails(HttpServletRequest request) {
            super(request);
            itemId = request.getParameter("itemId");
        }
    
        public String getItemId() {
            return itemId;
        }
    
        //TODO override hashCode, equals and toString to include itemId
        @Override
        public int hashCode() { /* collapsed */ }
        @Override
        public boolean equals(Object obj) { /* collapsed */ }
        @Override
        public String toString() { /* collapsed */ }
    }
    

    AuthenticationSuccessHandler는 다음 예제와 비슷한 로직을 가져야합니다.

    package com.dd.security;
    
    import org.springframework.security.core.Authentication;
    import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
    import org.springframework.util.StringUtils;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class DDAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
            CustomWebAuthenticationDetails details = (CustomWebAuthenticationDetails) authentication.getDetails();
            if(StringUtils.hasText(details.getItemId())) {
                //TODO sanity and security check for itemId needed
                String redirectUrl = "item/" + details.getItemId();
                response.sendRedirect(redirectUrl);
            }
            throw new IllegalStateException("itemId in authentication details not found");
        }
    }
    

    실제 예제는 여기에서 찾을 수 있습니다.

  2. from https://stackoverflow.com/questions/23926603/how-to-use-custom-filter-with-authentication-success-handler-ref-equivalent-in-s by cc-by-sa and MIT license