Skip to content

Commit

Permalink
boot api token update set login user details
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Feb 23, 2024
1 parent 80d615d commit 7b38b9c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class SecurityController {
private final ServerOAuth2AuthorizedClientRepository clientRepository;

@GetMapping("token")
public Mono<AuthenticationToken> token(WebSession session) {
return Mono.defer(() -> Mono.justOrEmpty(AuthenticationToken.build(session)));
public Mono<AuthenticationToken> token(WebSession session, Authentication authentication) {
return Mono.defer(() -> Mono.just(AuthenticationToken.build(session, authentication)));
}

@GetMapping("csrf")
Expand All @@ -42,11 +42,6 @@ public Mono<CsrfToken> csrfToken() {
});
}

@GetMapping("me")
public Mono<SecurityDetails> me() {
return ContextUtils.securityDetails();
}

@GetMapping("bind")
public Mono<Object> bindOauth2(String clientRegistrationId, Authentication authentication, ServerWebExchange exchange) {
return this.clientRepository.loadAuthorizedClient(clientRegistrationId, authentication, exchange)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.platform.boot.security.core;

import org.springframework.security.core.Authentication;
import org.springframework.web.server.WebSession;

import java.io.Serializable;
Expand All @@ -14,14 +15,15 @@
* @param lastAccessTime Last access time of the token in epoch seconds
* @author Alex bob(<a href="https://github.com/vnobo">Alex Bob</a>)
*/
public record AuthenticationToken(String token, Long expires, Long lastAccessTime) implements Serializable {
public record AuthenticationToken(String token, Long expires, Long lastAccessTime,
Object details) implements Serializable {

public static AuthenticationToken of(String token, String expires, Long lastAccessTime) {
return new AuthenticationToken(token, Long.parseLong(expires), lastAccessTime);
public static AuthenticationToken of(String token, Long expires, Long lastAccessTime, Object details) {
return new AuthenticationToken(token, expires, lastAccessTime, details);
}

public static AuthenticationToken build(WebSession session) {
return new AuthenticationToken(session.getId(), session.getMaxIdleTime().getSeconds(),
session.getLastAccessTime().getEpochSecond());
public static AuthenticationToken build(WebSession session, Authentication authentication) {
return of(session.getId(), session.getMaxIdleTime().getSeconds(),
session.getLastAccessTime().getEpochSecond(), authentication.getPrincipal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,7 @@
import java.io.Serializable;

/**
* UserAuditor is a class that represents an auditor for user information.
* It implements the Serializable interface to support serialization.
* <p>
* The class has two private fields: username and name, which store the username and name of the user auditor.
* <p>
* The class provides a static method withUsername(String username) that
* creates a new UserAuditor object and sets the username field to the given username.
* <p>
* The class also provides a static method withDetails(SecurityDetails securityDetails) that
* creates a new UserAuditor object and sets the username field to the username from the given SecurityDetails object.
* <p>
* This class is typically used in auditing user actions or tracking user information.
* <p>
*
* @author Alex bob (<a href="https://github.com/vnobo">Alex bob</a>)
* @author <a href="https://github.com/vnobo">Alex bob</a>
*/
public record UserAuditor(String code, String username, String name) implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, A
response.setStatusCode(HttpStatus.OK);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return exchange.getSession().flatMap(session -> {
AuthenticationToken authenticationToken = AuthenticationToken.build(session);
AuthenticationToken authenticationToken = AuthenticationToken.build(session, token);
var body = ContextUtils.objectToBytes(authenticationToken);
var dataBufferFactory = response.bufferFactory();
var bodyBuffer = dataBufferFactory.wrap(body);
Expand Down

0 comments on commit 7b38b9c

Please sign in to comment.