修改登录

This commit is contained in:
haozq 2025-09-03 14:37:23 +08:00
parent 7c1bb7511b
commit 9a61aeb568
33 changed files with 339 additions and 76 deletions

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.common;
package com.bonus.base.controller.common;
import java.awt.image.BufferedImage;
import java.io.IOException;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.common;
package com.bonus.base.controller.common;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.monitor;
package com.bonus.base.controller.monitor;
import java.util.ArrayList;
import java.util.Collection;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.monitor;
package com.bonus.base.controller.monitor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.monitor;
package com.bonus.base.controller.monitor;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.monitor;
package com.bonus.base.controller.monitor;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.monitor;
package com.bonus.base.controller.monitor;
import java.util.ArrayList;
import java.util.Collection;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

View File

@ -1,7 +1,8 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -39,6 +40,16 @@ public class SysLoginController
@Autowired
private TokenService tokenService;
@PostMapping("/sendPhone")
public AjaxResult sendPhone(@RequestBody String phone) {
// 下发短信
AjaxResult ajax= loginService.sendPhone(phone);
return ajax;
}
/**
* 登录方法
*
@ -46,12 +57,18 @@ public class SysLoginController
* @return 结果
*/
@PostMapping("/login")
public AjaxResult login(@RequestBody LoginBody loginBody)
{
public AjaxResult login(@RequestBody LoginBody loginBody) {
String token;
AjaxResult ajax = AjaxResult.success();
if("2".equals(loginBody.getLoginType())){
token = loginService.login(loginBody.getUsername(), loginBody.getCode());
}else{
token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
loginBody.getUuid());
}
// 生成令牌
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
loginBody.getUuid());
ajax.put(Constants.TOKEN, token);
return ajax;
}

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.system;
package com.bonus.base.controller.system;
import java.util.List;
import java.util.stream.Collectors;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.controller.tool;
package com.bonus.base.controller.tool;
import java.util.ArrayList;
import java.util.LinkedHashMap;

View File

@ -1,4 +1,4 @@
package com.bonus.waterdesign.core.config;
package com.bonus.base.core.config;
import java.util.ArrayList;
import java.util.List;

View File

@ -16,7 +16,11 @@
</description>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<!-- Spring框架基本的核心工具 -->
<dependency>
<groupId>org.springframework</groupId>
@ -118,6 +122,11 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -1,12 +1,20 @@
package com.bonus.common.core.domain.model;
import lombok.Data;
/**
* 用户登录对象
*
* @author ruoyi
*/
public class LoginBody
{
@Data
public class LoginBody {
/**
* 登录方式
* 1账号密码登录
* 2验证码登录
*/
private String loginType;
/**
* 用户名
*/
@ -21,49 +29,10 @@ public class LoginBody
* 验证码
*/
private String code;
/**
* 唯一标识
*/
private String uuid;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
public String getUuid()
{
return uuid;
}
public void setUuid(String uuid)
{
this.uuid = uuid;
}
}

View File

@ -0,0 +1,102 @@
package com.bonus.common.utils;
import cn.hutool.http.HttpRequest;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.hibernate.validator.internal.util.StringHelper;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* 第三方依赖
* <dependency>
* <groupId>cn.hutool</groupId>
* <artifactId>hutool-all</artifactId>
* <version>5.8.22</version>
* </dependency>
* 手机 短信 下发工具类
* @author 黑子
*/
public class PhoneUtils {
/**
* 短信验证码 发送地址 及账号
*/
public static String url="http://api.ktsms.cn/sms_token?ddtkey=bonus&secretkey=KtyBns@Admin2023!";
public static final String STRING_OK = "ok";
/**
* 验证码时长
* 分钟
*/
public static final String TIMES = "5";
/**
* 系统平台名称
*/
public static final String SYSTEM_NAME = "博诺思公共微服务平台";
/**
* 短信签名
*/
public static final String PHONE_HEAD = "【博诺思】";
/**
* 发送短信验证吗
* @param phone 手机号码
* @param msg 消息内容
* 如果msg不传 会使用默认值
* @return
*/
public static Map<String,String> sendPhoneMsg(String phone, String msg){
Map<String,String> map=new HashMap<>(4);
map.put("phone",phone);
if (!isValidPhoneNumber(phone)) {
map.put("code","201");
map.put("msg","手机号格式错误请输入11位数字号码");
}
StringBuilder sb=new StringBuilder();
sb.append(url).append("&mobile=").append(phone);
sb.append("&content=");
if(StringHelper.isNullOrEmptyString(msg)){
String code = getSixBitCode();
map.put("captcha",code);
sb.append(PHONE_HEAD);
sb.append("您正在进行短信验证,验证码:").append(code).append(",请在").append(TIMES).append("分钟内完成验证,切勿将验证码泄漏于他人。");
map.put("times",TIMES);
sb.append("发送时间:").append(DateUtils.getTime()).append("。(");
sb.append(SYSTEM_NAME).append("");
}else{
sb.append(msg);
}
String body = HttpRequest.post(sb.toString()).execute(false).body();
if (body == null || !body.contains(STRING_OK)) {
map.put("code","201");
map.put("msg","短信发送失败");
}else{
; map.put("code","200");
map.put("msg","发送成功");
}
return map;
}
public static boolean isValidPhoneNumber(String phoneNumber) {
// 定义中国的手机号正则表达式
String regex = "^1[3-9]\\d{9}$";
return phoneNumber.matches(regex);
}
private static String getSixBitCode() {
//随机数
Random random = new Random();
return String.valueOf(random.nextInt(900000) + 100000);
}
}

View File

@ -111,7 +111,7 @@ public class SecurityConfig
.authorizeHttpRequests((requests) -> {
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
requests.antMatchers("/login", "/sendPhone","/register", "/captchaImage").permitAll()
// 静态资源可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

View File

@ -0,0 +1,61 @@
package com.bonus.framework.web.service;
import com.bonus.common.core.domain.entity.SysUser;
import com.bonus.common.core.domain.model.LoginUser;
import com.bonus.common.enums.UserStatus;
import com.bonus.common.exception.ServiceException;
import com.bonus.common.utils.MessageUtils;
import com.bonus.common.utils.StringUtils;
import com.bonus.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.UsernameNotFoundException;
import org.springframework.stereotype.Service;
/**
* @author 黑子
* 用户登录
*/
@Service
public class LoginUserService {
private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
@Autowired
private ISysUserService userService;
@Autowired
private SysPasswordService passwordService;
@Autowired
private SysPermissionService permissionService;
public LoginUser loadUserByUsername(String username) throws UsernameNotFoundException
{
SysUser user = userService.selectUserByUserName(username);
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("登录用户不存在"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("登录用户已被删除"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("登录用户已被删除"));
}
passwordService.validate(user);
return createLoginUser(user);
}
public LoginUser createLoginUser(SysUser user)
{
return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
}
}

View File

@ -1,11 +1,15 @@
package com.bonus.framework.web.service;
import javax.annotation.Resource;
import com.bonus.common.core.domain.AjaxResult;
import com.bonus.common.utils.PhoneUtils;
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.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import com.bonus.common.constant.CacheConstants;
import com.bonus.common.constant.Constants;
@ -29,6 +33,10 @@ import com.bonus.framework.security.context.AuthenticationContextHolder;
import com.bonus.system.service.ISysConfigService;
import com.bonus.system.service.ISysUserService;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* 登录校验方法
*
@ -52,6 +60,11 @@ public class SysLoginService
@Autowired
private ISysConfigService configService;
@Autowired
LoginUserService loginUserService;
public static Integer TIMES=2;
/**
* 登录验证
*
@ -95,11 +108,39 @@ public class SysLoginService
}
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//记录登录信息
recordLoginInfo(loginUser.getUserId());
// 生成token
return tokenService.createToken(loginUser);
}
/**
* 手机验证码登录
* @param code
* @return
*/
public String login(String phone, String code) {
if(StringUtils.isEmpty(phone)){
throw new ServiceException("请输入正确的手机号!");
}
if(StringUtils.isEmpty(code)){
throw new ServiceException("验证码不正确!");
}
String data=redisCache.getCacheObject("code_"+phone);
if(StringUtils.isEmpty(data)){
throw new ServiceException("验证码已过期!");
}else {
if(data.equals(code)){
throw new ServiceException("验证码不正确!");
}
}
//执行登录方法
LoginUser loginUser= loginUserService.loadUserByUsername(phone);
////记录登录信息
recordLoginInfo(loginUser.getUserId());
// 生成token
return tokenService.createToken(loginUser);
}
/**
* 校验验证码
*
@ -178,4 +219,45 @@ public class SysLoginService
sysUser.setLoginDate(DateUtils.getNowDate());
userService.updateUserProfile(sysUser);
}
/**
* 下发短信
* @param phone
* @return
*/
public AjaxResult sendPhone(String phone) {
//验证手机手机号是否存在
String thisUser=userService.getUserInfo(phone);
if(!phone.equals(thisUser)){
AjaxResult ajax = AjaxResult.error();
ajax.put("msg", "手机号不存在或手机号不正确");
return ajax;
}
StringBuilder sb=new StringBuilder();
String code=getSixBitCode();
sb.append("【博诺思】验证码:").append(code).append(",验证码有效期").append(TIMES).append("分钟,切勿将验证码泄漏于他人。");
sb.append("发送时间:").append(DateUtils.getTime());
sb.append("。(安徽博诺思公共微服务平台)");
Map<String,String> map= PhoneUtils.sendPhoneMsg(phone,sb.toString());
if ("200".equals(map.get("code"))){
AjaxResult ajax = AjaxResult.success();
redisCache.setCacheObject("code_"+phone,code,TIMES, TimeUnit.MINUTES);
ajax.put("times", TIMES);
ajax.put("msg", "发送成功");
return ajax;
}else{
AjaxResult ajax = AjaxResult.error();
ajax.put("times", TIMES);
ajax.put("msg", "发送失败,请检查网络!");
return ajax;
}
}
private static String getSixBitCode() {
//随机数
Random random = new Random();
return String.valueOf(random.nextInt(900000) + 100000);
}
}

View File

@ -41,21 +41,20 @@ public class UserDetailsServiceImpl implements UserDetailsService
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("user.not.exists"));
throw new ServiceException(MessageUtils.message("登录用户不存在"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("user.password.delete"));
throw new ServiceException(MessageUtils.message("登录用户已被删除"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("user.blocked"));
throw new ServiceException(MessageUtils.message("登录用户已被删除"));
}
passwordService.validate(user);
return createLoginUser(user);
}

View File

@ -124,4 +124,11 @@ public interface SysUserMapper
* @return 结果
*/
public SysUser checkEmailUnique(String email);
/**
* 查询人员信息数据
* @param phone
*/
String getUserInfo(@Param("phone") String phone);
}

View File

@ -203,4 +203,6 @@ public interface ISysUserService
* @return 结果
*/
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName);
String getUserInfo(String phone);
}

View File

@ -547,4 +547,16 @@ public class SysUserServiceImpl implements ISysUserService
}
return successMsg.toString();
}
@Override
public String getUserInfo(String phone) {
try{
return userMapper.getUserInfo(phone);
}catch (Exception e){
log.error(e.toString(), e);
}
return null;
}
}

View File

@ -122,7 +122,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
where u.phonenumber = #{userName} and u.del_flag = '0'
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
@ -141,8 +141,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="checkEmailUnique" parameterType="String" resultMap="SysUserResult">
select user_id, email from sys_user where email = #{email} and del_flag = '0' limit 1
</select>
<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
<select id="getUserInfo" resultType="String">
select phonenumber from sys_user where phonenumber = #{phone} and del_flag = '0' limit 1
</select>
<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
insert into sys_user(
<if test="userId != null and userId != 0">user_id,</if>
<if test="deptId != null and deptId != 0">dept_id,</if>