我有一个 spring mvc web 应用程序,其中用户以经典方式登录到 session “session.setAttribute”。每当我需要登录用户数据时,我都会使用这些数据。
现在我想添加 android 应用程序,我想了解我是否必须为每个 android 请求添加额外的方法并在其中发送用户数据? 或者是否可以向相同的方法发出请求。
这种云应用的概念是什么?我必须为 android 请求编写不同的方法吗?因为在我们发起android请求的时候是不可能session.getAttribute的,所以返回null。
User user = userService.getByUserNameAndPassword(userName, password);
if (user != null) {
if (user.isActive()) {
Account account = new Account(user, request.getRemoteAddr());
HttpSession httpSession = request.getSession(true);
AccountRegistry.add(httpSession);
httpSession.setAttribute(Constant.ACCOUNT, account);
result.put(Constant.REF, Constant.SUCCESS);
}
公共(public)类帐户{
private UserRightsHandler userRightsService = null;
private User user;
private String ipAddress;
private boolean admin;
public Account(User user, String ipAddress) {
this.user = user;
this.ipAddress = ipAddress;
userRightsService = new UserRightsHandler(user);
setAdmin(userRightsService.isAdmin());
}
public UserRightsHandler getUserRightsService() {
return userRightsService;
}
public User getUser() {
return this.user;
}
public String getIpAddress() {
return ipAddress;
}
public boolean isAdmin() {
return admin;
}
private void setAdmin(boolean admin) {
this.admin = admin;
}
公共(public)类 AccountRegistry {
private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();
public static void add(HttpSession session) {
sessions.put(session.getId(), session);
}
public static void remove(HttpSession session) {
if (session != null) {
sessions.remove(session.getId());
session.setAttribute(Constant.ACCOUNT, null);
session.invalidate();
}
}
public static HttpSession getByHttpSessionID(String httpSessionID) {
Set<String> keys = sessions.keySet();
Iterator it = keys.iterator();
while (it.hasNext()) {
String sID = (String) it.next();
HttpSession session = sessions.get(sID);
if (sID.equals(httpSessionID)) {
return session;
}
}
return null;
}
public static void removeByHttpSessionID(String httpSessionID) {
HttpSession session = getByHttpSessionID(httpSessionID);
remove(session);
}
public static Account getCurrentAccount() {
HttpServletRequest request = ContextFilter.getCurrentInstance().getRequest();
HttpSession session = request.getSession();
return (Account) session.getAttribute(Constant.ACCOUNT);
}
@RequestMapping(value = "/changeStatus", method = RequestMethod.POST) 公共(public)@ResponseBody String changeStatus(HttpServletRequest 请求,HttpServletResponse 响应)抛出 UnsupportedEncodingException {
User editor = AccountRegistry.getCurrentAccount().getUser();
}
最佳答案
您可以要求用户在 Android 应用程序开始时通过自定义身份验证请求(例如/appLogin)发送他们的用户名和密码,然后如果它是正确的信用记录,您可以将 key 返回给用户(应用程序)并在应用程序期间将其存储到某个变量运行。然后,当用户想要做某事时,向服务器发送请求,您可以将其发送到具有/appExampleService 之类映射的函数,然后您可以在该函数中检查此 key 和设备是否有效,具体取决于您处理自定义登录过程的方式,然后此函数调用现有函数用于具有映射/exampleService 的 Web 浏览器。例如;
@JsonSerialize
@RequestMapping("/appExampleService")
public int someServiceForAppClient(
@RequestParam(value = "key", required = true) String apikey,
@RequestParam(value = "param", required = true) String someParam{
String name=userDAO.getUsernameFromApiKey(apikey);
return someService(someParam, name);
}
@JsonSerialize
@RequestMapping("/exampleService")
public int someServiceForWebClient(
@RequestParam(value = "param", required = true) String someParam) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName();
return someService(someParam, name);
}
public int someService(String someParam,String name){
return doBusiness(someParam, name);
}
userDAO 只是我为获取具有给定 key 的用户信息而创建的东西。还有一个应用程序登录服务,当用户启动应用程序时,它会将该 key 返回给用户发送他的用户名并通过
关于java - Spring MVC Web 请求和 Android 请求相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041255/