UmsAdminServiceImpl.java 3.8 KB
Newer Older
Z
zhh 已提交
1 2
package com.macro.mall.service.impl;

Z
zhh 已提交
3
import com.macro.mall.dto.UmsAdminParam;
Z
zhh 已提交
4 5 6 7
import com.macro.mall.mapper.UmsAdminMapper;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.model.UmsAdminExample;
import com.macro.mall.service.UmsAdminService;
Z
zhh 已提交
8
import com.macro.mall.util.JwtTokenUtil;
Z
zhh 已提交
9 10
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Z
zhh 已提交
11
import org.springframework.beans.BeanUtils;
Z
zhh 已提交
12
import org.springframework.beans.factory.annotation.Autowired;
Z
zhh 已提交
13 14 15 16 17
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.Authentication;
Z
zhh 已提交
18
import org.springframework.security.core.AuthenticationException;
Z
zhh 已提交
19 20 21
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
Z
zhh 已提交
22 23 24 25 26 27 28 29 30
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UmsAdminService实现类
 */
@Service
public class UmsAdminServiceImpl implements UmsAdminService{
Z
zhh 已提交
31
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
Z
zhh 已提交
32 33
    @Autowired
    private UmsAdminMapper adminMapper;
Z
zhh 已提交
34 35 36 37 38 39 40 41 42 43 44
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

Z
zhh 已提交
45 46 47 48 49 50 51 52 53 54
    @Override
    public UmsAdmin getAdminByUsername(String username) {
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UmsAdmin> adminList = adminMapper.selectByExample(example);
        if(adminList!=null&&adminList.size()>0){
            return adminList.get(0);
        }
        return null;
    }
Z
zhh 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    @Override
    public UmsAdmin register(UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminParam,umsAdmin);
        //查询是否有相同用户名的用户
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(umsAdmin.getUsername());
        List<UmsAdmin> umsAdminList = adminMapper.selectByExample(example);
        if(umsAdminList.size()>0){
            return null;
        }
        //将密码进行加密操作
        String md5Password = passwordEncoder.encodePassword(umsAdmin.getPassword(), null);
        umsAdmin.setPassword(md5Password);
        adminMapper.insert(umsAdmin);
        return umsAdmin;
    }

    @Override
    public String login(String username, String password) {
Z
zhh 已提交
76 77 78 79 80 81 82 83 84 85 86 87
        String token = null;
        //密码需要客户端加密后传递
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,passwordEncoder.encodePassword(password,null));
        try {
            Authentication authentication = authenticationManager.authenticate(authenticationToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            token = jwtTokenUtil.generateToken(userDetails);
        } catch (AuthenticationException e) {
            LOGGER.warn("登录异常:{}",e.getMessage());
        }
        return token;
Z
zhh 已提交
88 89 90 91 92 93 94 95 96 97
    }

    @Override
    public String refreshToken(String oldToken) {
        String token = oldToken.substring(tokenHead.length());
        if(jwtTokenUtil.canRefresh(token)){
            return jwtTokenUtil.refreshToken(token);
        }
        return null;
    }
Z
zhh 已提交
98
}