当前已知URL地址为“www.test.com”。 token生成方式为该代码段: private String buildToken( String currentTeamMemberName,String userId) { Map<String, String> kv = new LinkedHashMap<>(); kv.put("userId", userId); kv.put("currentTeamMemberName",currentTeamMemberName); kv.put("salt", "salt"); String signature = Sha1Crypto.encode(JsonHelper.getInstance().write(kv)); kv.remove("salt"); kv.put("signature", signature); kv.put("ts", System.currentTimeMillis()+""); String offset = Configuration.getInstance().getProperty("indicatorPlatformOffset"); if(offset == null || offset.trim().length() == 0){ offset = 1000 * 60 * 5 + ""; } kv.put("offset", offset); String token = JsonHelper.getInstance().write(kv);//Map转JSON String base64Token = Base64Codec.encode(token);//base64编码 return base64Token; } 需要编写一个接口,已知参数为userId(单点账户),currentTeamMemberName(账号所属团队的成员名称),offset(偏移量)值为300000; 接口将生成的token添加到cookie中,请求已知的URL地址,供其进行校验。 其地址的校验方法为: private boolean checkRefererTokenStr(String tokenStr) { try{ Map token = JsonHelper.getInstance().read(Base64Codec.decode(tokenStr), Map.class); String userId = (String) token.get("userId"); Map<String, String> signChecker = new LinkedHashMap<>(); signChecker.put("userId", userId); signChecker.put(CURRENT_TEAM_MEMBER_NAME, token.get(CURRENT_TEAM_MEMBER_NAME).toString()); signChecker.put("salt", "salt"); String sign = Sha1Crypto.encode(JsonHelper.getInstance().write(signChecker)); return sign.equals(token.get("signature")); }catch (Exception e){ log.error("验证单点集成页面请求referer失败!", e); } return false; } 请编写接口实现该需求
时间: 2024-04-25 13:27:31 浏览: 90
以下是一个简单的Java实现,可以作为一个Servlet或者Spring MVC Controller来使用:
```java
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TokenController {
private static final String CURRENT_TEAM_MEMBER_NAME = "currentTeamMemberName";
@RequestMapping("/generateToken")
public String generateToken(HttpServletRequest request, HttpServletResponse response,
@RequestParam("userId") String userId,
@RequestParam("currentTeamMemberName") String currentTeamMemberName,
@RequestParam(value = "offset", defaultValue = "300000") long offset) {
Map<String, String> kv = new LinkedHashMap<>();
kv.put("userId", userId);
kv.put(CURRENT_TEAM_MEMBER_NAME, currentTeamMemberName);
kv.put("salt", "salt");
String signature = Sha1Crypto.encode(JsonHelper.getInstance().write(kv));
kv.remove("salt");
kv.put("signature", signature);
kv.put("ts", System.currentTimeMillis() + "");
kv.put("offset", offset + "");
String token = JsonHelper.getInstance().write(kv);
String base64Token = Base64Codec.encode(token);
Cookie cookie = new Cookie("token", base64Token);
cookie.setPath("/");
response.addCookie(cookie);
return "Token generated successfully!";
}
@RequestMapping("/validateToken")
public String validateToken(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("token")) {
String base64Token = cookie.getValue();
String token = Base64Codec.decode(base64Token);
if (checkRefererTokenStr(token)) {
return "Token is valid!";
} else {
return "Token is invalid!";
}
}
}
}
return "Token not found!";
}
private boolean checkRefererTokenStr(String tokenStr) {
try {
Map<String, Object> token = JsonHelper.getInstance().read(Base64Codec.decode(tokenStr), Map.class);
String userId = (String) token.get("userId");
Map<String, String> signChecker = new LinkedHashMap<>();
signChecker.put("userId", userId);
signChecker.put(CURRENT_TEAM_MEMBER_NAME, token.get(CURRENT_TEAM_MEMBER_NAME).toString());
signChecker.put("salt", "salt");
String sign = Sha1Crypto.encode(JsonHelper.getInstance().write(signChecker));
return sign.equals(token.get("signature"));
} catch (Exception e) {
log.error("验证单点集成页面请求referer失败!", e);
}
return false;
}
}
```
这里使用了Spring MVC的注解来定义了两个接口:`/generateToken` 和 `/validateToken`,其中 `/generateToken` 接受三个参数:`userId`、`currentTeamMemberName` 和 `offset`,并生成一个token添加到Cookie中;`/validateToken` 从Cookie中读取token,并验证其有效性。
为了简化代码,这里使用了一些第三方库,你需要在项目中添加这些库的依赖:
- org.springframework:spring-web
- com.fasterxml.jackson.core:jackson-core
- com.fasterxml.jackson.core:jackson-databind
- org.apache.commons:commons-lang3
阅读全文