LoginServiceImpl.java 2.21 KB
package com.xkl.service;

import com.xkl.config.Constants;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
import com.xkl.repository.AdminRepository;
import com.xkl.repository.UserRepository;
import com.xkl.security.SecurityTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by win7 on 2016/12/18.
 */
@Service
public class LoginServiceImpl implements ILoginService{
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Override
    public User check(String username, String password) {
        User user = userRepository.findByLoginAccountAndStatus(username, Constants.STATUS_OK2);

        if (user == null) {  //用户不存在
            return null;
        } else {
            String salt = user.getSalt();
            String pass_in_db = user.getLoginPwd();
            String pass = SecurityTool.getPassword(username, password, salt);
            if (!pass.equals(pass_in_db))//密码错误
               return null;
        }
        return user;
    }

    @Override
    public User check(String username) {
        User user = userRepository.findByLoginAccountAndStatus(username.trim(), Constants.STATUS_OK2);
        return user;
    }

    @Override
    public XklAdminEntity checkAdmin(String username, String password) {
        XklAdminEntity admin = adminRepository.findByAccountAndStatus(username, Constants.STATUS_OK);
        // 只有 普通区域管理员5,直辖区域管理员6;普通区域操作员7,直辖区域操作员8 才能使用接口登录操作上传软件。
        if (admin == null || admin.getRoleId() < 5 || admin.getRoleId() > 8) {
            return null;
        }else{
            String salt = admin.getSalt();
            String adminType = Integer.toString(admin.getType());
            String str = admin.getAccount() + password + adminType + salt; // 构建待加密字符串
            String calcuPass = SecurityTool.encode(SecurityTool.ALGORITHM_MD5, str);
            String pass_in_db = admin.getPwd();
            if (!calcuPass.equals(pass_in_db))//密码错误
                return null;
        }
        return admin;
    }
}