PageRenderTime 4896ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/211AuthcenterProject/authcenter-proxy/src/main/java/com/fdhay/authcenter/proxy/service/impl/UserServiceImpl.java

http://java-hiking.googlecode.com/
Java | 74 lines | 65 code | 9 blank | 0 comment | 3 complexity | d156d8a354458813d7abcdd1c0e1577b MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. package com.fdhay.authcenter.proxy.service.impl;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.logging.Log;
  5. import org.apache.commons.logging.LogFactory;
  6. import org.springframework.stereotype.Service;
  7. import com.alibaba.fastjson.JSON;
  8. import com.fdhay.authcenter.proxy.domain.User;
  9. import com.fdhay.authcenter.proxy.domain.base.json.JsonException;
  10. import com.fdhay.authcenter.proxy.domain.base.json.JsonMessage;
  11. import com.fdhay.authcenter.proxy.domain.enums.JsonMessageResultStatusEnum;
  12. import com.fdhay.authcenter.proxy.service.UserService;
  13. import com.fdhay.authcenter.proxy.util.Constants;
  14. import com.fdhay.authcenter.proxy.util.HttpClientUtils;
  15. @Service(value="userService")
  16. public class UserServiceImpl implements UserService {
  17. private Log logger = LogFactory.getLog(getClass());
  18. private static String URL_API_OF_IS_VALID_USER = Constants.cfg.getValue("url.api.user.isValidUser", "url.api.user.isValidUser");
  19. private static String URL_API_OF_IS_VALID_USER_TOKEN = Constants.cfg.getValue("url.api.user.isValidUser.token", "url.api.user.isValidUser.token");
  20. private static String URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID = Constants.cfg.getValue("url.api.user.getLoginUserIfCookieValid", "url.api.user.getLoginUserIfCookieValid");
  21. private static String URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID_TOKEN = Constants.cfg.getValue("url.api.user.getLoginUserIfCookieValid.token", "url.api.user.getLoginUserIfCookieValid.token");
  22. @Override
  23. public User getLoginUserIfCookieValid(String cookie){
  24. User user = null;
  25. try {
  26. Map<String, String> params = new HashMap<String, String>(2);
  27. params.put("cookie", cookie);
  28. params.put("token", URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID_TOKEN);
  29. String response = HttpClientUtils.post(URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID, params, null);
  30. JsonMessage result = JSON.parseObject(response, JsonMessage.class);
  31. if(JsonMessageResultStatusEnum.FAIL.equals(result.getResultStatusCode())){
  32. throw new JsonException(JsonMessageResultStatusEnum.FAIL, response);
  33. }
  34. user = JSON.parseObject(result.getResult(), User.class);
  35. } catch (Exception e) {
  36. logger.error(e);
  37. }
  38. return user;
  39. }
  40. @Override
  41. public boolean isValidUser(String name, String password) {
  42. boolean isValid = true;
  43. try {
  44. Map<String, String> params = new HashMap<String, String>(3);
  45. params.put("name", name);
  46. params.put("password", password);
  47. params.put("token", URL_API_OF_IS_VALID_USER_TOKEN);
  48. String response = HttpClientUtils.post(URL_API_OF_IS_VALID_USER, params, null);
  49. JsonMessage result = JSON.parseObject(response, JsonMessage.class);
  50. if(JsonMessageResultStatusEnum.FAIL.equals(result.getResultStatusCode())){
  51. throw new JsonException(JsonMessageResultStatusEnum.FAIL, response);
  52. }
  53. } catch (Exception e) {
  54. logger.error(e);
  55. isValid = false;
  56. }
  57. if(logger.isDebugEnabled()){
  58. logger.debug("--> try verify with [name=" + name + ", password=***" + "] is valid user ? " + isValid);
  59. }
  60. return isValid;
  61. }
  62. public static void main(String[] args){
  63. new UserServiceImpl().isValidUser("admin", "admin");
  64. }
  65. }