不同于传统的单机用户信息存放在session域中,单点登录系统创建专门的服务处理用户登录,相关信息存储在Redis中。
1.定义服务的接口
查询值是否可用 http://YOURHOST/user/check/{param}/{type} type 可以是1,2,3,分别代表username,phone,email 该接口主要目的是查询要注册的信息是否可用,get方法。 例子 http://YOURHOST/user/check/zhangsan/1 { status: 200 //200 成功 msg: "OK" // 返回信息消息 data: false // 返回数据,true:数据可用,false:数据不可用 } 用户注册 http://YOURHOST/user/register POST方法,参数username,password,phone,email 返回值 { status: 400 msg: "注册失败. 请校验数据后请再提交数据." data: null } 用户登录 http://YOURHOST/user/login POST方法:参数:username,password 返回值: { status: 200 msg: "OK" data: "fe5cb546aeb3ce1bf37abcb08a40493e" //登录成功,返回token } 通过token查询用户信息 http://YOURHOST/user/token/{token} 方法:GET,返回值 { status: 200 msg: "OK" data: "{"id":1,"username":"zhangzhijun","phone":"15800807944", "email":"[email protected]","created":1414119176000,"updated":1414119179000}" } 安全退出: http://YOURHOST/user/logout/{token} 返回值 { status: 200 msg: "OK" data: "" }
Controller层代码
@Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/check/{param}/{type}") @ResponseBody public Object checkData(@PathVariable String param, @PathVariable Integer type, String callback) { TaotaoResult result = null; //参数有效性校验 if (StringUtils.isBlank(param)) { result = TaotaoResult.build(400, "校验内容不能为空"); } if (type == null) { result = TaotaoResult.build(400, "校验内容类型不能为空"); } if (type != 1 && type != 2 && type != 3 ) { result = TaotaoResult.build(400, "校验内容类型错误"); } //校验出错 if (null != result) { if (null != callback) { MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; } else { return result; } } //调用服务 try { result = userService.checkData(param, type); } catch (Exception e) { result = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } if (null != callback) { MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; } else { return result; } } @RequestMapping(value="/register", method=RequestMethod.POST) @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(value="/login", method=RequestMethod.POST) @ResponseBody public TaotaoResult userLogin(String username, String password, HttpServletRequest request,HttpServletResponse response) { try { TaotaoResult result = userService.userLogin(username, password,request,response); return result; } catch (Exception e) { e.printStackTrace(); return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e)); } } @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.isBlank(callback)) { return result; } else { MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; } } }