tfa整理

This commit is contained in:
Dftre 2025-03-09 21:49:18 +08:00
parent 061268be69
commit d9c06d9bf1
12 changed files with 97 additions and 134 deletions

View File

@ -24,14 +24,14 @@
"dbcode.connections": [
{
"connectionId": "btr-_nFe7R0oOvCj0mMun",
"name": "ry",
"name": "ry-mysql",
"driver": "mysql",
"connectionType": "host",
"host": "127.0.0.1",
"port": 3306,
"ssl": false,
"username": "root",
"password": "",
"password": "123456",
"savePassword": "secretStorage",
"database": "ry",
"connectionTimeout": 30,
@ -41,7 +41,7 @@
},
{
"connectionId": "7NX2UhXl__9t3Ca6TzEsB",
"name": "ry",
"name": "ry-postgres",
"driver": "postgres",
"connectionType": "host",
"host": "127.0.0.1",

View File

@ -0,0 +1,14 @@
package com.ruoyi.auth.common.service;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
public interface TfaService {
public void doBind(LoginBody loginBody);
public void doBindVerify(LoginBody loginBody);
public void doRegister(RegisterBody registerBody);
public void doRegisterVerify(RegisterBody registerBody);
}

View File

@ -1,4 +1,4 @@
package com.ruoyi.auth.common.controller;
package com.ruoyi.auth.controller;
import java.util.List;
@ -35,8 +35,7 @@ import jakarta.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/system/oauth")
@Tag(name = "【第三方认证】管理")
public class OauthUserController extends BaseController
{
public class OauthUserController extends BaseController {
@Autowired
private IOauthUserService oauthUserService;
@ -46,8 +45,7 @@ public class OauthUserController extends BaseController
@Operation(summary = "查询第三方认证列表")
@PreAuthorize("@ss.hasPermi('system:oauth:list')")
@GetMapping("/list")
public TableDataInfo list(OauthUser oauthUser)
{
public TableDataInfo list(OauthUser oauthUser) {
startPage();
List<OauthUser> list = oauthUserService.selectOauthUserList(oauthUser);
return getDataTable(list);
@ -60,8 +58,7 @@ public class OauthUserController extends BaseController
@PreAuthorize("@ss.hasPermi('system:oauth:export')")
@Log(title = "第三方认证", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, OauthUser oauthUser)
{
public void export(HttpServletResponse response, OauthUser oauthUser) {
List<OauthUser> list = oauthUserService.selectOauthUserList(oauthUser);
ExcelUtil<OauthUser> util = new ExcelUtil<OauthUser>(OauthUser.class);
util.exportExcel(response, list, "第三方认证数据");
@ -73,8 +70,7 @@ public class OauthUserController extends BaseController
@Operation(summary = "获取第三方认证详细信息")
@PreAuthorize("@ss.hasPermi('system:oauth:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
public AjaxResult getInfo(@PathVariable("id") Long id) {
return success(oauthUserService.selectOauthUserById(id));
}
@ -85,8 +81,7 @@ public class OauthUserController extends BaseController
@PreAuthorize("@ss.hasPermi('system:oauth:add')")
@Log(title = "第三方认证", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody OauthUser oauthUser)
{
public AjaxResult add(@RequestBody OauthUser oauthUser) {
return toAjax(oauthUserService.insertOauthUser(oauthUser));
}
@ -97,8 +92,7 @@ public class OauthUserController extends BaseController
@PreAuthorize("@ss.hasPermi('system:oauth:edit')")
@Log(title = "第三方认证", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody OauthUser oauthUser)
{
public AjaxResult edit(@RequestBody OauthUser oauthUser) {
return toAjax(oauthUserService.updateOauthUser(oauthUser));
}
@ -108,9 +102,8 @@ public class OauthUserController extends BaseController
@Operation(summary = "删除第三方认证")
@PreAuthorize("@ss.hasPermi('system:oauth:remove')")
@Log(title = "第三方认证", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids)
{
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable(name = "ids") Long[] ids) {
return toAjax(oauthUserService.deleteOauthUserByIds(ids));
}
}

View File

@ -0,0 +1,55 @@
package com.ruoyi.auth.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.auth.common.service.TfaService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
@RestController
@RequestMapping("/auth/<channel>") // dySms mail
public class TfaController extends BaseController {
@Autowired
Map<String, TfaService> tfaServiceMap;
@PostMapping("/send/bind")
public AjaxResult send(@PathVariable String channel, @RequestBody LoginBody loginBody) {
TfaService tfaService = tfaServiceMap.get(channel + "AuthService");
tfaService.doBind(loginBody);
return success();
}
@PostMapping("/verify/bind") // 发送验证码
public AjaxResult verify(@PathVariable String channel, @RequestBody LoginBody loginBody) {
TfaService tfaService = tfaServiceMap.get(channel + "AuthService");
tfaService.doBindVerify(loginBody);
return success();
}
@PostMapping("/send/register")
@Anonymous
public AjaxResult sendRegister(@PathVariable String channel, @RequestBody RegisterBody registerBody) {
TfaService tfaService = tfaServiceMap.get(channel + "AuthService");
tfaService.doRegister(registerBody);
return success();
}
@PostMapping("/verify/register")
@Anonymous
public AjaxResult verifyRegister(@PathVariable String channel, @RequestBody RegisterBody registerBody) {
TfaService tfaService = tfaServiceMap.get(channel + "AuthService");
tfaService.doRegisterVerify(registerBody);
return success();
}
}

View File

@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.auth.common.domain.OauthUser;
import com.ruoyi.auth.common.service.IOauthUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
@ -25,9 +27,7 @@ import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.auth.common.domain.OauthUser;
import com.ruoyi.auth.common.service.IOauthUserService;
import com.ruoyi.oauth.justauth.utils.AuthUtils;
import com.ruoyi.oauth.justauth.utils.JustAuthUtils;
import com.ruoyi.system.service.ISysUserService;
import jakarta.servlet.http.HttpServletRequest;
@ -91,7 +91,7 @@ public class SysAuthController extends BaseController
return error(source + "平台账号暂不支持");
}
JSONObject json = JSONObject.parseObject(obj);
AuthRequest authRequest = AuthUtils.getAuthRequest(source, json.getString("clientId"), json.getString("clientSecret"), json.getString("redirectUri"), authStateCache);
AuthRequest authRequest = JustAuthUtils.getAuthRequest(source, json.getString("clientId"), json.getString("clientSecret"), json.getString("redirectUri"), authStateCache);
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
return success(authorizeUrl);
}
@ -114,7 +114,7 @@ public class SysAuthController extends BaseController
return AjaxResult.error(10002, "第三方平台系统不支持或未提供来源");
}
JSONObject json = JSONObject.parseObject(obj);
AuthRequest authRequest = AuthUtils.getAuthRequest(source, json.getString("clientId"), json.getString("clientSecret"), json.getString("redirectUri"), authStateCache);
AuthRequest authRequest = JustAuthUtils.getAuthRequest(source, json.getString("clientId"), json.getString("clientSecret"), json.getString("redirectUri"), authStateCache);
AuthResponse<AuthUser> response = authRequest.login(callback);
if (response.ok())
{

View File

@ -38,7 +38,7 @@ import me.zhyd.oauth.request.AuthWeiboRequest;
*
* @author ruoyi
*/
public class AuthUtils
public class JustAuthUtils
{
@SuppressWarnings("deprecation")
public static AuthRequest getAuthRequest(String source, String clientId, String clientSecret, String redirectUri,

View File

@ -1,48 +0,0 @@
package com.ruoyi.tfa.email.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.tfa.email.service.impl.MailServiceImpl;
@RestController
@RequestMapping("/auth/mail")
public class MailAuthController extends BaseController {
@Autowired
MailServiceImpl serviceImpl;
@PostMapping("/send/bind") // 发送验证码
public AjaxResult send(@RequestBody LoginBody loginBody) {
serviceImpl.doBind(loginBody);
return success();
}
@PostMapping("/verify/bind") // 发送验证码
public AjaxResult verify(@RequestBody LoginBody loginBody) {
serviceImpl.doBindVerify(loginBody);
return success();
}
@PostMapping("/send/register")
@Anonymous
public AjaxResult sendRegister(@RequestBody RegisterBody registerBody) {
serviceImpl.doRegister(registerBody);
return success();
}
@PostMapping("/verify/register")
@Anonymous
public AjaxResult verifyRegister(@RequestBody RegisterBody registerBody) {
serviceImpl.doRegisterVerify(registerBody);
return success();
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.tfa.email.service;
import com.ruoyi.auth.common.service.OauthVerificationCodeService;
import com.ruoyi.auth.common.service.TfaService;
public interface IMailService extends OauthVerificationCodeService {
public interface IMailService extends OauthVerificationCodeService,TfaService {
}

View File

@ -24,7 +24,7 @@ import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.tfa.email.service.IMailService;
import com.ruoyi.tfa.email.utils.EmailUtil;
@Service
@Service("mailAuthService")
public class MailServiceImpl implements IMailService {
@Autowired

View File

@ -1,53 +0,0 @@
package com.ruoyi.tfa.phone.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.tfa.phone.service.Impl.DySmsServiceImpl;
/**
* 手机号认证Controller
*
* @author zlh
* @date 2024-04-16
*/
@RestController
@RequestMapping("/auth/phone")
public class DySmsAuthController extends BaseController {
@Autowired
public DySmsServiceImpl dySmsService;
@PostMapping("/send/bind") // 发送验证码
public AjaxResult send(@RequestBody LoginBody loginBody) {
dySmsService.doBind(loginBody);
return success();
}
@PostMapping("/verify/bind") // 发送验证码
public AjaxResult verify(@RequestBody LoginBody loginBody) {
dySmsService.doBindVerify(loginBody);
return success();
}
@PostMapping("/send/register")
@Anonymous
public AjaxResult sendRegister(@RequestBody RegisterBody registerBody) {
dySmsService.doRegister(registerBody);
return success();
}
@PostMapping("/verify/register")
@Anonymous
public AjaxResult verifyRegister(@RequestBody RegisterBody registerBody) {
dySmsService.doRegisterVerify(registerBody);
return success();
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.tfa.phone.service;
import com.ruoyi.auth.common.service.OauthVerificationCodeService;
import com.ruoyi.auth.common.service.TfaService;
/**
* 手机号认证Servcie
@ -8,6 +9,6 @@ import com.ruoyi.auth.common.service.OauthVerificationCodeService;
* @author zlh
* @date 2024-04-16
*/
public interface DySmsService extends OauthVerificationCodeService {
public interface DySmsService extends OauthVerificationCodeService, TfaService {
}

View File

@ -32,7 +32,7 @@ import com.ruoyi.tfa.phone.utils.DySmsUtil;
* @author zlh
* @date 2024-04-16
*/
@Service
@Service("dySmsAuthService")
public class DySmsServiceImpl implements DySmsService {
@Autowired
@ -114,14 +114,14 @@ public class DySmsServiceImpl implements DySmsService {
}
}
public boolean doRegisterVerify(RegisterBody registerBody) {
public void doRegisterVerify(RegisterBody registerBody) {
if (checkCode(registerBody.getPhonenumber(), registerBody.getCode(), OauthVerificationUse.REGISTER)) {
SysUser sysUser = new SysUser();
sysUser.setUserName(registerBody.getPhonenumber());
sysUser.setNickName(registerBody.getUsername());
sysUser.setPassword(SecurityUtils.encryptPassword(registerBody.getPassword()));
sysUser.setPhonenumber(registerBody.getPhonenumber());
return userService.registerUser(sysUser);
userService.registerUser(sysUser);
} else {
throw new ServiceException("验证码错误");
}
@ -158,14 +158,14 @@ public class DySmsServiceImpl implements DySmsService {
sendCode(loginBody.getPhonenumber(), RandomCodeUtil.numberCode(6), OauthVerificationUse.BIND);
}
public int doBindVerify(LoginBody loginBody) {
public void doBindVerify(LoginBody loginBody) {
if (checkCode(loginBody.getPhonenumber(), loginBody.getCode(), OauthVerificationUse.BIND)) {
SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
if (!SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword())) {
throw new ServiceException("密码错误");
}
sysUser.setPhonenumber(loginBody.getPhonenumber());
return userService.updateUser(sysUser);
userService.updateUser(sysUser);
} else {
throw new ServiceException("验证码错误");
}