I have successfully created a Spring RESTful web service with various APIs. Now I have to protect them from unauthorized access. I followed http://www.beingjavaguys.com/2014/10/spring-security-oauth2-integration.html and the login logic is completely different from mine. Can someone help me move on?
Receive user login request
@RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseBody @ResponseStatus(HttpStatus.OK) public UserResponse login(@RequestBody final UserLoginRequest userRequest) throws ServletException, IOException { UserResponse userResponse = new UserResponse(); try { userResponse = accessService.login(userRequest); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return userResponse; }
Request user login request
@Transactional public UserResponse login(UserLoginRequest userRequest) throws SQLException, ClassNotFoundException, IOException { UserResponse userResponse = new UserResponse(); int status = 0; //boolean isExist = loginDao.isUserExist(userRequest.getUsername(), userRequest.getPassword()); User user = loginDao.getUser(userRequest.getEmailID()); if (user != null) { if (userRequest.getPassword().equals(user.getPassword())) {//Case sensitive password and added to check status //User exist if (user.getStatus().equals("1")) { //Device token check loginDao.isDeviceTokenExists(userRequest, user.getProfileId()); status = 2; } else { status = 3; } } else { status = 4; } } else { status = 1; } if (status == 1) { userResponse.setCode(WeekenterConstants.USER_EMAIL_EXIST_CODE); userResponse.setMessage("User does not exists.Please Register."); } else if (status == 2) { userResponse.setCode(WeekenterConstants.SUCCESS_CODE); userResponse.setMessage("User login success"); userResponse.setId(user.getProfileId()); } else if (status == 3) { userResponse.setCode(WeekenterConstants.FAILURE_CODE); userResponse.setMessage("Your Account is blocked. Please contact Weekenter administrator."); userResponse.setId(user.getProfileId()); } else if (status == 4) { userResponse.setCode(WeekenterConstants.FAILURE_CODE); userResponse.setMessage("Password is wrong."); userResponse.setId(user.getProfileId()); } return userResponse; }
I have an API for sample countries, a list of users, etc. These services should only provide data to the Android client after the user is valid. I know that authentication will be processed using an access token. How can I do this in a standard way?
source share