登录
This commit is contained in:
@ -3,6 +3,9 @@ package com.ruoyi.framework.config;
|
||||
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;
|
||||
@ -31,7 +34,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
||||
* 自定义用户认证逻辑
|
||||
*/
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
private AllUserDetailsServiceImpl userDetailsService;
|
||||
|
||||
/**
|
||||
* 认证失败处理类
|
||||
@ -101,7 +104,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
||||
// 过滤请求
|
||||
.authorizeRequests()
|
||||
// 对于登录login 验证码captchaImage 允许匿名访问
|
||||
.antMatchers("/login", "/captchaImage","/demo/tress/all").anonymous()
|
||||
.antMatchers("/login", "/bgt/login", "/captchaImage","/demo/tress/all").anonymous()
|
||||
.antMatchers(
|
||||
HttpMethod.GET,
|
||||
"/*.html",
|
||||
|
@ -0,0 +1,46 @@
|
||||
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;
|
||||
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 AllUserDetailsServiceImpl implements UserDetailsService
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(AllUserDetailsServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private UserDetailsServiceImpl userDetailsService;
|
||||
|
||||
@Autowired
|
||||
private BgtUserDetailsServiceImpl bgtUserDetailsService;
|
||||
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String var) throws UsernameNotFoundException
|
||||
{
|
||||
if(var.contains(Constants.BGT)){
|
||||
var = var.replace(Constants.BGT, "");
|
||||
return bgtUserDetailsService.loadUserByUsername(var);
|
||||
}else {
|
||||
return userDetailsService.loadUserByUsername(var);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
package com.ruoyi.framework.web.service;
|
||||
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.entity.BgtUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
import com.ruoyi.common.utils.ServletUtils;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 登录校验方法
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
public class BgtLoginService
|
||||
{
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Resource
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
// @Autowired
|
||||
// private RedisCache redisCache;
|
||||
|
||||
// @Autowired
|
||||
// private CaptchaProperties captchaProperties;
|
||||
|
||||
@Autowired
|
||||
private ISysUserService userService;
|
||||
|
||||
@Autowired
|
||||
private AsyncService asyncService;
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
*
|
||||
* @param phone 电话
|
||||
* @param password 密码
|
||||
* @param code 验证码
|
||||
* @param uuid 唯一标识
|
||||
* @return 结果
|
||||
*/
|
||||
public String login(String phone, String password, String code, String uuid)
|
||||
{
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
// 用户验证
|
||||
Authentication authentication = null;
|
||||
try
|
||||
{
|
||||
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
||||
authentication = authenticationManager
|
||||
.authenticate(new UsernamePasswordAuthenticationToken(Constants.BGT+phone, password));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e instanceof BadCredentialsException)
|
||||
{
|
||||
asyncService.recordLogininfor(phone, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
|
||||
throw new UserPasswordNotMatchException();
|
||||
}
|
||||
else
|
||||
{
|
||||
asyncService.recordLogininfor(phone, Constants.LOGIN_FAIL, e.getMessage(), request);
|
||||
throw new CustomException(e.getMessage());
|
||||
}
|
||||
}
|
||||
asyncService.recordLogininfor(phone, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
||||
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||
// recordLoginInfo(loginUser.getUser());
|
||||
// 生成token
|
||||
return tokenService.createToken(loginUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录登录信息
|
||||
*/
|
||||
public void recordLoginInfo(BgtUser user)
|
||||
{
|
||||
// user.(ServletUtils.getClientIP());
|
||||
// user.setLoginDate(DateUtils.getNowDate());
|
||||
// user.setUpdateBy(user.getUserName());
|
||||
// userService.updateUserProfile(user);
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user