Spring MVC Web请求和同一方法的Android请求

时间:2016-10-14 10:44:24

标签: java android web integration

我有一个spring mvc web应用程序,用户可以在其中以典型方式登录会话“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);

        }

公共类帐户{

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;
}

}

公共类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();


    }

1 个答案:

答案 0 :(得分:1)

您可以要求用户通过自定义身份验证请求(例如/ appLogin)在Android应用启动时发送用户和密码,如果是正确的信用卡,您可以将密钥返回给用户(应用),并将其存储到app中的某个变量跑。然后,当用户想要做某事时,向服务器发送请求,您可以将其发送到具有/ appExampleService映射的函数,然后您可以检查该函数,此密钥和设备有效,具体取决于您如何处理自定义登录过程然后此函数调用现有函数用于具有映射/ exampleService的Web浏览器。例如;

colnames(df1) <- colnames(df2) <- c("attribute", "value")
rbind.data.frame(df1, df2)

userDAO只是我创建的用于获取给定密钥的用户信息的东西。还有一个App登录服务,当用户启动应用程序发送用户名并通过时,该用户将该密钥返回给用户