feat: register logic refactor
This commit is contained in:
45
pom.xml
45
pom.xml
@@ -13,19 +13,6 @@
|
|||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>myplayer-backend</name>
|
<name>myplayer-backend</name>
|
||||||
<description>myplayer-backend</description>
|
<description>myplayer-backend</description>
|
||||||
<url/>
|
|
||||||
<licenses>
|
|
||||||
<license/>
|
|
||||||
</licenses>
|
|
||||||
<developers>
|
|
||||||
<developer/>
|
|
||||||
</developers>
|
|
||||||
<scm>
|
|
||||||
<connection/>
|
|
||||||
<developerConnection/>
|
|
||||||
<tag/>
|
|
||||||
<url/>
|
|
||||||
</scm>
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -64,7 +51,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
<optional>true</optional>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
@@ -105,11 +92,21 @@
|
|||||||
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||||
<version>3.5.11</version>
|
<version>3.5.11</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis-spring</artifactId>
|
||||||
|
<version>3.0.4</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.jsonwebtoken</groupId>
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
<artifactId>jjwt</artifactId>
|
<artifactId>jjwt</artifactId>
|
||||||
<version>0.12.6</version>
|
<version>0.12.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springdoc</groupId>
|
||||||
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
|
<version>2.8.14</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -122,29 +119,9 @@
|
|||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<annotationProcessorPaths>
|
|
||||||
<path>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
</path>
|
|
||||||
</annotationProcessorPaths>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
<configuration>
|
|
||||||
<excludes>
|
|
||||||
<exclude>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
</exclude>
|
|
||||||
</excludes>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ package xin.merlin.myplayerbackend;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import xin.merlin.myplayerbackend.config.security.JwtProperties;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableConfigurationProperties(JwtProperties.class)
|
||||||
public class MyplayerBackendApplication {
|
public class MyplayerBackendApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package xin.merlin.myplayerbackend.config.security;
|
||||||
|
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||||
|
@Override
|
||||||
|
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
|
||||||
|
String message = "认证失败";
|
||||||
|
if (exception instanceof InsufficientAuthenticationException) {
|
||||||
|
message = "未提供认证信息";
|
||||||
|
}
|
||||||
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
||||||
|
response.setContentType("application/json;charset=utf-8");
|
||||||
|
response.getWriter().write(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,12 +35,12 @@ public class JWTAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!jwtUtil.isTokenExpired(token)) {
|
if (!jwtUtil.isTokenExpired(token)) {
|
||||||
System.out.println(token);
|
System.out.println("token expired: " + token);
|
||||||
String username = jwtUtil.getUAccount(token);
|
String account = jwtUtil.getAccount(token);
|
||||||
|
|
||||||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
if (account != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||||
UsernamePasswordAuthenticationToken authenticationToken =
|
UsernamePasswordAuthenticationToken authenticationToken =
|
||||||
new UsernamePasswordAuthenticationToken(username, null, Collections.emptyList());
|
new UsernamePasswordAuthenticationToken(account, null, Collections.emptyList());
|
||||||
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package xin.merlin.myplayerbackend.config.security;
|
package xin.merlin.myplayerbackend.config.security;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
@@ -22,21 +21,23 @@ public class SecurityConfig {
|
|||||||
private final JWTAuthenticationFilter jwtAuthenticationFilter;
|
private final JWTAuthenticationFilter jwtAuthenticationFilter;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http,CustomAuthenticationEntryPoint entryPoint) throws Exception {
|
||||||
http
|
http
|
||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(authz -> authz
|
.authorizeHttpRequests(authz -> authz
|
||||||
.requestMatchers(
|
.requestMatchers(
|
||||||
|
"/error",
|
||||||
"/login",
|
"/login",
|
||||||
"/register",
|
"/register",
|
||||||
"/health",
|
"/health",
|
||||||
"/code/**",
|
"/code/**",
|
||||||
"/blog/**"
|
"/v3/api-docs/**"
|
||||||
).permitAll()
|
).permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
|
||||||
|
.exceptionHandling(ex -> ex.authenticationEntryPoint(entryPoint));
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
@@ -48,7 +49,7 @@ public class SecurityConfig {
|
|||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**")
|
registry.addMapping("/**")
|
||||||
.allowedOriginPatterns("*") // 开发阶段允许所有来源
|
.allowedOriginPatterns("*")
|
||||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||||
.allowedHeaders("*")
|
.allowedHeaders("*")
|
||||||
.exposedHeaders("Authorization")
|
.exposedHeaders("Authorization")
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package xin.merlin.myplayerbackend.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
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 xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
import xin.merlin.myplayerbackend.entity.http.Code;
|
||||||
|
import xin.merlin.myplayerbackend.service.CodeService;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.Response;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.ResultCode;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 验证码controller
|
||||||
|
* 包含发送和验证逻辑*/
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/code")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CodeController {
|
||||||
|
|
||||||
|
private final CodeService codeService;
|
||||||
|
|
||||||
|
//发送验证码
|
||||||
|
@PostMapping("/send")
|
||||||
|
Response send(@RequestBody Account account){
|
||||||
|
try {
|
||||||
|
return codeService.send(account);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return Response.fail(ResultCode.SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//验证验证码是否存在、可用、正确
|
||||||
|
@PostMapping("/verify")
|
||||||
|
Response verify(@RequestBody Code code){
|
||||||
|
try {
|
||||||
|
return codeService.verify(code);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return Response.fail(ResultCode.SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package xin.merlin.myplayerbackend.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.Response;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.ResultCode;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HealthController {
|
||||||
|
|
||||||
|
@GetMapping("/health")
|
||||||
|
Response health() {
|
||||||
|
return Response.success(ResultCode.SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package xin.merlin.myplayerbackend.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
import xin.merlin.myplayerbackend.entity.http.Code;
|
||||||
|
import xin.merlin.myplayerbackend.service.CodeService;
|
||||||
|
import xin.merlin.myplayerbackend.service.impl.AccountServiceImpl;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.Response;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.ResultCode;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 登录controller
|
||||||
|
* 包括:登录和注册逻辑
|
||||||
|
* */
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class LoginController {
|
||||||
|
|
||||||
|
private final AccountServiceImpl accountService;
|
||||||
|
|
||||||
|
private final CodeService codeService;
|
||||||
|
|
||||||
|
private final HttpServletRequest request;
|
||||||
|
|
||||||
|
//login
|
||||||
|
@PostMapping("/login")
|
||||||
|
Response login(@RequestBody Account account){
|
||||||
|
|
||||||
|
|
||||||
|
return Response.success(ResultCode.SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
//register
|
||||||
|
@PostMapping("/register")
|
||||||
|
Response register(@RequestBody Code code){
|
||||||
|
try {
|
||||||
|
Response response = codeService.verify(code);
|
||||||
|
if(response.getCode().equals("200")){
|
||||||
|
Account account = new Account();
|
||||||
|
account.setAccount(code.getAccount());
|
||||||
|
account.setIp(request.getRemoteAddr());
|
||||||
|
accountService.save(account);
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return Response.fail(ResultCode.SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
17
src/main/java/xin/merlin/myplayerbackend/entity/Account.java
Normal file
17
src/main/java/xin/merlin/myplayerbackend/entity/Account.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("account")
|
||||||
|
public class Account {
|
||||||
|
@TableId("id")
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
private String account;
|
||||||
|
private String password;
|
||||||
|
private String ip;
|
||||||
|
}
|
||||||
18
src/main/java/xin/merlin/myplayerbackend/entity/Audit.java
Normal file
18
src/main/java/xin/merlin/myplayerbackend/entity/Audit.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("audit")
|
||||||
|
public class Audit {
|
||||||
|
@TableId("a_id")
|
||||||
|
private Integer a_id;
|
||||||
|
|
||||||
|
private Integer type;
|
||||||
|
private Integer applicant;
|
||||||
|
private Integer influence;
|
||||||
|
private String changed;
|
||||||
|
}
|
||||||
15
src/main/java/xin/merlin/myplayerbackend/entity/Friends.java
Normal file
15
src/main/java/xin/merlin/myplayerbackend/entity/Friends.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("friends")
|
||||||
|
public class Friends {
|
||||||
|
private Integer id;
|
||||||
|
private Integer f_id;
|
||||||
|
private String nickname;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
16
src/main/java/xin/merlin/myplayerbackend/entity/Group.java
Normal file
16
src/main/java/xin/merlin/myplayerbackend/entity/Group.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("group")
|
||||||
|
public class Group {
|
||||||
|
@TableId("g_id")
|
||||||
|
private Integer g_id;
|
||||||
|
|
||||||
|
private String g_name;
|
||||||
|
private String g_introduction;
|
||||||
|
private String g_avatar;
|
||||||
|
}
|
||||||
12
src/main/java/xin/merlin/myplayerbackend/entity/Groups.java
Normal file
12
src/main/java/xin/merlin/myplayerbackend/entity/Groups.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("groups")
|
||||||
|
public class Groups {
|
||||||
|
private Integer g_id;
|
||||||
|
private Integer id;
|
||||||
|
private Integer role;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("inviting")
|
||||||
|
public class Inviting {
|
||||||
|
@TableId("i_id")
|
||||||
|
private Integer i_id;
|
||||||
|
|
||||||
|
private Integer inviter;
|
||||||
|
private Integer target;
|
||||||
|
private Integer status;
|
||||||
|
private Integer room;
|
||||||
|
}
|
||||||
20
src/main/java/xin/merlin/myplayerbackend/entity/Message.java
Normal file
20
src/main/java/xin/merlin/myplayerbackend/entity/Message.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("message")
|
||||||
|
public class Message {
|
||||||
|
@TableId("m_id")
|
||||||
|
private Integer m_id;
|
||||||
|
|
||||||
|
private Integer sender;
|
||||||
|
private Integer receiver;
|
||||||
|
private String content;
|
||||||
|
private LocalDateTime time;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("playroom")
|
||||||
|
public class Playroom {
|
||||||
|
@TableId("r_id")
|
||||||
|
private Integer r_id;
|
||||||
|
|
||||||
|
private String r_name;
|
||||||
|
private String r_introduction;
|
||||||
|
private String r_avatar;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("playrooms")
|
||||||
|
public class Playrooms {
|
||||||
|
private Integer r_id;
|
||||||
|
private Integer id;
|
||||||
|
private Integer role;
|
||||||
|
}
|
||||||
17
src/main/java/xin/merlin/myplayerbackend/entity/User.java
Normal file
17
src/main/java/xin/merlin/myplayerbackend/entity/User.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@TableName("user")
|
||||||
|
public class User {
|
||||||
|
@TableId("u_id")
|
||||||
|
private String u_id;
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
private String u_name;
|
||||||
|
private String u_introduction;
|
||||||
|
private String u_avatar;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package xin.merlin.myplayerbackend.entity.http;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Code {
|
||||||
|
private String account;
|
||||||
|
private String c_id;
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AccountMapper extends BaseMapper<Account> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Audit;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface AuditMapper extends BaseMapper<Audit> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Friends;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface FriendsMapper extends BaseMapper<Friends> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Group;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface GroupMapper extends BaseMapper<Group> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Groups;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface GroupsMapper extends BaseMapper<Groups> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Inviting;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface InvitingMapper extends BaseMapper<Inviting> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Message;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface MessageMapper extends BaseMapper<Message> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Playroom;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface PlayroomMapper extends BaseMapper<Playroom> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package xin.merlin.myplayerbackend.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Playrooms;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface PlayroomsMapper extends BaseMapper<Playrooms> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
import xin.merlin.myplayerbackend.entity.http.Code;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.Response;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.ResultCode;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CodeService {
|
||||||
|
// 验证码字典
|
||||||
|
private static final Cache<String, String> waitingList = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 抛出以便登录时访问字典
|
||||||
|
public Cache<String, String> getWaitingList() {
|
||||||
|
return waitingList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 冷却缓存:限制邮箱请求频率
|
||||||
|
private static final Cache<String, Boolean> emailCooldown = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(60, TimeUnit.SECONDS) // 冷却 60 秒
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 验证码验证次数
|
||||||
|
private static final Cache<String, Integer> codeFailCount = Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private final MailService mailService;
|
||||||
|
|
||||||
|
// 发送验证码
|
||||||
|
public Response send(Account account){
|
||||||
|
String a = account.getAccount();
|
||||||
|
if(a==null) return Response.success(ResultCode.MAIL_ACCOUNT_NOT_PROVIDED);
|
||||||
|
log.info("sending code to ----->{}", a);
|
||||||
|
|
||||||
|
// 检查是否在冷却中
|
||||||
|
if (emailCooldown.getIfPresent(a) != null) {
|
||||||
|
return Response.success(ResultCode.MAIL_REQUEST_TOO_FAST);
|
||||||
|
}
|
||||||
|
String tempId = UUID.randomUUID().toString()
|
||||||
|
.replace("-", "");
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 加入验证码字典
|
||||||
|
waitingList.put(tempId, mailService.sendMail(a));
|
||||||
|
// 加入验证码冷却
|
||||||
|
emailCooldown.put(a, true);
|
||||||
|
return Response.success(ResultCode.SUCCESS, Map.of("c_id", tempId));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//验证验证码是否存在、可用、正确
|
||||||
|
@Transactional
|
||||||
|
public Response verify(Code code){
|
||||||
|
String c_id = code.getC_id();
|
||||||
|
String account = code.getAccount();
|
||||||
|
String c = code.getCode();
|
||||||
|
if(c_id==null||account==null||c==null) return Response.success(ResultCode.MAIL_INFO_LOST);
|
||||||
|
Integer count = codeFailCount.getIfPresent(c_id);
|
||||||
|
|
||||||
|
//设置并查看最大尝试次数
|
||||||
|
try {
|
||||||
|
if(count == null){
|
||||||
|
codeFailCount.put(c_id, 1);
|
||||||
|
}else if(count<=5){
|
||||||
|
codeFailCount.put(c_id, count+1);
|
||||||
|
}else {
|
||||||
|
codeFailCount.invalidate(c_id);
|
||||||
|
return Response.success(ResultCode.MAIL_VERIFY_FAIL_TOO_MANY);
|
||||||
|
}
|
||||||
|
|
||||||
|
//执行验证逻辑
|
||||||
|
String tempCode = waitingList.getIfPresent(c_id);
|
||||||
|
log.info("verifying...{},{}--?=--{}", account,code, tempCode);
|
||||||
|
if (tempCode == null) return Response.success(ResultCode.MAIL_VERIFY_NOT_EXIST);
|
||||||
|
if (!tempCode.equals(code.getCode())) return Response.success(ResultCode.MAIL_VERIFY_CODE_ERROR);
|
||||||
|
codeFailCount.invalidate(c_id);
|
||||||
|
emailCooldown.invalidate(account);
|
||||||
|
|
||||||
|
return Response.success(ResultCode.SUCCESS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.AccountMapper;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.Response;
|
||||||
|
import xin.merlin.myplayerbackend.utils.result.ResultCode;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class LoginService{
|
||||||
|
|
||||||
|
private final AccountMapper accountMapper;
|
||||||
|
|
||||||
|
public Response login(Account account){
|
||||||
|
Account ta = accountMapper.selectOne(new QueryWrapper<Account>().eq("account",account.getAccount()));
|
||||||
|
if(ta == null) return Response.success(ResultCode.USER_NOT_FOUND);
|
||||||
|
|
||||||
|
//TODO:111
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.mail.MailException;
|
||||||
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MailService {
|
||||||
|
|
||||||
|
private final JavaMailSender mailSender;
|
||||||
|
|
||||||
|
@Value("${spring.mail.username}")
|
||||||
|
private String mail;
|
||||||
|
|
||||||
|
public String sendMail(String receiver){
|
||||||
|
try {
|
||||||
|
String code = Double.toString(Math.random()).substring(2,8);
|
||||||
|
SimpleMailMessage message = new SimpleMailMessage();
|
||||||
|
message.setFrom(mail);
|
||||||
|
message.setTo(receiver);
|
||||||
|
message.setSubject("Welcome to use Merlin`s product");
|
||||||
|
message.setText("欢迎使用Merlin.xin产品! \n"+"您的验证码为:"+code+"\n有效期五分钟,请勿泄露!");
|
||||||
|
mailSender.send(message);
|
||||||
|
return code;
|
||||||
|
} catch (MailException e) {
|
||||||
|
log.error("e: ", e);
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Account;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.AccountMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Audit;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.AuditMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AuditServiceImpl extends ServiceImpl<AuditMapper, Audit> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Friends;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.FriendsMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FriendsServiceImpl extends ServiceImpl<FriendsMapper, Friends> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Group;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.GroupMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Groups;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.GroupsMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GroupsServiceImpl extends ServiceImpl<GroupsMapper, Groups> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Inviting;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.InvitingMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class InvitingServiceImpl extends ServiceImpl<InvitingMapper, Inviting> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Message;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.MessageMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Playroom;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.PlayroomMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PlayroomServiceImpl extends ServiceImpl<PlayroomMapper, Playroom> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package xin.merlin.myplayerbackend.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import xin.merlin.myplayerbackend.entity.Playrooms;
|
||||||
|
import xin.merlin.myplayerbackend.mapper.PlayroomsMapper;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PlayroomsServiceImpl extends ServiceImpl<PlayroomsMapper, Playrooms> {
|
||||||
|
}
|
||||||
@@ -38,14 +38,14 @@ public class JwtUtil {
|
|||||||
/**
|
/**
|
||||||
* 生成 JWT Token
|
* 生成 JWT Token
|
||||||
*/
|
*/
|
||||||
public String generateToken(String uAccount, Integer uId) {
|
public String generateToken(String account, Integer id) {
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
Date expireDate = new Date(now.getTime() + jwtProperties.getExpire() * 1000L);
|
Date expireDate = new Date(now.getTime() + jwtProperties.getExpire() * 1000L);
|
||||||
|
|
||||||
return Jwts.builder()
|
return Jwts.builder()
|
||||||
.subject(uAccount)
|
.subject(account)
|
||||||
.claim("id", uId)
|
.claim("id", id)
|
||||||
.claim("account", uAccount)
|
.claim("account", account)
|
||||||
.id(UUID.randomUUID().toString())
|
.id(UUID.randomUUID().toString())
|
||||||
.issuedAt(now)
|
.issuedAt(now)
|
||||||
.expiration(expireDate)
|
.expiration(expireDate)
|
||||||
@@ -82,7 +82,7 @@ public class JwtUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取账号
|
* 获取账号
|
||||||
*/
|
*/
|
||||||
public String getUAccount(String token) {
|
public String getAccount(String token) {
|
||||||
Claims claims = getClaims(token);
|
Claims claims = getClaims(token);
|
||||||
return claims.getSubject();
|
return claims.getSubject();
|
||||||
}
|
}
|
||||||
@@ -90,9 +90,9 @@ public class JwtUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取用户ID
|
* 获取用户ID
|
||||||
*/
|
*/
|
||||||
public String getUId(String token) {
|
public Integer getId(String token) {
|
||||||
Claims claims = getClaims(token);
|
Claims claims = getClaims(token);
|
||||||
return claims.get("id", String.class);
|
return claims.get("id", Integer.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义异常类
|
// 自定义异常类
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package xin.merlin.myplayerbackend.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class RandomCode {
|
||||||
|
|
||||||
|
private static final Random rand = new Random();
|
||||||
|
|
||||||
|
public static String generateID(){
|
||||||
|
// 生成一个0到999999999的随机数,然后格式化为9位数字字符串
|
||||||
|
return String.format("%09d", rand.nextInt(1000000000));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String generateCode(){
|
||||||
|
// 生成一个0到999999的随机数,然后格式化为6位数字字符串
|
||||||
|
return String.format("%06d", rand.nextInt(1000000));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package xin.merlin.myplayerbackend.utils;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class SecretKeyGenerator {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SecretKey key = Keys.secretKeyFor(io.jsonwebtoken.SignatureAlgorithm.HS256);
|
||||||
|
String base64Key = Base64.getEncoder().encodeToString(key.getEncoded());
|
||||||
|
System.out.println("Your secure Base64 key:");
|
||||||
|
System.out.println(base64Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,23 +10,23 @@ import java.time.LocalDateTime;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class Response<T> {
|
public class Response<T> {
|
||||||
private int code;
|
private String code;
|
||||||
private String message;
|
private String message;
|
||||||
private T data;
|
private T data;
|
||||||
|
|
||||||
// 请求成功
|
// 请求成功
|
||||||
public static <T> Response<T> success(ResultCode code , T data){
|
public static <T> Response<T> success(ResultCode code , T data){
|
||||||
System.out.println( LocalDateTime.now()+" success : \n"+code);
|
System.out.println( LocalDateTime.now()+" success : "+code);
|
||||||
return new Response<T>(code.getCode(), code.getMessage(), data);
|
return new Response<T>(code.getCode(), code.getMessage(), data);
|
||||||
}
|
}
|
||||||
public static <T> Response<T> success(ResultCode code){
|
public static <T> Response<T> success(ResultCode code){
|
||||||
System.out.println( LocalDateTime.now()+" success : \n"+code);
|
System.out.println( LocalDateTime.now()+" success : "+code);
|
||||||
return new Response<T>(code.getCode(), code.getMessage(),null);
|
return new Response<T>(code.getCode(), code.getMessage(),null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求失败
|
// 请求失败
|
||||||
public static <T> Response<T> fail(ResultCode code){
|
public static <T> Response<T> fail(ResultCode code){
|
||||||
System.out.println( LocalDateTime.now()+" fail : \n"+code);
|
System.out.println( LocalDateTime.now()+" fail : "+code);
|
||||||
return new Response<T>(code.getCode(),code.getMessage(), null);
|
return new Response<T>(code.getCode(),code.getMessage(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,29 +8,42 @@ import lombok.Getter;
|
|||||||
@Getter
|
@Getter
|
||||||
public enum ResultCode {
|
public enum ResultCode {
|
||||||
|
|
||||||
SUCCESS(200, "成功"),
|
SUCCESS("200", "成功"),
|
||||||
BAD_REQUEST(400, "请求参数错误"),
|
BAD_REQUEST("400", "请求参数错误"),
|
||||||
UNAUTHORIZED(401, "未认证或登录已过期"),
|
UNAUTHORIZED("401", "未认证或登录已过期"),
|
||||||
FORBIDDEN(403, "无权限访问"),
|
FORBIDDEN("403", "无权限访问"),
|
||||||
NOT_FOUND(404, "资源不存在"),
|
NOT_FOUND("404", "资源不存在"),
|
||||||
SERVER_ERROR(500, "服务器内部错误"),
|
SERVER_ERROR("500", "服务器内部错误"),
|
||||||
|
|
||||||
// 自定义业务错误码
|
// 自定义业务错误码
|
||||||
USER_BANNED(1000,"用户被封禁"),
|
|
||||||
USER_NOT_FOUND(1001, "用户不存在"),
|
//账户相关
|
||||||
USER_EXIST(1003,"用户已存在"),
|
ACCOUNT_EXIST("2001","账户已存在"),
|
||||||
USER_PASSWORD_ERROR(1004,"用户密码错误"),
|
|
||||||
USER_VERIFICATION_ERROR(1005,"验证码不存在或错误"),
|
//用户相关
|
||||||
USER_SEND_TOO_FAST(1006,"用户请求过快"),
|
USER_BANNED("4000","用户被封禁"),
|
||||||
USER_SEND_TOO_OFTEN(1007,"请求次数过多,已被限制"),
|
USER_NOT_FOUND("4001", "用户不存在"),
|
||||||
ORDER_NOT_FOUND(2000, "订单不存在");
|
USER_EXIST("4003","用户已存在"),
|
||||||
|
USER_PASSWORD_ERROR("4004","用户密码错误"),
|
||||||
|
USER_VERIFICATION_ERROR("4005","验证码不存在或错误"),
|
||||||
|
USER_SEND_TOO_FAST("4006","用户请求过快"),
|
||||||
|
USER_SEND_TOO_OFTEN("4007","请求次数过多,已被限制"),
|
||||||
|
ORDER_NOT_FOUND("4000", "订单不存在"),
|
||||||
|
|
||||||
|
//邮箱相关
|
||||||
|
MAIL_ACCOUNT_NOT_PROVIDED("4101","未提供验证码接受账户"),
|
||||||
|
MAIL_REQUEST_TOO_FAST("4102","验证码请求过于频繁"),
|
||||||
|
MAIL_INFO_LOST("4103","验证信息丢失"),
|
||||||
|
MAIL_VERIFY_FAIL_TOO_MANY("4104","验证码错误过多,请重新申请验证码"),
|
||||||
|
MAIL_VERIFY_NOT_EXIST("4105","验证码元素丢失,请重新申请验证码"),
|
||||||
|
MAIL_VERIFY_CODE_ERROR("4106","验证码错误,请重新输入");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private final int code;
|
private final String code;
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|
||||||
ResultCode(int code, String message) {
|
ResultCode(String code, String message) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user