下面是一个使用Spring Boot框架调用第三方API实现登录获取Cookie并判断Cookie有效时间的示例代码:
@RestController
public class LoginController {
private static final String API_URL = "https://example.com/api/login";
private static final String COOKIE_NAME = "SESSION_ID";
private static final int COOKIE_MAX_AGE = 60 * 60; // 设置Cookie有效时间为1小时
@PostMapping("/login")
public String doLogin(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(API_URL);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", password);
StringEntity entity = new StringEntity(json.toString(), "UTF-8");
httpPost.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(httpResponse.getEntity());
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 登录成功,获取Cookie并设置有效时间
String sessionId = httpResponse.getFirstHeader("Set-Cookie").getValue().split(";")[0];
Cookie cookie = new Cookie(COOKIE_NAME, sessionId);
cookie.setMaxAge(COOKIE_MAX_AGE);
response.addCookie(cookie);
return "redirect:/welcome";
} else {
// 登录失败,返回错误信息
return responseBody;
}
}
@GetMapping("/welcome")
public String welcome(HttpServletRequest request, HttpServletResponse response) throws IOException {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(COOKIE_NAME)) {
String sessionId = cookie.getValue();
// 判断Cookie是否过期
if (cookie.getMaxAge() > 0) {
// 验证Cookie是否有效
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(API_URL + "?sessionId=" + sessionId);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
String responseBody = EntityUtils.toString(httpResponse.getEntity());
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Cookie有效,返回欢迎页面
return "welcome";
} else {
// Cookie无效,重定向到登录页面
return "redirect:/login";
}
}
}
}
}
// 没有找到Cookie或者Cookie已过期,重定向到登录页面
return "redirect:/login";
}
}
代码说明:
@RestController注解表示这是一个REST风格的控制器类,可以处理HTTP请求和响应。
API_URL表示第三方API的URL地址,COOKIE_NAME表示Cookie的名称,COOKIE_MAX_AGE表示Cookie的有效时间,单位为秒。
HttpServletRequest和HttpServletResponse是Spring Boot中的类,用于处理HTTP请求和响应。
@RequestParam注解表示获取请求参数的值,其中username和password是请求参数名。
使用Apache HttpClient库发送POST请求,将用户名和密码包装成JSON格式的字符串,并设置请求头的Content-Type为application/json;charset=UTF-8。
通过httpResponse获取API返回的响应状态码和响应体,如果状态码为200,则登录成功,从响应头中获取Cookie的值,并创建一个名为COOKIE_NAME的Cookie,设置Cookie的有效时间为COOKIE_MAX_AGE,并将Cookie添加到响应中,同时重定向到欢迎页面;否则登录失败,返回错误信息。
在welcome()方法中判断用户是否已经登录,首先获取请求中的Cookie数组,然后遍历数组查找名为COOKIE_NAME的Cookie,如果找到了则获取Cookie的值,并判断Cookie是否过期(即getMaxAge()是否大于0),如果未过期则使用Cookie中的sessionId参数向API发送GET请求,验证Cookie是否有效,如果API返回的状态码为200,则Cookie有效,返回欢迎页面;否则Cookie无效,重定向到登录页面;如果没有找到名为COOKIE_NAME的Cookie则重定向到登录页面。 需要注意的是,这只是一个简单的示例代码,实际应用中需要根据API的返回格式和业务需求进行修改和扩展。另外,同样需要进行密码加密存储和CSRF防护等安全措施。