feat():登录服务接口规范
parent
3ed79a5512
commit
c606722154
2
pom.xml
2
pom.xml
|
@ -4,7 +4,7 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.muyu</groupId>
|
<groupId>com.muyu</groupId>
|
||||||
<artifactId>cloud-server-parent</artifactId>
|
<artifactId>cloud-server-parent</artifactId>
|
||||||
<version>3.6.3</version>
|
<version>3.6.4</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package com.muyu.auth.controller;
|
package com.muyu.auth.controller;
|
||||||
|
|
||||||
import com.muyu.auth.form.LoginBody;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import com.muyu.auth.domain.TokenResp;
|
||||||
|
import com.muyu.auth.form.LoginUserReq;
|
||||||
import com.muyu.auth.form.RegisterBody;
|
import com.muyu.auth.form.RegisterBody;
|
||||||
import com.muyu.auth.service.SysLoginService;
|
import com.muyu.auth.service.SysLoginService;
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
|
@ -10,6 +12,8 @@ import com.muyu.common.security.auth.AuthUtil;
|
||||||
import com.muyu.common.security.service.TokenService;
|
import com.muyu.common.security.service.TokenService;
|
||||||
import com.muyu.common.security.utils.SecurityUtils;
|
import com.muyu.common.security.utils.SecurityUtils;
|
||||||
import com.muyu.common.system.domain.LoginUser;
|
import com.muyu.common.system.domain.LoginUser;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
@ -18,12 +22,15 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token 控制
|
* token 控制
|
||||||
*
|
*
|
||||||
* @author muyu
|
* @author muyu
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
|
@Tag(name="鉴权接口",description = "鉴权相关控制层")
|
||||||
public class TokenController {
|
public class TokenController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private TokenService tokenService;
|
private TokenService tokenService;
|
||||||
|
@ -32,14 +39,23 @@ public class TokenController {
|
||||||
private SysLoginService sysLoginService;
|
private SysLoginService sysLoginService;
|
||||||
|
|
||||||
@PostMapping("login")
|
@PostMapping("login")
|
||||||
public Result<?> login (@RequestBody LoginBody form) {
|
@Operation(summary = "登录接口",description = "登录鉴权接口")
|
||||||
|
public Result<TokenResp> login (@RequestBody LoginUserReq form) {
|
||||||
// 用户登录
|
// 用户登录
|
||||||
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
||||||
// 获取登录token
|
// 获取登录token
|
||||||
return Result.success(tokenService.createToken(userInfo));
|
Map<String, Object> tokenMap = tokenService.createToken(userInfo);
|
||||||
|
|
||||||
|
return Result.success(
|
||||||
|
TokenResp.builder()
|
||||||
|
.accessToken(Convert.toStr(tokenMap.get("access_token")))
|
||||||
|
.expiresIn(Convert.toLong(tokenMap.get("expires_in")))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("logout")
|
@DeleteMapping("logout")
|
||||||
|
@Operation(summary = "注销接口",description = "用户注销接口")
|
||||||
public Result<?> logout (HttpServletRequest request) {
|
public Result<?> logout (HttpServletRequest request) {
|
||||||
String token = SecurityUtils.getToken(request);
|
String token = SecurityUtils.getToken(request);
|
||||||
if (StringUtils.isNotEmpty(token)) {
|
if (StringUtils.isNotEmpty(token)) {
|
||||||
|
@ -53,6 +69,7 @@ public class TokenController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("refresh")
|
@PostMapping("refresh")
|
||||||
|
@Operation(summary = "用户登录时间刷新",description = "用户登录时间刷新接口,可以进行用户token续约")
|
||||||
public Result<?> refresh (HttpServletRequest request) {
|
public Result<?> refresh (HttpServletRequest request) {
|
||||||
LoginUser loginUser = tokenService.getLoginUser(request);
|
LoginUser loginUser = tokenService.getLoginUser(request);
|
||||||
if (StringUtils.isNotNull(loginUser)) {
|
if (StringUtils.isNotNull(loginUser)) {
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.muyu.auth.domain;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:liuxinyue
|
||||||
|
* @Package:com.muyu.auth.domain
|
||||||
|
* @Project:cloud-auth
|
||||||
|
* @name:TokenResp
|
||||||
|
* @Date:2024/8/8 21:28
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Tag(name="token响应结果集",description = "包含token和token有效时间")
|
||||||
|
public class TokenResp {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token令牌
|
||||||
|
*/
|
||||||
|
@Schema(
|
||||||
|
title = "token令牌",
|
||||||
|
type = "String",
|
||||||
|
defaultValue = "ey.eyJjoiTVVZVSJ9.bLQLgvFA",
|
||||||
|
description = "JWT令牌,包含头,载体,尾"
|
||||||
|
)
|
||||||
|
private String accessToken;
|
||||||
|
/**
|
||||||
|
* 有效时间
|
||||||
|
*/
|
||||||
|
@Schema(
|
||||||
|
title = "令牌有效时间",
|
||||||
|
type = "Long",
|
||||||
|
defaultValue = "720",
|
||||||
|
description = "过期时间,时间单位为分钟(MIN)"
|
||||||
|
)
|
||||||
|
private Long expiresIn;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,34 +0,0 @@
|
||||||
package com.muyu.auth.form;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户登录对象
|
|
||||||
*
|
|
||||||
* @author muyu
|
|
||||||
*/
|
|
||||||
public class LoginBody {
|
|
||||||
/**
|
|
||||||
* 用户名
|
|
||||||
*/
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户密码
|
|
||||||
*/
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.muyu.auth.form;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录对象
|
||||||
|
*
|
||||||
|
* @author muyu
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Tag(name = "登录请求实体类")
|
||||||
|
public class LoginUserReq {
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
@Schema(
|
||||||
|
title = "用户名称",
|
||||||
|
type = "String",
|
||||||
|
defaultValue = "admin",
|
||||||
|
description = "登录用户名称"
|
||||||
|
)
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户密码
|
||||||
|
*/
|
||||||
|
@Schema(
|
||||||
|
title = "用户密码",
|
||||||
|
type = "String",
|
||||||
|
defaultValue = "admin123",
|
||||||
|
description = "登录用户名称密码"
|
||||||
|
)
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
}
|
|
@ -5,6 +5,6 @@ package com.muyu.auth.form;
|
||||||
*
|
*
|
||||||
* @author muyu
|
* @author muyu
|
||||||
*/
|
*/
|
||||||
public class RegisterBody extends LoginBody {
|
public class RegisterBody extends LoginUserReq {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue