|
@@ -1,10 +1,21 @@
|
|
package com.macro.mall.service.impl;
|
|
package com.macro.mall.service.impl;
|
|
|
|
|
|
|
|
+import com.macro.mall.dto.UmsAdminParam;
|
|
import com.macro.mall.mapper.UmsAdminMapper;
|
|
import com.macro.mall.mapper.UmsAdminMapper;
|
|
import com.macro.mall.model.UmsAdmin;
|
|
import com.macro.mall.model.UmsAdmin;
|
|
import com.macro.mall.model.UmsAdminExample;
|
|
import com.macro.mall.model.UmsAdminExample;
|
|
import com.macro.mall.service.UmsAdminService;
|
|
import com.macro.mall.service.UmsAdminService;
|
|
|
|
+import com.macro.mall.util.JwtTokenUtil;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+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;
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
+import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -16,6 +27,17 @@ import java.util.List;
|
|
public class UmsAdminServiceImpl implements UmsAdminService{
|
|
public class UmsAdminServiceImpl implements UmsAdminService{
|
|
@Autowired
|
|
@Autowired
|
|
private UmsAdminMapper adminMapper;
|
|
private UmsAdminMapper adminMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserDetailsService userDetailsService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private JwtTokenUtil jwtTokenUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ private PasswordEncoder passwordEncoder;
|
|
|
|
+ @Value("${jwt.tokenHead}")
|
|
|
|
+ private String tokenHead;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public UmsAdmin getAdminByUsername(String username) {
|
|
public UmsAdmin getAdminByUsername(String username) {
|
|
UmsAdminExample example = new UmsAdminExample();
|
|
UmsAdminExample example = new UmsAdminExample();
|
|
@@ -26,4 +48,40 @@ public class UmsAdminServiceImpl implements UmsAdminService{
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @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) {
|
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,password);
|
|
|
|
+ Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
+ UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
|
|
|
+ return jwtTokenUtil.generateToken(userDetails);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String refreshToken(String oldToken) {
|
|
|
|
+ String token = oldToken.substring(tokenHead.length());
|
|
|
|
+ if(jwtTokenUtil.canRefresh(token)){
|
|
|
|
+ return jwtTokenUtil.refreshToken(token);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
}
|
|
}
|