优化代码
This commit is contained in:
parent
bda0b64406
commit
707a616bda
9
pom.xml
9
pom.xml
@ -207,13 +207,20 @@
|
|||||||
<version>8.0.1.Final</version>
|
<version>8.0.1.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 公共工具 -->
|
<!-- 用于JavaBean属性操作库 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-beanutils</groupId>
|
<groupId>commons-beanutils</groupId>
|
||||||
<artifactId>commons-beanutils</artifactId>
|
<artifactId>commons-beanutils</artifactId>
|
||||||
<version>1.9.4</version>
|
<version>1.9.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 通用工具类库 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.12.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-logging</groupId>
|
<groupId>commons-logging</groupId>
|
||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
|
|||||||
@ -12,10 +12,8 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
|
||||||
import org.springframework.security.authentication.LockedException;
|
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -23,6 +21,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* 登录控制层
|
* 登录控制层
|
||||||
@ -42,22 +42,27 @@ public class AuthController {
|
|||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ResponseEntity<ApiResponse<LoginVO>> login(@Valid @RequestBody LoginRequest loginRequest) {
|
public ApiResponse<LoginVO> login(@Valid @RequestBody LoginRequest loginRequest) {
|
||||||
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
|
||||||
loginRequest.getUsername(), loginRequest.getPassword()
|
loginRequest.getUsername(), loginRequest.getPassword()
|
||||||
));
|
));
|
||||||
|
|
||||||
LoginVO loginVO = authService.login(authentication, loginRequest.getRememberMe());
|
LoginVO loginVO = authService.login(authentication, loginRequest.getRememberMe());
|
||||||
return ResponseEntity.ok().body(ApiResponse.ofStatus(Status.LOGIN_SUCCESS, loginVO));
|
return ApiResponse.ofStatus(Status.LOGIN_SUCCESS, loginVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
public ApiResponse<?> logout(HttpServletRequest request) {
|
public ApiResponse<Void> logout(HttpServletRequest request) {
|
||||||
String token = jwtUtil.getTokenFromRequest(request);
|
String token = jwtUtil.getTokenFromRequest(request);
|
||||||
if (token == null || token.isBlank()) {
|
|
||||||
|
// 1. 统一响应格式(全部使用ResponseEntity)
|
||||||
|
if (StringUtils.isBlank(token)) {
|
||||||
return ApiResponse.ofStatus(Status.TOKEN_EXPIRED);
|
return ApiResponse.ofStatus(Status.TOKEN_EXPIRED);
|
||||||
}
|
}
|
||||||
authService.logout(token);
|
|
||||||
|
// 2. 异步执行登出操作(提高性能)
|
||||||
|
CompletableFuture.runAsync(() -> authService.logout(token));
|
||||||
|
|
||||||
return ApiResponse.ofStatus(Status.SUCCESS);
|
return ApiResponse.ofStatus(Status.SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user