Skip to content

Commit 106f91a

Browse files
committed
Fix fetching user information
1 parent 3a091d8 commit 106f91a

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

server/user/src/main/java/com/continiousdisappointment/user/controller/UserController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.continiousdisappointment.user.service.UserService;
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestHeader;
78
import org.springframework.web.bind.annotation.RequestMapping;
89
import org.springframework.web.bind.annotation.RestController;
910

@@ -14,7 +15,7 @@ public class UserController {
1415
private final UserService userService;
1516

1617
@GetMapping
17-
public User getUserInfo() {
18-
return userService.getUserInfo();
18+
public User getUserInfo(@RequestHeader("Authorization") String authorization) {
19+
return userService.getUserInfo(authorization);
1920
}
2021
}
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.continiousdisappointment.user.service;
22

33
import org.springframework.http.*;
4-
import org.springframework.security.core.Authentication;
5-
import org.springframework.security.core.context.SecurityContextHolder;
6-
import org.springframework.security.oauth2.core.OAuth2AccessToken;
74
import org.springframework.stereotype.Service;
85
import org.springframework.web.client.RestTemplate;
96

@@ -14,14 +11,13 @@ public class UserService {
1411

1512
private final RestTemplate restTemplate = new RestTemplate();
1613

17-
public User getUserInfo() {
14+
public User getUserInfo(String authorization) {
1815
String url = "https://gitlab.lrz.de/api/v4/user";
19-
String accessToken = getBearerToken();
20-
if (accessToken == null) {
16+
if (authorization == null) {
2117
throw new IllegalStateException("No access token found in security context");
2218
}
2319
HttpHeaders headers = new HttpHeaders();
24-
headers.setBearerAuth(accessToken); // Sets Authorization: Bearer <token>
20+
headers.set(HttpHeaders.AUTHORIZATION, authorization); // Sets Authorization: Bearer <token>
2521

2622
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
2723

@@ -32,17 +28,4 @@ public User getUserInfo() {
3228
User.class);
3329
return response.getBody();
3430
}
35-
36-
private String getBearerToken() {
37-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
38-
if (authentication == null) {
39-
return null;
40-
}
41-
OAuth2AccessToken credentials = (OAuth2AccessToken) authentication.getCredentials();
42-
if (credentials != null) {
43-
return credentials.getTokenValue();
44-
}
45-
return null;
46-
}
47-
4831
}

0 commit comments

Comments
 (0)