用户注册&单点登录
package com.tttttt.portal.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.tttttt.manager.pojo.User; import com.tttttt.portal.util.CookieUtils; import com.tttttt.sso.service.UserService; @Controller @RequestMapping("user") public class UserController { @Autowired private UserService userService; @Value("${TT_TICKET}") private String TT_TICKET; // type : "POST", // //url : "/service/user/doRegister", // url : "/user/doRegister.html", // data : {password:_password,username:_username,phone:_phone}, // dataType : 'json', // if(result.status == "200") /** * 用户注册 * * @param user * @return */ @RequestMapping(value = "doRegister", method = RequestMethod.POST) @ResponseBody public Map<String, Object> doRegister(User user) { // 调用sso服务注册用户 this.userService.saveUser(user); // 封装返回数据map Map<String, Object> map = new HashMap<>(); map.put("status", "200"); return map; } // type: "POST", // url: "/service/user/doLogin?r=" + Math.random(), // data: {username:_username,password:_password}, // dataType : "json", // if (obj.status == 200) /** * 用户登录 * * @param request * @param response * @param user * @return */ @RequestMapping(value = "doLogin", method = RequestMethod.POST) @ResponseBody public Map<String, Object> doLogin(HttpServletRequest request, HttpServletResponse response, User user) { // 声明返回的map Map<String, Object> map = new HashMap<>(); // 调用单点登录服务,执行用户登录操作,返回ticket String ticket = this.userService.doLogin(user); // 判断ticket是否为非空 if (StringUtils.isNotBlank(ticket)) { // 如果为非空表示登录成功 // 把ticket放到cookie中 CookieUtils.setCookie(request, response, this.TT_TICKET, ticket, 60 * 60 * 24, true); // 封装返回数据 map.put("status", 200); } // 如果为空,表示登录失败,什么都不做 // 返回结果 return map; } }
package com.tttttt.sso.service; import com.tttttt.manager.pojo.User; public interface UserService { /** * 检查数据是否可用 * * @param param * @param type * @return */ Boolean check(String param, Integer type); /** * 根据Ticket查询用户 * * @param ticket * @return */ User queryUserByTicket(String ticket); /** * 用户注册 * * @param user */ void saveUser(User user); /** * 用户登录 * * @param user * @return */ String doLogin(User user); }
package com.tttttt.sso.service.impl; import java.io.IOException; import java.util.Date; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.tttttt.manager.mapper.UserMapper; import com.tttttt.manager.pojo.User; import com.tttttt.sso.redis.RedisUtils; import com.tttttt.sso.service.UserService; @Service public class UserServiceImpl implements UserService { private static final ObjectMapper MAPPER = new ObjectMapper(); @Autowired private UserMapper userMapper; @Value("${tttttt_TICKET_KEY}") private String tttttt_TICKET_KEY; @Autowired private RedisUtils redisUtils; @Override public Boolean check(String param, Integer type) { // 声明查询条件 User user = new User(); // 1、2、3分别代表username、phone、email switch (type) { case 1: user.setUsername(param); break; case 2: user.setPhone(param); break; case 3: user.setEmail(param); break; default: break; } // 根据条件查询 int count = this.userMapper.selectCount(user); // 如果查询结果count为0表示没有人用过,所以返回true,表示可用 // 如果查询结果count不为0表示有人用过,所以返回false,表示不可用 return count == 0; } @Override public User queryUserByTicket(String ticket) { // 从redis中查询用户的登录信息,是一个json格式的数据 // 需要给redis中的key增加前缀,便于redis数据的维护和管理 String json = this.redisUtils.get(this.tttttt_TICKET_KEY + ticket); // 判断json不为空 if (StringUtils.isNotBlank(json)) { try { // 把json格式的数据转为User对象 User user = MAPPER.readValue(json, User.class); // 如果用户请求这个方法,表示用户是活动的,所以需要重置有效时间 this.redisUtils.expire(this.tttttt_TICKET_KEY + ticket, 60 * 60); // 返回user结果 return user; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 如果查询结果为空,或者有异常,返回null return null; } @Override public void saveUser(User user) { // 设置用户的数据 user.setCreated(new Date()); user.setUpdated(user.getCreated()); // 对用户密码进行md5加密 user.setPassword(DigestUtils.md5Hex(user.getPassword())); // 保存用户数据到MySQL this.userMapper.insert(user); } @Override public String doLogin(User user) { // 根据条件从MySQL中查询用户 user.setPassword(DigestUtils.md5Hex(user.getPassword())); User result = this.userMapper.selectOne(user); // 如果查到的结果不为空,表示用户登录成功 if (result != null) { // 生成唯一的数,就是ticket // 利用redis单线程的特点,使用incr每次加一,获取唯一数 // ticket就是redis的唯一数+用户id String ticket = "" + this.redisUtils.incr("tttttt_TICKET_INCR") + result.getId(); try { // 把ticket和用户的数据(json数据)放到redis中 this.redisUtils.set(this.tttttt_TICKET_KEY + ticket, MAPPER.writeValueAsString(result), 60 * 60); // 返回ticket return ticket; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 如果查询结果为空或者有异常,返回空 return null; } }
登录的问题:
1.没有欢迎用户的语句
2.cookie中没有ticket的信息
#配置Nginx反向代理的时候携带域名
proxy_set_header Host $host;