登录
This commit is contained in:
@ -1,9 +1,9 @@
|
|||||||
package com.ruoyi.web.controller.bgt;
|
package com.ruoyi.web.controller.common;
|
||||||
|
|
||||||
import com.ruoyi.common.constant.Constants;
|
import com.ruoyi.common.constant.Constants;
|
||||||
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
import com.ruoyi.common.core.domain.model.BgtLoginBody;
|
import com.ruoyi.common.core.domain.model.BgtLoginBody;
|
||||||
import com.ruoyi.framework.web.service.BgtLoginService;
|
import com.ruoyi.framework.web.service.AppLoginService;
|
||||||
import com.ruoyi.framework.web.service.SysPermissionService;
|
import com.ruoyi.framework.web.service.SysPermissionService;
|
||||||
import com.ruoyi.framework.web.service.TokenService;
|
import com.ruoyi.framework.web.service.TokenService;
|
||||||
import com.ruoyi.system.service.ISysMenuService;
|
import com.ruoyi.system.service.ISysMenuService;
|
||||||
@ -22,10 +22,10 @@ import java.util.Map;
|
|||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class BgtLoginController
|
public class AppLoginController
|
||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
private BgtLoginService loginService;
|
private AppLoginService loginService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysMenuService menuService;
|
private ISysMenuService menuService;
|
||||||
@ -42,13 +42,13 @@ public class BgtLoginController
|
|||||||
* @param loginBody 登录信息
|
* @param loginBody 登录信息
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@PostMapping("/bgt/login")
|
@PostMapping("/app/login")
|
||||||
public AjaxResult login(@RequestBody BgtLoginBody loginBody)
|
public AjaxResult login(@RequestBody BgtLoginBody loginBody)
|
||||||
{
|
{
|
||||||
Map<String,Object> ajax = new HashMap<>();
|
Map<String,Object> ajax = new HashMap<>();
|
||||||
// 生成令牌
|
// 生成令牌
|
||||||
String token = loginService.login(loginBody.getPhone(), loginBody.getPassword(), loginBody.getCode(),
|
String token = loginService.login(loginBody.getPhone(), loginBody.getPassword(), loginBody.getCode(),
|
||||||
loginBody.getUuid());
|
loginBody.getUuid(),loginBody.getUserTypePrefix());
|
||||||
ajax.put(Constants.TOKEN, token);
|
ajax.put(Constants.TOKEN, token);
|
||||||
return AjaxResult.success(ajax);
|
return AjaxResult.success(ajax);
|
||||||
}
|
}
|
@ -140,5 +140,10 @@ public class Constants
|
|||||||
/**
|
/**
|
||||||
* 包工头用户标志 前缀
|
* 包工头用户标志 前缀
|
||||||
*/
|
*/
|
||||||
public static final String BGT = "bgt_:";
|
public static final String BGT = "bgt_";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 务工者用户标志 前缀
|
||||||
|
*/
|
||||||
|
public static final String WGZ = "wgz_";
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package com.ruoyi.common.core.domain.entity;
|
package com.ruoyi.common.core.domain.entity;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
|
||||||
import com.ruoyi.common.annotation.Excel;
|
import com.ruoyi.common.annotation.Excel;
|
||||||
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModel;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
@ -31,8 +28,8 @@ public class BgtUser implements Serializable {
|
|||||||
|
|
||||||
/** 主键ID */
|
/** 主键ID */
|
||||||
@ApiModelProperty("主键ID")
|
@ApiModelProperty("主键ID")
|
||||||
@TableId(value = "id")
|
@TableId(value = "id",type = IdType.AUTO)
|
||||||
private String id;
|
private Long id;
|
||||||
|
|
||||||
/** 唯一标识 */
|
/** 唯一标识 */
|
||||||
@Excel(name = "唯一标识")
|
@Excel(name = "唯一标识")
|
||||||
|
@ -35,4 +35,9 @@ public class BgtLoginBody
|
|||||||
*/
|
*/
|
||||||
private String uuid = "";
|
private String uuid = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户类型前缀
|
||||||
|
*/
|
||||||
|
private String userTypePrefix;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,6 @@ import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
|
|||||||
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
|
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
|
||||||
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
|
||||||
import com.ruoyi.framework.web.service.AllUserDetailsServiceImpl;
|
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 de.codecentric.boot.admin.server.config.AdminServerProperties;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
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.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
import org.springframework.security.web.authentication.logout.LogoutFilter;
|
||||||
@ -104,7 +101,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
|
|||||||
// 过滤请求
|
// 过滤请求
|
||||||
.authorizeRequests()
|
.authorizeRequests()
|
||||||
// 对于登录login 验证码captchaImage 允许匿名访问
|
// 对于登录login 验证码captchaImage 允许匿名访问
|
||||||
.antMatchers("/login", "/bgt/login", "/captchaImage","/demo/tress/all").anonymous()
|
.antMatchers("/login", "/app/login", "/captchaImage","/demo/tress/all").anonymous()
|
||||||
.antMatchers(
|
.antMatchers(
|
||||||
HttpMethod.GET,
|
HttpMethod.GET,
|
||||||
"/*.html",
|
"/*.html",
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
package com.ruoyi.framework.web.service;
|
package com.ruoyi.framework.web.service;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Validator;
|
|
||||||
import com.ruoyi.common.constant.Constants;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -29,16 +23,15 @@ public class AllUserDetailsServiceImpl implements UserDetailsService
|
|||||||
private UserDetailsServiceImpl userDetailsService;
|
private UserDetailsServiceImpl userDetailsService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BgtUserDetailsServiceImpl bgtUserDetailsService;
|
private AppUserDetailsServiceImpl appUserDetailsService;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String var) throws UsernameNotFoundException
|
public UserDetails loadUserByUsername(String var) throws UsernameNotFoundException
|
||||||
{
|
{
|
||||||
if(var.contains(Constants.BGT)){
|
if(var.contains(Constants.BGT)||var.contains(Constants.WGZ)){
|
||||||
var = var.replace(Constants.BGT, "");
|
return appUserDetailsService.loadUserByUsername(var);
|
||||||
return bgtUserDetailsService.loadUserByUsername(var);
|
} else {
|
||||||
}else {
|
|
||||||
return userDetailsService.loadUserByUsername(var);
|
return userDetailsService.loadUserByUsername(var);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class BgtLoginService
|
public class AppLoginService
|
||||||
{
|
{
|
||||||
@Autowired
|
@Autowired
|
||||||
private TokenService tokenService;
|
private TokenService tokenService;
|
||||||
@ -38,8 +38,8 @@ public class BgtLoginService
|
|||||||
// @Autowired
|
// @Autowired
|
||||||
// private CaptchaProperties captchaProperties;
|
// private CaptchaProperties captchaProperties;
|
||||||
|
|
||||||
@Autowired
|
// @Autowired
|
||||||
private ISysUserService userService;
|
// private ISysUserService userService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AsyncService asyncService;
|
private AsyncService asyncService;
|
||||||
@ -53,7 +53,7 @@ public class BgtLoginService
|
|||||||
* @param uuid 唯一标识
|
* @param uuid 唯一标识
|
||||||
* @return 结果
|
* @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();
|
HttpServletRequest request = ServletUtils.getRequest();
|
||||||
// 用户验证
|
// 用户验证
|
||||||
@ -62,7 +62,7 @@ public class BgtLoginService
|
|||||||
{
|
{
|
||||||
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
||||||
authentication = authenticationManager
|
authentication = authenticationManager
|
||||||
.authenticate(new UsernamePasswordAuthenticationToken(Constants.BGT+phone, password));
|
.authenticate(new UsernamePasswordAuthenticationToken(userTypePrefix+phone, password));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
@ -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 + " 已停用");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user