package com.example.demo.serviceImpl; import com.example.demo.domain.entity.Admin; import com.example.demo.mapper.AdminMapper; import com.example.demo.security.SecurityConfig; import com.example.demo.sevice.AdminService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Transactional @Service @RequiredArgsConstructor @CacheConfig(cacheNames = "admin") public class AdminServiceImpl implements AdminService { @Autowired private SecurityConfig securityConfig; private final AdminMapper adminMapper; @Autowired private AuthenticationManager authenticationManager; @Autowired private PasswordEncoder passwordEncoder; @Override public int add(Admin admin) { return adminMapper.insert(admin); } @CacheEvict(value = "admin",allEntries = true) @Override public int edit(Admin admin) { return adminMapper.update(admin); } @Cacheable(key="#root.method.name") @Override public Admin findById(Integer adminId) { return adminMapper.selectById(adminId); } @Cacheable(key="#root.method.name") @Override public Admin findByUsername(String username) { return adminMapper.selectByName(username); } @Cacheable(key="#root.method.name") @Override public List search(Admin admin) { return adminMapper.select(admin); } @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #admin.hashCode() ") @Override public PageInfo searchForPage(Integer pageNum, Integer pageSize, Admin admin) { PageHelper.startPage(pageNum, pageSize); List list = adminMapper.select(admin); return new PageInfo<>(list); } @Override public Admin login(Admin admin) throws Exception { System.out.println("第一步"); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(admin.getJwcode(),admin.getPassword()); System.out.println(token+"第二步"); try { Authentication authentication = authenticationManager.authenticate(token); System.out.println(authentication+"第一步"); Admin loginAdmin = (Admin) authentication.getPrincipal(); System.out.println(token); System.out.println(loginAdmin+"logAdmin"); return loginAdmin; }catch (BadCredentialsException exception){ throw new BadCredentialsException("用户或密码错误"); } } }