3
votes

I have an spring mvc web application in which users login to session "session.setAttribute" classically. Whenever I need loggedin user data I use this data.

Now I want to add android app and what I want to learn do I have to add additional methods for each android request and send user data within it? Or Is there away to make a request to same methods.

What is the consept for this kind of cloud apps? Do I have to write different methods for android requests? Because it is not possible session.getAttribute when wemake an android request, it returns 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 class Account {

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 class 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 request, HttpServletResponse response) throws UnsupportedEncodingException {

        User editor = AccountRegistry.getCurrentAccount().getUser();


    }
1
@Please shere your code - Jay Prakash
Edit question with your code - Parth Solanki

1 Answers

1
votes

You can ask user send their user and password at the start of Android app via custom authenticate request like /appLogin then if it is correct creditentals you can return a key to user (to app) and store it to some variable during app run. Then when user want to do something send a request to server you can send it to a function with mapping like /appExampleService then you can check at that function this key and device valid depending on how you handle custom login process then this function call existing function that is used for web browsers that have mapping /exampleService. For example;

@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 is just something I created for to get info of user with given key. And there is a service for App login as well which return that key to user when he started the app send his username and pass