/211AuthcenterProject/authcenter-proxy/src/main/java/com/fdhay/authcenter/proxy/service/impl/UserServiceImpl.java
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
- package com.fdhay.authcenter.proxy.service.impl;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.stereotype.Service;
-
- import com.alibaba.fastjson.JSON;
- import com.fdhay.authcenter.proxy.domain.User;
- import com.fdhay.authcenter.proxy.domain.base.json.JsonException;
- import com.fdhay.authcenter.proxy.domain.base.json.JsonMessage;
- import com.fdhay.authcenter.proxy.domain.enums.JsonMessageResultStatusEnum;
- import com.fdhay.authcenter.proxy.service.UserService;
- import com.fdhay.authcenter.proxy.util.Constants;
- import com.fdhay.authcenter.proxy.util.HttpClientUtils;
-
- @Service(value="userService")
- public class UserServiceImpl implements UserService {
- private Log logger = LogFactory.getLog(getClass());
- private static String URL_API_OF_IS_VALID_USER = Constants.cfg.getValue("url.api.user.isValidUser", "url.api.user.isValidUser");
- private static String URL_API_OF_IS_VALID_USER_TOKEN = Constants.cfg.getValue("url.api.user.isValidUser.token", "url.api.user.isValidUser.token");
- private static String URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID = Constants.cfg.getValue("url.api.user.getLoginUserIfCookieValid", "url.api.user.getLoginUserIfCookieValid");
- 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");
-
-
- @Override
- public User getLoginUserIfCookieValid(String cookie){
- User user = null;
- try {
- Map<String, String> params = new HashMap<String, String>(2);
- params.put("cookie", cookie);
- params.put("token", URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID_TOKEN);
- String response = HttpClientUtils.post(URL_API_OF_GET_LOGIN_USER_IF_COOKIE_VALID, params, null);
- JsonMessage result = JSON.parseObject(response, JsonMessage.class);
- if(JsonMessageResultStatusEnum.FAIL.equals(result.getResultStatusCode())){
- throw new JsonException(JsonMessageResultStatusEnum.FAIL, response);
- }
- user = JSON.parseObject(result.getResult(), User.class);
- } catch (Exception e) {
- logger.error(e);
- }
- return user;
- }
-
- @Override
- public boolean isValidUser(String name, String password) {
- boolean isValid = true;
- try {
- Map<String, String> params = new HashMap<String, String>(3);
- params.put("name", name);
- params.put("password", password);
- params.put("token", URL_API_OF_IS_VALID_USER_TOKEN);
- String response = HttpClientUtils.post(URL_API_OF_IS_VALID_USER, params, null);
- JsonMessage result = JSON.parseObject(response, JsonMessage.class);
- if(JsonMessageResultStatusEnum.FAIL.equals(result.getResultStatusCode())){
- throw new JsonException(JsonMessageResultStatusEnum.FAIL, response);
- }
- } catch (Exception e) {
- logger.error(e);
- isValid = false;
- }
- if(logger.isDebugEnabled()){
- logger.debug("--> try verify with [name=" + name + ", password=***" + "] is valid user ? " + isValid);
- }
- return isValid;
- }
-
-
- public static void main(String[] args){
- new UserServiceImpl().isValidUser("admin", "admin");
- }
- }