2012-09-20 3 views
3

사용자 정의 로그 아웃 처리기를 사용하여 게시 로그 아웃 리디렉션을 구현하는 방법이 궁금합니다. 나는 CustomLogoutSuccessHandler을 구현하지만 난 로그인 한 사용자가 이전에 설정 한 액세스 HTTP 세션 데이터 해제 방법이 없습니다. 데이터는 항상 곁에 비어 ...사용자 기반의 동적 게시 후 로그 아웃 리디렉션 URL?

class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler { 

    private static final ThreadLocal<Authentication> AUTH_HOLDER = new ThreadLocal<Authentication>() 

    void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 
     AUTH_HOLDER.set authentication 

     // reading session variable... 
     request.session?.variable // but this is always empty 

     try { 
      super.handle(request, response, authentication) 
     } 
     finally { 
      AUTH_HOLDER.remove() 
     } 
    } 

    @Override 
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) { 
     Authentication auth = AUTH_HOLDER.get() 

     String url = super.determineTargetUrl(request, response) 

     // do something with the url based on session data.. 

     url 
    } 
} 

답변

2

나도 몰라가있는 경우 이 작업을 수행하는 쉬운 방법이 있지만 아래의 솔루션이 필요했습니다.

LogoutSuccessHandler에서 setTargetUrlParameter를 설정하기 만하면됩니다. 이를 위해 현재 요청에 매개 변수를 추가하기 위해 Lincoln Baxter, III 이 작성한 HttpServletRequestWrapper 구현을 사용했습니다. 다음은 관련 코드입니다.

@Component 
public class MyCustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler { 

    @Override 
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, 
      Authentication authentication) throws IOException, ServletException { 
     HttpServletRequest wrappedRequest = request; 

     if (authentication != null) { 
      //do something with the Principal and add the corresponding url 
      Map<String, String[]> extraParams = new TreeMap<String, String[]>(); 
      extraParams.put("targetUrl", new String[] {"/target.xhtml"}); 
      wrappedRequest = new PrettyFacesWrappedRequest(request, extraParams); 
      setTargetUrlParameter("targetUrl"); 
     } 
     setDefaultTargetUrl("/general/main.xhtml"); 
     super.onLogoutSuccess(wrappedRequest, response, authentication);  
    } 
} 

과의 ApplicationContext에 관련된 변화 :

<http> 
    <logout logout-url="/j_spring_security_logout" 
       success-handler-ref="myCustomLogoutSuccessHandler" 
       invalidate-session="true"/> 
</http> 
<beans:bean id="myCustomLogoutSuccessHandler" class="com.examples.MyCustomLogoutSuccessHandler"/> 

public class PrettyFacesWrappedRequest extends HttpServletRequestWrapper 
{ 
    private final Map<String, String[]> modifiableParameters; 
    private Map<String, String[]> allParameters = null; 

    /** 
    * Create a new request wrapper that will merge additional parameters into 
    * the request object without prematurely reading parameters from the 
    * original request. 
    * 
    * @param request 
    * @param additionalParams 
    */ 
    public PrettyFacesWrappedRequest(final HttpServletRequest request, 
                final Map<String, String[]> additionalParams) 
    { 
     super(request); 
     modifiableParameters = new TreeMap<String, String[]>(); 
     modifiableParameters.putAll(additionalParams); 
    } 

    @Override 
    public String getParameter(final String name) 
    { 
     String[] strings = getParameterMap().get(name); 
     if (strings != null) 
     { 
      return strings[0]; 
     } 
     return super.getParameter(name); 
    } 

    @Override 
    public Map<String, String[]> getParameterMap() 
    { 
     if (allParameters == null) 
     { 
      allParameters = new TreeMap<String, String[]>(); 
      allParameters.putAll(super.getParameterMap()); 
      allParameters.putAll(modifiableParameters); 
     } 
     //Return an unmodifiable collection because we need to uphold the interface contract. 
     return Collections.unmodifiableMap(allParameters); 
    } 

    @Override 
    public Enumeration<String> getParameterNames() 
    { 
     return Collections.enumeration(getParameterMap().keySet()); 
    } 

    @Override 
    public String[] getParameterValues(final String name) 
    { 
     return getParameterMap().get(name); 
    } 
} 

는 다음 CustomLogoutSuccessHandler에서,이 같은 매개 변수로이에서 targetUrl 추가

관련 문제