//package com.lutao.cloud.auth.sso.config;
//
//import cn.hutool.core.util.CharsetUtil;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.lutao.cloud.auth.sso.client.SSOHttpClient;
//import com.lutao.cloud.auth.sso.domain.ServiceResponse;
//import com.lutao.cloud.auth.sso.util.HashUtil;
//import com.lutao.cloud.common.core.constant.CommonConstants;
//import com.lutao.cloud.common.core.util.MessageInfo;
//import com.lutao.cloud.common.core.util.StringUtil;
//import com.lutao.cloud.common.security.sso.SSOAuthenticationToken;
//import lombok.Setter;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.MediaType;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.web.AuthenticationEntryPoint;
//import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.PrintWriter;
//
///**
// * 登录验证filter
// */
//@Slf4j
//public class SSOAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
//
//	@Setter
//	private AuthenticationEntryPoint authenticationEntryPoint;
//
//	@Setter
//	private SSOHttpClient ssoHttpClient;
//
//	@Setter
//	private ObjectMapper objectMapper;
//
//	@Setter
//	private String loginUrl;
//
//	public SSOAuthenticationFilter() {
//		super(new AntPathRequestMatcher("/sso/login"));
//	}
//
//	@Override
//	@SneakyThrows
//	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
//		String ticket = request.getParameter("ticket");
//
//		if(StringUtil.isBlank(ticket)) {
//			response("ticket is empty", response);
//			return null;
//		}
//
//		// 获取用户信息
//		ServiceResponse serviceResponse = ssoHttpClient.serviceValidate(loginUrl, ticket);
//		log.error("serviceResponse:{}", serviceResponse);
//
//		if(StringUtil.isNoneBlank(serviceResponse.getAuthenticationFailure())) {
//			response(serviceResponse.getAuthenticationFailure(), response);
//            return null;
//		}
//
//		String userName = serviceResponse.getAuthenticationSuccess().getAttributes().getIdCardUUID();
//		SSOAuthenticationToken ssoAuthenticationToken = new SSOAuthenticationToken(userName);
//		ssoAuthenticationToken.setTicket(ticket);
//
//		setDetails(request, ssoAuthenticationToken);
//
//		Authentication authResult = null;
//		try {
//			authResult = this.getAuthenticationManager().authenticate(ssoAuthenticationToken);
//
//			logger.debug("Authentication success: " + authResult);
//			SecurityContextHolder.getContext().setAuthentication(authResult);
//		} catch (Exception failed) {
//			SecurityContextHolder.clearContext();
//			logger.debug("Authentication request failed: " + failed);
//
//			try {
//				authenticationEntryPoint.commence(request, response,
//						new UsernameNotFoundException(failed.getMessage(), failed));
//			} catch (Exception e) {
//				logger.error("authenticationEntryPoint handle error:{}", failed);
//			}
//		}
//
//		return authResult;
//	}
//
//	private void setDetails(HttpServletRequest request, SSOAuthenticationToken authRequest) {
//		authRequest.setDetails(authenticationDetailsSource.buildDetails(request));
//	}
//
//	private void response(String message, HttpServletResponse response) throws Exception {
//		MessageInfo<String> result = new MessageInfo<>();
//		result.setMsg(message);
//		result.setData(message);
//		result.setCode(CommonConstants.FAIL);
//
//		response.setCharacterEncoding(CharsetUtil.UTF_8);
//		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//		PrintWriter printWriter = response.getWriter();
//		printWriter.append(objectMapper.writeValueAsString(result));
//	}
//}