接口文檔:
1.1. 注冊接口
1.1.1. 檢查數據是否可用
請求方法 |
GET |
URL |
http://sso.taotao.com/user/check/{param}/{type} |
參數說明 |
格式如:zhangsan/ 1,其中zhangsan是校驗的數據,type為類型,可選參數1、2、3分別代表username、phone、email
可選參數callback:如果有此參數表示此方法為jsonp請求,需要支持jsonp。
|
示例 |
http://sso.taotao.com/user/check/zhangsan/1 |
返回值 |
{ status: 200 //200 成功 msg: "OK" // 返回信息消息 data: false // 返回數據,true:數據可用,false:數據不可用 }
|
1.1.2. 用戶注冊
請求方法 |
POST |
URL |
http://sso.taotao.com/user/register |
參數 |
username //用戶名 password //密碼 phone //手機號 email //郵箱 |
參數說明 |
|
示例 |
http://sso.taotao.com/user/register
|
返回值 |
{ status: 400 msg: "注冊失敗. 請校驗數據后請再提交數據." data: null }
|
1.2. 用戶登錄
請求方法 |
POST |
URL |
|
參數 |
username //用戶名 password //密碼 |
參數說明 |
|
示例 |
http://sso.taotao.com/user/login
username=zhangsan&password=123 |
返回值 |
{ status: 200 msg: "OK" data: "fe5cb546aeb3ce1bf37abcb08a40493e" //登錄成功,返回token }
|
1.3. 通過token查詢用戶信息
請求方法 |
GET |
URL |
http://sso.taotao.com/user/token/{token} |
參數 |
token //用戶登錄憑證 callback//jsonp回調方法 |
參數說明 |
可選參數callback:如果有此參數表示此方法為jsonp請求,需要支持jsonp。
|
示例 |
http://sso.taotao.com/user/token/fe5cb546aeb3ce1bf37abcb08a40493e
|
返回值 |
{ status: 200 msg: "OK" data: "{"id":1,"username":"zhangzhijun","phone":"15800807944", "email":"420840806@qq.com","created":1414119176000,"updated":1414119179000}" }
|
1.4. 安全退出
請求方法 |
GET |
URL |
http://sso.taotao.com/user/logout/{token} |
參數 |
token //用戶登錄憑證 callback//jsonp回調方法 |
參數說明 |
可選參數callback:如果有此參數表示此方法為jsonp請求,需要支持jsonp。
|
示例 |
http://sso.taotao.com/user/logout/fe5cb546aeb3ce1bf37abcb08a40493e
|
返回值 |
{ status: 200 msg: "OK" data: "" } |
Controller:
package com.taotao.sso.controller; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.converter.json.MappingJacksonValue; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.taotao.common.pojo.TaotaoResult; import com.taotao.common.utils.ExceptionUtil; import com.taotao.pojo.TbUser; import com.taotao.sso.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /**校驗用戶名、電話、郵箱是否重復方法 * 接口文檔: * 請求方法 GET URL http://sso.taotao.com/user/check/{param}/{type} 參數說明 格式如:zhangsan/ 1,其中zhangsan是校驗的數據,type為類型,可選參數1、2、3分別代表username、phone、email 可選參數callback:如果有此參數表示此方法為jsonp請求,需要支持jsonp。 */ @RequestMapping("/check/{param}/{type}") @ResponseBody public Object checkData(@PathVariable String param,@PathVariable Integer type,String callback){ //返回結果 TaotaoResult result = null; //校驗參數是否正確(注意:在Controller中校驗即可,service中可以不校驗了) if (StringUtils.isEmpty(param)) { result = TaotaoResult.build(400, "校驗內容不能為空"); } if (type==null) { result = TaotaoResult.build(400, "校驗內容參數不能為空"); } if (1!=type && 2!=type && 3!=type) { result = TaotaoResult.build(400, "校驗內容類型錯誤"); } //說明參數異常需要提前返回 if (result!=null) { //判斷是否需要支持jsonP if (callback!=null) { //需要將返回結果封裝成支持jsonP的形式(注意:這種返回json支持的寫法) MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; }else{ return result; } } //因為是提供接口服務,所以要處理可能出現的邏輯上的異常 try { //調用service執行正常的業務邏輯 result = userService.checkData(param, type); } catch (Exception e) { result = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } //正常返回也需要判斷是否需要jsonP if (null!=callback) { MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; }else{ return result; } } //用戶注冊 @RequestMapping("/register") @ResponseBody public TaotaoResult createUser(TbUser user) { try { TaotaoResult result = userService.createUser(user); return result; } catch (Exception e) { return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } } //用戶登錄 @RequestMapping("/login") @ResponseBody public TaotaoResult userLogin(String username,String password){ //因為是接口服務端,所以要try處理異常 try { TaotaoResult result = userService.userLogin(username, password); return result; } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } } //根據token獲取用戶信息(判斷是否登錄) @RequestMapping("/token/{token}") @ResponseBody public Object getUserByToken(@PathVariable String token,String callback){ TaotaoResult result = null; try { result = userService.getUserByToken(token); } catch (Exception e) { e.printStackTrace(); result = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } //判斷是否為jsonp調用 if (StringUtils.isEmpty(callback)) { return result; }else{ MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; } } }
Service:
package com.taotao.sso.service.impl; import java.util.Date; import java.util.List; import java.util.UUID; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.propertyeditors.UUIDEditor; import org.springframework.stereotype.Service; import org.springframework.util.DigestUtils; import com.taotao.common.pojo.TaotaoResult; import com.taotao.common.utils.JsonUtils; import com.taotao.mapper.TbUserMapper; import com.taotao.pojo.TbUser; import com.taotao.pojo.TbUserExample; import com.taotao.pojo.TbUserExample.Criteria; import com.taotao.sso.dao.JedisClient; import com.taotao.sso.service.UserService; /** * 用戶管理的service * @author Administrator */ @Service public class UserServiceImpl implements UserService { @Autowired private TbUserMapper userMapper; @Autowired private JedisClient jedisClient; // #用戶session在redis中保存的key @Value("${REDIS_USER_SESSION_KEY}") private String REDIS_USER_SESSION_KEY; //REDIS_USER_SESSION_KEY=REDIS_USER_SESSION // #session的過期時間30分鍾 @Value("${SSO_SESSION_EXPIRE}") private Integer SSO_SESSION_EXPIRE; //SSO_SESSION_EXPIRE=1800 //校驗用戶名、電話、郵箱 是否不重復 @Override public TaotaoResult checkData(String content, Integer type) { //創建查詢對象 TbUserExample example = new TbUserExample(); Criteria criteria = example.createCriteria(); //封裝查詢條件 switch (type) { case 1: criteria.andUsernameEqualTo(content); break; case 2: criteria.andPhoneEqualTo(content); break; case 3: criteria.andEmailEqualTo(content); break; } //因為在Controller層中調用此接口前就已經校驗過 type的值一定為123中的一個,所以這里不用再次校驗了 //執行查詢 List<TbUser> list = userMapper.selectByExample(example); if (list!=null && list.size()>0) { return TaotaoResult.ok(false); } return TaotaoResult.ok(true); } //用戶注冊 @Override public TaotaoResult createUser(TbUser user) { user.setCreated(new Date()); user.setUpdated(new Date()); user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes())); userMapper.insert(user); return TaotaoResult.ok(); } //用戶登錄 @Override public TaotaoResult userLogin(String username, String password) { //和數據庫中數據比對 TbUserExample example = new TbUserExample(); example.createCriteria(). andUsernameEqualTo(username). andPasswordEqualTo(DigestUtils.md5DigestAsHex(password.getBytes())); List<TbUser> list = userMapper.selectByExample(example); if (list==null || list.size()==0) { return TaotaoResult.build(400, "用戶名或密碼錯誤"); } //說明登錄成功,獲取用戶對象 TbUser user = list.get(0); //生成token,並把用戶存入redis中 //注意:這種生成uuid的方法(無需引入其他jar包,用的java.util包) String token = UUID.randomUUID().toString(); //注意:存入redis中的用戶信息的key 是 大分類 + : +生成的 uuid,value 是用戶的對象轉json串 String key = REDIS_USER_SESSION_KEY+":"+token; //為了安全,在存入用戶信息前先將用戶密碼去掉 user.setPassword(null); String userJson = JsonUtils.objectToJson(user); //將用戶存入redis中 jedisClient.set(key, userJson); //注意:這里用戶登錄后把用戶信息及token存入redis是正常的業務邏輯,如果失敗,需要整個方法回滾,所以不用單獨try //設置過期時間 jedisClient.expire(key, SSO_SESSION_EXPIRE); return TaotaoResult.ok(token); } //根據token判斷用戶是否為登錄狀態 @Override public TaotaoResult getUserByToken(String token) { //拿token到redis中取用戶信息 REDIS_USER_SESSION;:8d7b07f2-eb83-4446-bab4-bc1416727b5f String key = REDIS_USER_SESSION_KEY+":"+token; String json = jedisClient.get(key); //判斷是否為空 if (StringUtils.isEmpty(json)) { return TaotaoResult.build(400, "此session已經過期,請重新登錄"); } //並更新過期時間 jedisClient.expire(key, SSO_SESSION_EXPIRE); //返回用戶信息 return TaotaoResult.ok(JsonUtils.jsonToPojo(json, TbUser.class)); } }