feat: 添加退出接口
This commit is contained in:
@@ -15,6 +15,8 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/auth")
|
@RequestMapping("/auth")
|
||||||
public class AuthController {
|
public class AuthController {
|
||||||
@@ -59,4 +61,12 @@ public class AuthController {
|
|||||||
public boolean getRegisterEnabled() {
|
public boolean getRegisterEnabled() {
|
||||||
return authService.getRegisterEnabled();
|
return authService.getRegisterEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/logout")
|
||||||
|
public ResponseEntity<Void> logout() {
|
||||||
|
var cookie = authService.buildCookie(TokenConstant.TOKEN_NAME, "", Duration.ZERO);
|
||||||
|
return ResponseEntity.status(HttpStatus.OK)
|
||||||
|
.header(HttpHeaders.SET_COOKIE, cookie.toString())
|
||||||
|
.body(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.onixbyte.helix.manager;
|
||||||
|
|
||||||
|
import com.onixbyte.helix.properties.TokenProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SecurityManager {
|
||||||
|
|
||||||
|
private final TokenProperties tokenProperties;
|
||||||
|
|
||||||
|
public SecurityManager(TokenProperties tokenProperties) {
|
||||||
|
this.tokenProperties = tokenProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Duration getTokenValidDuration() {
|
||||||
|
return tokenProperties.validTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
package com.onixbyte.helix.service;
|
package com.onixbyte.helix.service;
|
||||||
|
|
||||||
|
import com.onixbyte.helix.client.TokenClient;
|
||||||
import com.onixbyte.helix.domain.entity.Setting;
|
import com.onixbyte.helix.domain.entity.Setting;
|
||||||
import com.onixbyte.helix.domain.entity.User;
|
import com.onixbyte.helix.domain.entity.User;
|
||||||
import com.onixbyte.helix.domain.web.request.LoginRequest;
|
import com.onixbyte.helix.domain.web.request.LoginRequest;
|
||||||
import com.onixbyte.helix.exception.BizException;
|
import com.onixbyte.helix.exception.BizException;
|
||||||
import com.onixbyte.helix.manager.*;
|
import com.onixbyte.helix.manager.ApplicationManager;
|
||||||
|
import com.onixbyte.helix.manager.CaptchaManager;
|
||||||
|
import com.onixbyte.helix.manager.SecurityManager;
|
||||||
|
import com.onixbyte.helix.manager.SettingManager;
|
||||||
import com.onixbyte.helix.security.authentication.UsernamePasswordAuthentication;
|
import com.onixbyte.helix.security.authentication.UsernamePasswordAuthentication;
|
||||||
import com.onixbyte.helix.shared.SettingName;
|
import com.onixbyte.helix.shared.SettingName;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -15,6 +19,7 @@ import org.springframework.http.ResponseCookie;
|
|||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -27,18 +32,22 @@ public class AuthService {
|
|||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
private final SettingManager settingManager;
|
private final SettingManager settingManager;
|
||||||
private final ApplicationManager applicationManager;
|
private final ApplicationManager applicationManager;
|
||||||
|
private final TokenClient tokenClient;
|
||||||
|
private final SecurityManager securityManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public AuthService(
|
public AuthService(
|
||||||
CaptchaManager captchaManager,
|
CaptchaManager captchaManager,
|
||||||
AuthenticationManager authenticationManager,
|
AuthenticationManager authenticationManager,
|
||||||
SettingManager settingManager,
|
SettingManager settingManager,
|
||||||
ApplicationManager applicationManager
|
ApplicationManager applicationManager,
|
||||||
) {
|
TokenClient tokenClient, SecurityManager securityManager) {
|
||||||
this.captchaManager = captchaManager;
|
this.captchaManager = captchaManager;
|
||||||
this.authenticationManager = authenticationManager;
|
this.authenticationManager = authenticationManager;
|
||||||
this.settingManager = settingManager;
|
this.settingManager = settingManager;
|
||||||
this.applicationManager = applicationManager;
|
this.applicationManager = applicationManager;
|
||||||
|
this.tokenClient = tokenClient;
|
||||||
|
this.securityManager = securityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,6 +101,16 @@ public class AuthService {
|
|||||||
public ResponseCookie buildCookie(String cookieName, String token) {
|
public ResponseCookie buildCookie(String cookieName, String token) {
|
||||||
var cookieBuilder = ResponseCookie.from(cookieName, token)
|
var cookieBuilder = ResponseCookie.from(cookieName, token)
|
||||||
.httpOnly(true)
|
.httpOnly(true)
|
||||||
|
.maxAge(securityManager.getTokenValidity())
|
||||||
|
.path("/");
|
||||||
|
|
||||||
|
return cookieBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseCookie buildCookie(String cookieName, String token, Duration validDuration) {
|
||||||
|
var cookieBuilder = ResponseCookie.from(cookieName, token)
|
||||||
|
.httpOnly(true)
|
||||||
|
.maxAge(validDuration)
|
||||||
.path("/");
|
.path("/");
|
||||||
|
|
||||||
return cookieBuilder.build();
|
return cookieBuilder.build();
|
||||||
|
|||||||
Reference in New Issue
Block a user