¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.lf.server.config.PropertiesConfig; |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.helper.WebHelper; |
| | | import org.apache.http.Header; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicNameValuePair; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * ç¾åæå¡ç±» |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class SignService { |
| | | @Autowired |
| | | PropertiesConfig propertiesConfig; |
| | | |
| | | private final static String SUCCESS = "success"; |
| | | |
| | | /** |
| | | * èªå¨ç»å½Druid |
| | | */ |
| | | public void loginDruid(HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | List<NameValuePair> list = new ArrayList<>(); |
| | | list.add(new BasicNameValuePair("loginUsername", propertiesConfig.getDruidUser())); |
| | | list.add(new BasicNameValuePair("loginPassword", propertiesConfig.getDruidPwd())); |
| | | |
| | | String url = req.getRequestURL().toString().replace("/sign/toDruid", "/druid/submitLogin"); |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | | |
| | | UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(list, StaticData.TEXT_ENCODER); |
| | | HttpPost httpPost = new HttpPost(url); |
| | | httpPost.setEntity(postEntity); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpPost); |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | String rs = EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | | if (!SUCCESS.equals(rs)) { |
| | | return; |
| | | } |
| | | |
| | | Header[] headers = closeResponse.getAllHeaders(); |
| | | for (int i = 0; i < headers.length; i++) { |
| | | Header header = headers[i]; |
| | | if ("Set-Cookie".equals(header.getName())) { |
| | | String val = header.getValue(); |
| | | String sessionId = val.substring(val.indexOf("=") + 1, val.indexOf(";")); |
| | | WebHelper.saveCookie(StaticData.DRUID_COOKIE_KEY, sessionId, res); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |