package cn.autoform.fw.config; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.changhong.passport.client.handler.AppClientLoginHandler; import com.changhong.passport.client.handler.AppSessionHandler; import com.changhong.passport.core.model.EncryCredentialInfo; import cn.autoform.bean.system.PrivateRestConst; import cn.autoform.bean.system.UserInfo; import cn.autoform.fw.config.login.LoginFilterRegistrationBean; import cn.autoform.util.tool.JSONTool; import lombok.extern.slf4j.Slf4j; @Configuration @Slf4j public class LoginConfig { @Value("${ssoServerHost}") private String soServerHost; @Value("${ssoAppId}") private String ssoAppId; @Bean public FilterRegistrationBean getFilter(){ FilterRegistrationBean fr = new FilterRegistrationBean(); fr.setFilter(new LoginFilterRegistrationBean()); fr.setUrlPatterns(Arrays.asList("/login")); Map param = new HashMap<>(); param.put("ssoServerHost", soServerHost); param.put("ssoClientAppId", ssoAppId); param.put("appClientLoginHandlerClass", ClientLoginHandlerImpl.class.getName()); param.put("appSessionHandlerClass", AppSessionHandlerImpl.class.getName()); fr.setInitParameters(param); return fr; } public static class AppSessionHandlerImpl implements AppSessionHandler{ @Override public boolean contains(String arg0) { return false; } @Override public EncryCredentialInfo get(String arg0) { return null; } @Override public void remove(String arg0) { } @Override public void set(String arg0, EncryCredentialInfo arg1) { System.err.println(); } } /** * 登录 * @author WangYX * */ public static class ClientLoginHandlerImpl implements AppClientLoginHandler, PrivateRestConst{ @Override public void loginClient(EncryCredentialInfo user, HttpServletRequest request, HttpServletResponse response) { UserInfo.LogInInfo loginInfo = new UserInfo.LogInInfo(); loginInfo.setOpenId(user.getOpenId()); loginInfo.setToken(user.getToken()); Cookie cookieToken = new Cookie(OPEN_ID,user.getOpenId()); Cookie cookieOpenId = new Cookie(TOKEN, user.getToken()); cookieToken.setPath("/"); cookieToken.setMaxAge(-1); cookieOpenId.setPath("/"); cookieOpenId.setMaxAge(-1); System.out.println("用户对象"+JSONTool.toJson(loginInfo)); /* * 添加登录信息 */ response.addCookie(cookieToken); response.addCookie(cookieOpenId); System.out.println("cookie:"+JSONTool.toJson(request.getCookies())); } } }