package com.ndpmedia.rocketmq.authentication;

import com.ndpmedia.rocketmq.cockpit.util.LoginConstant;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Collection;

/**
 * try to save user login session.
 */
public class RocketMQUserLoginSuccessHanlder extends SavedRequestAwareAuthenticationSuccessHandler
        implements LoginConstant
{
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws ServletException, IOException
    {
        HttpSession session = request.getSession();

        Cookie[] cookies = request.getCookies();

        for (Cookie c : cookies)
        {
            System.out.println(c.getName() + " [request.getRemoteHost()] " + c.getValue());
        }

        Cookie users = getCookie(request, LOGIN_PARAMETER_USERNAME, request.getParameter(LOGIN_PARAMETER_USERNAME));
        Cookie pass = getCookie(request, LOGIN_PARAMETER_PASSWORD, request.getParameter(LOGIN_PARAMETER_PASSWORD));

        Collection<? extends GrantedAuthority> c = authentication.getAuthorities();
        StringBuffer grant = new StringBuffer();
        for (GrantedAuthority g : c)
        {
            System.out.println(g.getAuthority());
            grant.append(g.getAuthority());
            grant.append(";");
        }

        Cookie auth = getCookie(request, LOGIN_PARAMETER_AUTHORITY, grant.toString());
        response.addCookie(users);
        response.addCookie(pass);
        response.addCookie(auth);

        super.onAuthenticationSuccess(request, response, authentication);
    }

    public static String getIpAddr(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }
        //当使用本地域名访问时，获取到的IP系特殊IP 0:0:0:0:0:0:0:1
        if (ip.startsWith("0:0:0:0:"))
        {
            ip = "localhost";
        }

        return ip;
    }

    public static Cookie getCookie(HttpServletRequest request, String key, String value)
    {
        Cookie cookie = new Cookie(key, value);
        cookie.setPath("/");
        cookie.setDomain(getIpAddr(request));
        cookie.setHttpOnly(true);
        System.out.println(cookie.toString());
        return cookie;
    }
}

@@ -2094,24 +2094,151 @@
  String ip =
+ getServerIP(request);%0A        if (ip == null %7C%7C ip.length() == 0 %7C%7C %22unknown%22.equalsIgnoreCase(ip))%0A        %7B%0A            ip =
  request.get
@@ -2260,24 +2260,34 @@
 rded-for%22);%0A
+        %7D%0A
         if (
@@ -2880,32 +2880,227 @@
 turn ip;%0A    %7D%0A%0A
+    private static String getServerIP(HttpServletRequest request)%0A    %7B%0A        StringBuffer url = request.getRequestURL();%0A        System.out.println(url);%0A        return url.toString();%0A    %7D%0A%0A
     public stati

debug : server ip and cookie can not get as well .