金币系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.3 KiB

  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.domain.entity.Admin;
  3. import com.example.demo.mapper.AdminMapper;
  4. import com.example.demo.security.SecurityConfig;
  5. import com.example.demo.sevice.AdminService;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import lombok.RequiredArgsConstructor;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.cache.annotation.CacheConfig;
  11. import org.springframework.cache.annotation.CacheEvict;
  12. import org.springframework.cache.annotation.CachePut;
  13. import org.springframework.cache.annotation.Cacheable;
  14. import org.springframework.security.authentication.AuthenticationManager;
  15. import org.springframework.security.authentication.BadCredentialsException;
  16. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  17. import org.springframework.security.core.Authentication;
  18. import org.springframework.security.crypto.password.PasswordEncoder;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.List;
  22. @Transactional
  23. @Service
  24. @RequiredArgsConstructor
  25. @CacheConfig(cacheNames = "admin")
  26. public class AdminServiceImpl implements AdminService {
  27. @Autowired
  28. private SecurityConfig securityConfig;
  29. private final AdminMapper adminMapper;
  30. @Autowired
  31. private AuthenticationManager authenticationManager;
  32. @Autowired
  33. private PasswordEncoder passwordEncoder;
  34. @Override
  35. public int add(Admin admin) {
  36. return adminMapper.insert(admin);
  37. }
  38. @CacheEvict(value = "admin",allEntries = true)
  39. @Override
  40. public int edit(Admin admin) {
  41. return adminMapper.update(admin);
  42. }
  43. @Cacheable(key="#root.method.name")
  44. @Override
  45. public Admin findById(Integer adminId) {
  46. return adminMapper.selectById(adminId);
  47. }
  48. @Cacheable(key="#root.method.name")
  49. @Override
  50. public Admin findByUsername(String username) {
  51. return adminMapper.selectByName(username);
  52. }
  53. @Cacheable(key="#root.method.name")
  54. @Override
  55. public List<Admin> search(Admin admin) {
  56. return adminMapper.select(admin);
  57. }
  58. @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #admin.hashCode() ")
  59. @Override
  60. public PageInfo<Admin> searchForPage(Integer pageNum, Integer pageSize, Admin admin) {
  61. PageHelper.startPage(pageNum, pageSize);
  62. List<Admin> list = adminMapper.select(admin);
  63. return new PageInfo<>(list);
  64. }
  65. @Override
  66. public Admin login(Admin admin) throws Exception {
  67. System.out.println("第一步");
  68. UsernamePasswordAuthenticationToken token =
  69. new UsernamePasswordAuthenticationToken(admin.getJwcode(),admin.getPassword());
  70. System.out.println(token+"第二步");
  71. try {
  72. Authentication authentication = authenticationManager.authenticate(token);
  73. System.out.println(authentication+"第一步");
  74. Admin loginAdmin = (Admin) authentication.getPrincipal();
  75. System.out.println(token);
  76. System.out.println(loginAdmin+"logAdmin");
  77. return loginAdmin;
  78. }catch (BadCredentialsException exception){
  79. throw new BadCredentialsException("用户或密码错误");
  80. }
  81. }
  82. }