LoginServiceImpl.java 2 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);
        if(admin == null){
            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;
    }
}