标签:stack ace 记录 获取 ati blank ssi mapper common
package com.taotao.sso.service.impl;
service层:代码
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.stereotype.Service;
import org.springframework.util.DigestUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.common.utils.TaotaoResult;
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层进行数据库的校验
@Service
public class UserServiceImpl implements UserService {
@Autowired
private TbUserMapper userMapper;
@Autowired
private JedisClient jedisClient;
@Value("${REDIS_SESSION_TOKEN}")
private String REDIS_SESSION_TOKEN;
@Value("${REDSI_SESSION_TIME}")
private Integer REDSI_SESSION_TIME;
public TaotaoResult checkData(String param, Integer type) {
TbUserExample example=new TbUserExample();
Criteria criteria=example.createCriteria();
//1代表用户名,2代表电话,3代表邮箱
if (1==type) {
criteria.andUsernameEqualTo(param);
}else if (2==type) {
criteria.andPhoneEqualTo(param);
}else{
criteria.andEmailEqualTo(param);
}
List<TbUser> result= userMapper.selectByExample(example);
if(result==null || result.size()==0){
return TaotaoResult.ok(true);
}else{
return TaotaoResult.ok(false);
}
}
//用户注册接口实现类
@Override
public TaotaoResult loginData(TbUser user) {
user.setUpdated(new Date());
user.setCreated(new Date());
//spring提供了一个MD5加密密码的方式
user.setPassword("DigestUtils..md5DigestAsHex(user.getPassword().getBytes())");
userMapper.insert(user);
return TaotaoResult.ok();
}
//登录接口的实现逻辑
@Override
public TaotaoResult userLogin(String username, String password) {
//先查询数据库中是否存在此用户名,若存在则表示用户名正确,再验证密码是否跟数据库中的MD5加密后的是否一样,都满足则登录成功,给予访问者一个token令牌,并将token令牌写入redis
TbUserExample example=new TbUserExample();
Criteria criteria=example.createCriteria();
criteria.andUsernameEqualTo(username);
//返回值的快捷键alt+shift+L
List<TbUser> list = userMapper.selectByExample(example);
if (list==null || list.size()==0) {
TaotaoResult.build(400,"用户名或密码不存在");
}
//根据用户名查询只能查询到一条记录,所以获取当前这个对象的信息,就是list.get(0)
TbUser user=list.get(0);
if(DigestUtils.md5DigestAsHex(password.getBytes())!=user.getPassword() ) {
TaotaoResult.build(400,"用户名或密码不存在");
}
//将密码保存到redis比较危险,所以这里在保存之前将密码去掉
user.setPassword(null);
//否则用户名和密码都正确,则可以登录,办法token令牌
String token=UUID.randomUUID().toString();
//将token写入redis中,写入的方法要记住。要用到redis的客户端jedis.value的值是序列号的形式
jedisClient.set(REDIS_SESSION_TOKEN+":"+token, JsonUtils.objectToJson(user));
//设置session的过期时间
jedisClient.expire(REDIS_SESSION_TOKEN+":"+token, REDSI_SESSION_TIME);
//返回token
return TaotaoResult.ok(token);
}
//根据token判断用户是否已经登录
@Override
public TaotaoResult userByToken(String token) {
//从redis中获取token的信息,判断用户是否过期,如果没有过期则更新session时间
String json = jedisClient.get(REDIS_SESSION_TOKEN+":"+token);
if (StringUtils.isBlank(json)) {
return TaotaoResult.build(400,"session已经过期,请重新登录");
}
//如果没有过期,则更新session时间
jedisClient.expire(REDIS_SESSION_TOKEN+":"+token,REDSI_SESSION_TIME);
//返回给controller层的是json数据
return TaotaoResult.ok(JsonUtils.jsonToPojo(json, TbUser.class));
}
}
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.common.utils.TaotaoResult;
import com.taotao.pojo.TbUser;
import com.taotao.sso.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/check/{param}/{type}")
@ResponseBody
public Object checkDate(@PathVariable String param ,@PathVariable Integer type,String callback){
TaotaoResult result=null;
if (param==null) {
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(400,ExceptionUtil.getStackTrace(e));
}
//校验出错
if (null != result) {
if (null != callback) {
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
} else {
return result;
}
}
return result;
}
//用户注册接口编写
@RequestMapping(value="/register",method=RequestMethod.POST)
@ResponseBody
public TaotaoResult loginData(TbUser user){
//利用try catch捕获异常
try {
TaotaoResult result=userService.loginData(user);
return result;
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500,ExceptionUtil.getStackTrace(e));
}
}
//登录接口的编写,接口文档提示是post请求,如果不写post则get和post都可以,一般如果接口明确写了则要写post方法
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public TaotaoResult userLogin(String username,String password){
try {
TaotaoResult result=userService.userLogin(username, password);
return result;
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(400,ExceptionUtil.getStackTrace(e));
}
}
@RequestMapping("/token/{token}")
@ResponseBody
public Object userByToken(@PathVariable String token,String callback){
TaotaoResult result=null;
try {
result=userService.userByToken(token);
} catch (Exception e) {
e.printStackTrace();
result=TaotaoResult.build(500,ExceptionUtil.getStackTrace(e));
}
if (StringUtils.isBlank(callback)) {
return result;
}else{
MappingJacksonValue mappingJacksonValue=new MappingJacksonValue(result);
mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
}
}
}
标签:stack ace 记录 获取 ati blank ssi mapper common
原文地址:http://www.cnblogs.com/fengli9998/p/6403503.html