|  | @@ -1,34 +1,31 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.auth.handler;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import java.io.IOException;
 | 
	
		
			
				|  |  | -import java.io.PrintWriter;
 | 
	
		
			
				|  |  | -import java.util.Base64;
 | 
	
		
			
				|  |  | -import java.util.HashMap;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import javax.servlet.ServletException;
 | 
	
		
			
				|  |  | -import javax.servlet.http.HttpServletRequest;
 | 
	
		
			
				|  |  | -import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +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 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.ClientDetails;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.ClientDetailsService;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.OAuth2Authentication;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.OAuth2Request;
 | 
	
		
			
				|  |  | -import org.springframework.security.oauth2.provider.TokenRequest;
 | 
	
		
			
				|  |  | +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 com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | -import com.ym.mec.common.constant.CommonConstants;
 | 
	
		
			
				|  |  | -import com.ym.mec.common.entity.HttpResponseResult;
 | 
	
		
			
				|  |  | +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);
 | 
	
	
		
			
				|  | @@ -46,35 +43,29 @@ public class BaseAuthenticationSuccessEventHandler extends SavedRequestAwareAuth
 | 
	
		
			
				|  |  |  	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException,
 | 
	
		
			
				|  |  |  			IOException {
 | 
	
		
			
				|  |  |  		logger.info("用户:{} 登录成功", authentication.getPrincipal());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		String header = request.getHeader("Authorization");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		if (header == null || !header.startsWith(CommonConstants.BASIC)) {
 | 
	
		
			
				|  |  | -			throw new UnapprovedClientAuthenticationException("请求头中client信息为空");
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  		try {
 | 
	
		
			
				|  |  | -			String[] tokens = extractAndDecodeHeader(header);
 | 
	
		
			
				|  |  | -			String clientId = tokens[0];
 | 
	
		
			
				|  |  | -			// String clientSecret = tokens[1];
 | 
	
		
			
				|  |  | +			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(new HashMap<String, String>(), clientId, clientDetails.getScope(), "password");
 | 
	
		
			
				|  |  | +			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.setCharacterEncoding("utf-8");
 | 
	
		
			
				|  |  |  			response.setContentType("application/json; charset=utf-8");
 | 
	
		
			
				|  |  | -			PrintWriter printWriter = response.getWriter();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  			HttpResponseResult result = new HttpResponseResult(true, HttpStatus.SC_OK, oAuth2AccessToken, "");
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			printWriter.append(objectMapper.writeValueAsString(result));
 | 
	
		
			
				|  |  | -			printWriter.flush();
 | 
	
		
			
				|  |  | -			printWriter.close();
 | 
	
		
			
				|  |  | +			response.getWriter().write(objectMapper.writeValueAsString(result));
 | 
	
		
			
				|  |  |  		} catch (IOException e) {
 | 
	
		
			
				|  |  |  			throw new BadCredentialsException("Failed to decode basic authentication token");
 | 
	
		
			
				|  |  |  		}
 |