|  | @@ -1,99 +1,26 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.auth.core.handler;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | -import com.ym.mec.common.entity.HttpResponseResult;
 | 
	
		
			
				|  |  | -import org.apache.commons.collections.MapUtils;
 | 
	
		
			
				|  |  | -import org.apache.http.HttpStatus;
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import javax.servlet.ServletException;
 | 
	
		
			
				|  |  | +import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | +import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | -import org.springframework.http.HttpHeaders;
 | 
	
		
			
				|  |  | -import org.springframework.http.MediaType;
 | 
	
		
			
				|  |  | -import org.springframework.security.authentication.BadCredentialsException;
 | 
	
		
			
				|  |  |  import org.springframework.security.core.Authentication;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.common.OAuth2AccessToken;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.*;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
 | 
	
		
			
				|  |  |  import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | -import org.springframework.web.client.RestTemplate;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import javax.servlet.ServletException;
 | 
	
		
			
				|  |  | -import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | -import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | -import java.io.IOException;
 | 
	
		
			
				|  |  | -import java.util.Base64;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Component
 | 
	
		
			
				|  |  |  public class BaseAuthenticationSuccessEventHandler extends SavedRequestAwareAuthenticationSuccessHandler {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	private final static Logger logger = LoggerFactory.getLogger(BaseAuthenticationSuccessEventHandler.class);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	@Autowired
 | 
	
		
			
				|  |  | -	private ClientDetailsService clientDetailsService;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	@Autowired
 | 
	
		
			
				|  |  | -	private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	@Autowired
 | 
	
		
			
				|  |  | -	private ObjectMapper objectMapper;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  	@Override
 | 
	
		
			
				|  |  |  	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException,
 | 
	
		
			
				|  |  |  			IOException {
 | 
	
		
			
				|  |  |  		logger.info("用户:{} 登录成功", authentication.getPrincipal());
 | 
	
		
			
				|  |  | -		/*try {
 | 
	
		
			
				|  |  | -			String clientId = request.getParameter("clientId");
 | 
	
		
			
				|  |  | -			String clientSecret = request.getParameter("clientSecret");
 | 
	
		
			
				|  |  | -			if (clientId == null || clientSecret == null) {
 | 
	
		
			
				|  |  | -				throw new UnapprovedClientAuthenticationException("请求头中client信息为空");
 | 
	
		
			
				|  |  | -			}
 | 
	
		
			
				|  |  | -			String base64ClientCredentials = Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes());
 | 
	
		
			
				|  |  | -			HttpHeaders headers = new HttpHeaders();
 | 
	
		
			
				|  |  | -			headers.add("Authorization", "Basic " + base64ClientCredentials);
 | 
	
		
			
				|  |  | -			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
 | 
	
		
			
				|  |  | -			TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "password");
 | 
	
		
			
				|  |  | -			OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
 | 
	
		
			
				|  |  | -			OAuth2AccessToken oAuth2AccessToken = defaultAuthorizationServerTokenServices.createAccessToken(oAuth2Authentication);
 | 
	
		
			
				|  |  | -			logger.info("获取token 成功:{}", oAuth2AccessToken.getValue());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			response.setContentType("application/json; charset=utf-8");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			HttpResponseResult result = new HttpResponseResult(true, HttpStatus.SC_OK, oAuth2AccessToken, "");
 | 
	
		
			
				|  |  | -			response.getWriter().write(objectMapper.writeValueAsString(result));
 | 
	
		
			
				|  |  | -		} catch (IOException e) {
 | 
	
		
			
				|  |  | -			throw new BadCredentialsException("Failed to decode basic authentication token");
 | 
	
		
			
				|  |  | -		}*/
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	/**
 | 
	
		
			
				|  |  | -	 * Decodes the header into a username and password.
 | 
	
		
			
				|  |  | -	 *
 | 
	
		
			
				|  |  | -	 * @throws BadCredentialsException if the Basic header is not present or is not valid
 | 
	
		
			
				|  |  | -	 *                                 Base64
 | 
	
		
			
				|  |  | -	 */
 | 
	
		
			
				|  |  | -	private String[] extractAndDecodeHeader(String header) throws IOException {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		byte[] base64Token = header.substring(6).getBytes("UTF-8");
 | 
	
		
			
				|  |  | -		byte[] decoded;
 | 
	
		
			
				|  |  | -		try {
 | 
	
		
			
				|  |  | -			decoded = Base64.getDecoder().decode(base64Token);
 | 
	
		
			
				|  |  | -		} catch (IllegalArgumentException e) {
 | 
	
		
			
				|  |  | -			throw new BadCredentialsException("Failed to decode basic authentication token");
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		String token = new String(decoded, "utf-8");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		int delim = token.indexOf(":");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		if (delim == -1) {
 | 
	
		
			
				|  |  | -			throw new BadCredentialsException("Invalid basic authentication token");
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		return new String[] { token.substring(0, delim), token.substring(delim + 1) };
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  |  }
 |