This commit is contained in:
zt
2025-02-17 09:36:25 +08:00
parent d27512baa4
commit 038c65b59a
9 changed files with 149 additions and 103 deletions

View File

@ -4,8 +4,6 @@ import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
import com.ruoyi.framework.web.service.AllUserDetailsServiceImpl;
import com.ruoyi.framework.web.service.BgtUserDetailsServiceImpl;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@ -16,7 +14,6 @@ import org.springframework.security.config.annotation.method.configuration.Enabl
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
@ -104,7 +101,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求
.authorizeRequests()
// 对于登录login 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/bgt/login", "/captchaImage","/demo/tress/all").anonymous()
.antMatchers("/login", "/app/login", "/captchaImage","/demo/tress/all").anonymous()
.antMatchers(
HttpMethod.GET,
"/*.html",

View File

@ -1,12 +1,6 @@
package com.ruoyi.framework.web.service;
import cn.hutool.core.lang.Validator;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,16 +23,15 @@ public class AllUserDetailsServiceImpl implements UserDetailsService
private UserDetailsServiceImpl userDetailsService;
@Autowired
private BgtUserDetailsServiceImpl bgtUserDetailsService;
private AppUserDetailsServiceImpl appUserDetailsService;
@Override
public UserDetails loadUserByUsername(String var) throws UsernameNotFoundException
{
if(var.contains(Constants.BGT)){
var = var.replace(Constants.BGT, "");
return bgtUserDetailsService.loadUserByUsername(var);
}else {
if(var.contains(Constants.BGT)||var.contains(Constants.WGZ)){
return appUserDetailsService.loadUserByUsername(var);
} else {
return userDetailsService.loadUserByUsername(var);
}

View File

@ -24,7 +24,7 @@ import javax.servlet.http.HttpServletRequest;
* @author ruoyi
*/
@Component
public class BgtLoginService
public class AppLoginService
{
@Autowired
private TokenService tokenService;
@ -38,8 +38,8 @@ public class BgtLoginService
// @Autowired
// private CaptchaProperties captchaProperties;
@Autowired
private ISysUserService userService;
// @Autowired
// private ISysUserService userService;
@Autowired
private AsyncService asyncService;
@ -53,7 +53,7 @@ public class BgtLoginService
* @param uuid 唯一标识
* @return 结果
*/
public String login(String phone, String password, String code, String uuid)
public String login(String phone, String password, String code, String uuid,String userTypePrefix)
{
HttpServletRequest request = ServletUtils.getRequest();
// 用户验证
@ -62,7 +62,7 @@ public class BgtLoginService
{
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(Constants.BGT+phone, password));
.authenticate(new UsernamePasswordAuthenticationToken(userTypePrefix+phone, password));
}
catch (Exception e)
{

View File

@ -0,0 +1,119 @@
package com.ruoyi.framework.web.service;
import cn.hutool.core.lang.Validator;
import com.ruoyi.bgt.service.IBgtUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.BgtUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.BaseException;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import java.lang.reflect.Field;
/**
* 用户验证处理
*
* @author ruoyi
*/
@Service
public class AppUserDetailsServiceImpl implements UserDetailsService
{
private static final Logger log = LoggerFactory.getLogger(AppUserDetailsServiceImpl.class);
@Autowired
private IBgtUserService userService;
@Autowired
private SysPermissionService permissionService;
@Override
public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException
{
UserDetails userDetailsl = null;
if(phone.contains(Constants.BGT)){
BgtUser user = userService.selectUserByPhone(phone.replace(Constants.BGT,""));
check(user,phone);
userDetailsl = createLoginUser(user);
} else if (phone.contains(Constants.WGZ)) {
}
return userDetailsl;
}
public UserDetails createLoginUser(Object obj)
{
SysUser sysUser = new SysUser();
try {
Field phoneField = obj.getClass().getDeclaredField("phone");
phoneField.setAccessible(true);
String phone= phoneField.get(obj).toString();
Field userIdField = obj.getClass().getDeclaredField("userId");
userIdField.setAccessible(true);
Long userId = (Long)userIdField.get(obj);
Field usernameField = obj.getClass().getDeclaredField("username");
usernameField.setAccessible(true);
String username = usernameField.get(obj).toString();
Field passwordField = obj.getClass().getDeclaredField("password");
passwordField.setAccessible(true);
String password = passwordField.get(obj).toString();
sysUser.setPhonenumber(phone);
sysUser.setUserId(userId);
sysUser.setUserName(username);
sysUser.setPassword(password);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new LoginUser(sysUser, null);
}
public void check(Object obj,String phone){
if (Validator.isNull(obj))
{
log.info("登录用户:{} 不存在.", phone);
throw new UsernameNotFoundException("登录用户:" + phone + " 不存在");
}
String status;
String delFlag;
try {
Field statusField = obj.getClass().getDeclaredField("status");
statusField.setAccessible(true);
status= statusField.get(obj).toString();
Field delFlagField = obj.getClass().getDeclaredField("delFlag");
delFlagField.setAccessible(true);
delFlag = delFlagField.get(obj).toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
if (UserStatus.DELETED.getCode().equals(delFlag))
{
log.info("登录用户:{} 已被删除.", phone);
throw new BaseException("对不起,您的账号:" + phone + " 已被删除");
}
else if (UserStatus.DISABLE.getCode().equals(status))
{
log.info("登录用户:{} 已被停用.", phone);
throw new BaseException("对不起,您的账号:" + phone + " 已停用");
}
}
}

View File

@ -1,70 +0,0 @@
package com.ruoyi.framework.web.service;
import cn.hutool.core.lang.Validator;
import com.ruoyi.bgt.service.IBgtUserService;
import com.ruoyi.common.core.domain.entity.BgtUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
* 用户验证处理
*
* @author ruoyi
*/
@Service
public class BgtUserDetailsServiceImpl implements UserDetailsService
{
private static final Logger log = LoggerFactory.getLogger(BgtUserDetailsServiceImpl.class);
@Autowired
private IBgtUserService userService;
@Autowired
private SysPermissionService permissionService;
@Override
public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException
{
BgtUser user = userService.selectUserByPhone(phone);
if (Validator.isNull(user))
{
log.info("登录用户:{} 不存在.", phone);
throw new UsernameNotFoundException("登录用户:" + phone + " 不存在");
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", phone);
throw new BaseException("对不起,您的账号:" + phone + " 已被删除");
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", phone);
throw new BaseException("对不起,您的账号:" + phone + " 已停用");
}
return createLoginUser(user);
}
public UserDetails createLoginUser(BgtUser user)
{
SysUser sysUser = new SysUser();
sysUser.setPhonenumber(user.getPhone());
sysUser.setUserId(user.getUserId());
sysUser.setUserName(user.getUsername());
sysUser.setPassword(user.getPassword());
return new LoginUser(sysUser, null);
}
}