①在控制类中添加接口
@PostMapping("/addUserInfo")
public AjaxResult addUserInfo(@RequestBody UserInfoDto userInfoDto) throws Exception {
return AjaxResult.success(wxLoginService.wxLogin(userInfoDto.getName(),userInfoDto.getIdentityNumber()));
}
②添加WxLoginService类文件
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class WxLoginService {
@Resource
private WxAuthenticationProvider authenticationManager;
@Autowired
private TokenService tokenService;
public String wxLogin(String username, String password) throws Exception {
Authentication authentication = null;
LoginUser loginUser = null;
try {
authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (Exception e) {
if (e instanceof BadCredentialsException) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
throw new UserPasswordNotMatchException();
}
}
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
if(authentication != null){
loginUser = (LoginUser) authentication.getPrincipal();
}else {
throw new Exception("账号密码错误");
}
return tokenService.createToken(loginUser);
}
}
③添加WxAuthenticationProvider 文件
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import java.util.Collection;
@Component
public class WxAuthenticationProvider implements AuthenticationProvider {
@Autowired
private WxUserDetailsServiceImpl userDetailsService;
@SneakyThrows
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String userName = authentication.getName();
Object password = authentication.getCredentials();
UserDetails userInfo = userDetailsService.loadUserByUsername(userName);
String pwd = userInfo.getPassword();
if(!password.toString().equals(pwd.substring(pwd.length() - 6, pwd.length()))){
throw new Exception("用户不存在/密码错误");
}
Collection<? extends GrantedAuthority> authorities = userInfo.getAuthorities();
return new UsernamePasswordAuthenticationToken(userInfo, userInfo.getPassword(), authorities);
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}
④添加UserDetailsService 实现类
import com.ruoyi.common.core.domain.entity.WxUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.PojoUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.construction.domain.vo.PersonnelInformationVo;
import com.ruoyi.construction.service.impl.ConstructionPersonnelInformationServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class WxUserDetailsServiceImpl implements UserDetailsService {
@Autowired
private ConstructionPersonnelInformationServiceImpl constructionPersonnelInformationService;
@Autowired
private SysPermissionService permissionService;
@SneakyThrows
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
PersonnelInformationVo personnelInformationVo = constructionPersonnelInformationService.selectConstructionPersonnelInformationByName(username);
if (StringUtils.isNull(personnelInformationVo)) {
throw new Exception("登录用户不存在");
}
return createLoginUser(personnelInformationVo);
}
public UserDetails createLoginUser(PersonnelInformationVo personnelInformationVo) {
WxUser wxUser = PojoUtil.copyBean(personnelInformationVo, WxUser.class);
wxUser.setUserName(personnelInformationVo.getName());
wxUser.setPassword(personnelInformationVo.getIdentityNumber());
return new LoginUser(wxUser, permissionService.WxGetMenuPermission());
}
}
⑤在LoginUser中添加对应的用户信息属性,并修改username和password的get方法
private WxUser wxUser;
public LoginUser(WxUser wxUser, Set<String> permissions)
{
this.wxUser = wxUser;
this.permissions = permissions;
}
@Override
public String getPassword()
{
if(user != null){
return user.getPassword();
}else {
return wxUser.getPassword();
}
}
@Override
public String getUsername()
{
if(user != null){
return user.getUserName();
}else {
return wxUser.getUserName();
}
}
⑤需要在 xxx-framework/src/main/java/…/SecurityConfig中添加条件 @Qualifier(“userDetailsServiceImpl”)
到此就可以实现小程序获取app用户的登录接口了~