Java 如何从spring Hibernate获取sessionid
我正在用Java构建RESTWebServices,以便其他客户端可以使用它 现在我正试图在webservice中实现会话管理,尽管我在google中搜索过,但我还不知道如何在下面的代码中实现会话 RestController.javaJava 如何从spring Hibernate获取sessionid,java,spring,hibernate,web-services,Java,Spring,Hibernate,Web Services,我正在用Java构建RESTWebServices,以便其他客户端可以使用它 现在我正试图在webservice中实现会话管理,尽管我在google中搜索过,但我还不知道如何在下面的代码中实现会话 RestController.java @Controller @RequestMapping("/employee") public class RestController { @Autowired DataServices dataServices; static final Logger
@Controller
@RequestMapping("/employee")
public class RestController {
@Autowired
DataServices dataServices;
static final Logger logger = Logger.getLogger(RestController.class);
/* Submit form in Spring Restful Services */
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status addEmployee(@RequestBody Employee employee) {
try {
dataServices.addEntity(employee);
return new Status(1, "Employee added Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
}
}
/* Ger a single objct in Json form in Spring Rest Services */
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody Employee getEmployee(@PathVariable("id") long id) {
Employee employee = null;
try {
employee = dataServices.getEntityById(id);
} catch (Exception e) {
e.printStackTrace();
}
return employee;
}
/* Getting List of objects in Json format in Spring Restful Services */
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody List getEmployee() {
List employeeList = null;
try {
employeeList = dataServices.getEntityList();
} catch (Exception e) {
e.printStackTrace();
}
return employeeList;
}
/* Delete an object from DB in Spring Restful Services */
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
public @ResponseBody Status deleteEmployee(@PathVariable("id") long id) {
try {
dataServices.deleteEntity(id);
return new Status(1, "Employee deleted Successfully !");
} catch (Exception e) {
return new Status(0, e.toString());
}
}
@RequestMapping(value = "/save/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status saveUser(@PathVariable("id") long id, @RequestBody Employee employee) {
Employee employeeupdate = null;
try {
employeeupdate = dataServices.getEntityById(id);
employeeupdate.setId(employee.getId());
employeeupdate.setFirstName(employee.getFirstName());
employeeupdate.setLastName(employee.getLastName());
employeeupdate.setEmail(employee.getEmail());
employeeupdate.setPhone(employee.getPhone());
dataServices.updateEntity(employeeupdate);
return new Status(1, "Employee updated Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
}
}}
DataDaoImpl.java
public class DataDaoImpl implements DataDao {
@Autowired
SessionFactory sessionFactory;
Session session = null;
Transaction tx = null;
@Override
public boolean addEntity(Employee employee) throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(employee);
tx.commit();
session.close();
return false;
}
@Override
public Employee getEntityById(long id) throws Exception {
session = sessionFactory.openSession();
Employee employee = (Employee) session.load(Employee.class, new Long(id));
tx = session.getTransaction();
session.beginTransaction();
tx.commit();
return employee;
}
@SuppressWarnings("unchecked")
@Override
public List getEntityList() throws Exception {
session = sessionFactory.openSession();
tx = session.beginTransaction();
List employeeList = session.createCriteria(Employee.class).list();
tx.commit();
session.close();
return employeeList;
}
@Override
public boolean deleteEntity(long id) throws Exception {
session = sessionFactory.openSession();
Object o = session.load(Employee.class, id);
tx = session.getTransaction();
session.beginTransaction();
session.delete(o);
tx.commit();
return false;
}
@Override
public boolean updateEntity(Employee employeeupdate) throws Exception {
// session = sessionFactory.openSession();
tx = session.beginTransaction();
session.update(employeeupdate);
tx.commit();
session.close();
return false;
}}
我的问题是:
任何建议和帮助都将不胜感激。在身份验证时使用java UUID生成密钥,您可以使用Google Guava缓存或数据库存储密钥,并在每次后续请求中验证密钥。 步骤如下- 1-客户端使用用户名+密码登录 2-服务器验证并返回uuid/令牌,并将令牌与用户ID一起存储在缓存中 3-客户提出请求 4-服务器查看请求中的令牌 5-服务器查找令牌并确定令牌是否过期或不存在 6-如果令牌过期或不存在,服务器将返回错误
7-如果令牌未过期,服务器将在响应中返回数据。您如何验证用户?@anand mishra我只是在web服务上进行示例。好像现在我计划通过比较数据库中的数据来验证用户。如果我可以使用spring身份验证,我想检查一下,如果你有任何建议或链接,这将对我有帮助。使用java UUID在身份验证时生成会话密钥,并将其存储在Google Guava缓存中,并在后续的每个请求中验证它。