我正在为它制作简单的REST服务和客户端。我尝试制作某种安全性,所以当你进入/登录网站时我只生成UUID:
@RequestMapping("/login")
public uuid getUUID()
{
temp = new uuid();
return temp;
}
然后在客户端我得到这个UUID。现在我想把这个UUID传递给我的服务" getPerson"看起来像这样:
@RequestMapping("/{userId}/getperson")
public Person getPerson(@PathVariable("userId") int user, uuid uuid)
{
if(System.currentTimeMillis() - uuid.getDate().getTime() < 60000 &&
uuid.getHash().toString().equals(temp.toString()))
return personService.getPerson(user);
else
return null;
}
我想要实现的是通过将其时间戳和字符串与先前创建的临时对象进行比较来简单验证UUID。这是我的问题 - 我不知道如何从客户端传递对象uuid。 我非常成熟的客户端看起来像这样:
RestTemplate restTemplate = new RestTemplate();
uuid myUUID = restTemplate.getForObject("http://localhost:8080/login", uuid.class);
HttpEntity<uuid> requestUUID = new HttpEntity<uuid>(myUUID);
//HttpEntity<Person> request = new HttpEntity<Person>(new Person("John", "Great", 2));
//restTemplate.postForObject("http://localhost:8080/addperson", request, Person.class);
Person person = restTemplate.postForObject("http://localhost:8080/2/getperson", requestUUID, Person.class);
我不知道这种类型的验证是否安全,但如果您能告诉我如何传递我的对象,那就太棒了。
答案 0 :(得分:0)
我在下面找到了简单的解决方案,
这是我在下面显示的概念
import java.util.UUID;
public class Person {
private UUID uuid;
public Person() {
}
public Person(UUID uuid) {
super();
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
@Override
public String toString() {
return "Person [uuid=" + uuid + "]";
}
}
控制器 IWebExample 接口:
import java.util.UUID;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
public interface IWebExample {
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
UUID getUUID();
@RequestMapping(value = "/{userId}/getPerson", method = RequestMethod.POST)
@ResponseBody
Person getPerson(int userId, UUID uuid);
}
控制器实施
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import com.finvendor.serverwebapi.resources.example.IWebExample;
import com.finvendor.serverwebapi.resources.example.Person;
@Controller
public class WebExampleImpl implements IWebExample {
@Override
public UUID getUUID() {
return UUID.randomUUID();
}
@Override
public Person getPerson(@PathVariable("userId") int userId, @RequestBody UUID uuid) {
return new Person(uuid);
}
}
客户代码:
import java.util.UUID;
import org.springframework.web.client.RestTemplate;
public class ClientMainApp {
public static void main(String[] args) {
RestTemplate template=new RestTemplate();
UUID uuid = template.getForObject("http://localhost:8080/mylogin",UUID.class);
System.out.println("UUID="+uuid);
//here i am passing uuid object (as per your requirement)
Person person = template.postForObject("http://localhost:8080/1/getPerson", uuid, Person.class);
System.out.println(person);
}
}
步骤
创建战争文件
- tomcat中的部署和
- 运行客户端代码
输出为:
UUID = e1e89e96-8118-446c-900D-d123b1b566ea
人[uuid = e1e89e96-8118-446c-900d-d123b1b566ea]
在输出中,您可以观察到我们在客户端代码中的postForObject(...)rest模板方法中传递对象uuid。
问题解决方案的底线是你需要将@RequestBody用于uuid输入参数
希望你有这个想法和解决方案!!