Java框架搭建SSO

第二十篇:单点登录系统的具体实现(1)

2018-07-23  本文已影响87人  __y

前言:
上面我们已经假设已经搭建好了相应的工程,下面就是具体实现的业务逻辑代码
主要以下业务代码需要实现:
1、sso注册功能实现
2、sso登录功能实现
3、通过token获得用户信息
4、Ajax跨域请求(jsonp)

1.检测数据功能实现

首先我们知道,我们实现注册功能的时候要注意校验,看有没有重复的数据
功能分析
请求的url:/user/check/{param}/{type}
参数:从url中取参数1、String param(要校验的数据)2、Integer type(校验的数据类型)
响应的数据:json数据。TaotaoResult,封装的数据校验的结果true:成功false:失败。
业务逻辑:
1、从tb_user表中查询数据
2、查询条件根据参数动态生成。
3、判断查询结果,如果查询到数据返回false。
4、如果没有返回true。
5、使用TaotaoResult包装,并返回。
下面是实现
Dao层
从tb_user表查询。可以使用逆向工程。
Service层

image.png
参数:
1、要校验的数据:String param
2、数据类型:int type(1、2、3分别代表username、phone、email)
返回值:TaotaoResult
具体代码
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper userMapper;
    
    @Override
    public TaotaoResult checkData(String param, int type) {
        // 1、从tb_user表中查询数据
        TbUserExample example = new TbUserExample();
        Criteria criteria = example.createCriteria();
        // 2、查询条件根据参数动态生成。
        //1、2、3分别代表username、phone、email
        if (type == 1) {
            criteria.andUsernameEqualTo(param);
        } else if (type == 2) {
            criteria.andPhoneEqualTo(param);
        } else if (type == 3) {
            criteria.andEmailEqualTo(param);
        } else {
            return TaotaoResult.build(400, "非法的参数");
        }
        //执行查询
        List<TbUser> list = userMapper.selectByExample(example);
        // 3、判断查询结果,如果查询到数据返回false。
        if (list == null || list.size() == 0) {
            // 4、如果没有返回true。
            return TaotaoResult.ok(true);
        } 
        // 5、使用TaotaoResult包装,并返回。
        return TaotaoResult.ok(false);
    }

}

表现层
需要在taotao-sso-web中实现。

@Controller
public class UserController {

    @Autowired
    private UserService userService;
    
    @RequestMapping("/user/check/{param}/{type}")
    @ResponseBody
    public TaotaoResult checkData(@PathVariable String param, @PathVariable Integer type) {
        TaotaoResult taotaoResult = userService.checkData(param, type);
        return taotaoResult;
    }
}

2.注册功能

@Override
    public TaotaoResult createUser(TbUser user) {
        // 1、使用TbUser接收提交的请求。
        
        if (StringUtils.isBlank(user.getUsername())) {
            return TaotaoResult.build(400, "用户名不能为空");
        }
        if (StringUtils.isBlank(user.getPassword())) {
            return TaotaoResult.build(400, "密码不能为空");
        }
        //校验数据是否可用
        TaotaoResult result = checkData(user.getUsername(), 1);
        if (!(boolean) result.getData()) {
            return TaotaoResult.build(400, "此用户名已经被使用");
        }
        //校验电话是否可以
        if (StringUtils.isNotBlank(user.getPhone())) {
            result = checkData(user.getPhone(), 2);
            if (!(boolean) result.getData()) {
                return TaotaoResult.build(400, "此手机号已经被使用");
            }
        }
        //校验email是否可用
        if (StringUtils.isNotBlank(user.getEmail())) {
            result = checkData(user.getEmail(), 3);
            if (!(boolean) result.getData()) {
                return TaotaoResult.build(400, "此邮件地址已经被使用");
            }
        }
        // 2、补全TbUser其他属性。
        user.setCreated(new Date());
        user.setUpdated(new Date());
        // 3、密码要进行MD5加密。
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass);
        // 4、把用户信息插入到数据库中。
        userMapper.insert(user);
        // 5、返回TaotaoResult。
        return TaotaoResult.ok();
    }

@RequestMapping(value="/user/register", method=RequestMethod.POST)
    @ResponseBody
    public TaotaoResult register(TbUser user) {
        TaotaoResult result = userService.createUser(user);
        return result;
    }

上一篇 下一篇

猜你喜欢

热点阅读