File tree Expand file tree Collapse file tree 3 files changed +43
-6
lines changed
src/main/java/com/alwinsimon/UserManagementJavaSpringBoot Expand file tree Collapse file tree 3 files changed +43
-6
lines changed Original file line number Diff line number Diff line change 8
8
import org .springframework .security .authentication .AuthenticationProvider ;
9
9
import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
10
10
import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
11
+ import org .springframework .security .core .context .SecurityContextHolder ;
11
12
import org .springframework .security .core .userdetails .UserDetailsService ;
12
13
import org .springframework .security .core .userdetails .UsernameNotFoundException ;
13
14
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
14
15
import org .springframework .security .crypto .password .PasswordEncoder ;
15
16
17
+ import java .security .Principal ;
18
+
16
19
@ Configuration
17
20
@ RequiredArgsConstructor
18
21
public class ApplicationConfig {
@@ -54,4 +57,9 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration a
54
57
55
58
}
56
59
60
+ @ Bean
61
+ public Principal principal () throws Exception {
62
+ return () -> SecurityContextHolder .getContext ().getAuthentication ().getName ();
63
+ }
64
+
57
65
}
Original file line number Diff line number Diff line change 1
1
package com .alwinsimon .UserManagementJavaSpringBoot .Controller ;
2
2
3
- import org .springframework .web .bind .annotation .*;
4
- //import com.alwinsimon.UserManagementJavaSpringBoot.Model.User;
3
+ import com .alwinsimon .UserManagementJavaSpringBoot .Model .User ;
4
+ import com .alwinsimon .UserManagementJavaSpringBoot .Service .UserService ;
5
+ import lombok .RequiredArgsConstructor ;
6
+ import org .springframework .web .bind .annotation .CrossOrigin ;
7
+ import org .springframework .web .bind .annotation .GetMapping ;
8
+ import org .springframework .web .bind .annotation .RequestMapping ;
9
+ import org .springframework .web .bind .annotation .RestController ;
5
10
6
11
@ RestController
7
12
@ RequestMapping ("/api/v1/user" )
13
+ @ RequiredArgsConstructor
8
14
@ CrossOrigin ("*" )
9
15
public class UserController {
10
- @ PostMapping ("/register" )
11
- public String addUser (@ RequestBody String requestBody ){
12
- System .out .println ("Received request body: " + requestBody );
13
- return requestBody ;
16
+
17
+ private final UserService userService ;
18
+
19
+ @ GetMapping ("/current-user" )
20
+ public User getCurrentUser () {
21
+ return userService .currentUserDetails ();
14
22
}
15
23
}
Original file line number Diff line number Diff line change
1
+ package com .alwinsimon .UserManagementJavaSpringBoot .Service ;
2
+
3
+ import com .alwinsimon .UserManagementJavaSpringBoot .Model .User ;
4
+ import com .alwinsimon .UserManagementJavaSpringBoot .Repository .UserRepository ;
5
+ import lombok .RequiredArgsConstructor ;
6
+ import org .springframework .stereotype .Service ;
7
+
8
+ import java .security .Principal ;
9
+
10
+ @ Service
11
+ @ RequiredArgsConstructor
12
+ public class UserService {
13
+
14
+ private final UserRepository userRepository ;
15
+ private final Principal principal ;
16
+
17
+ public User currentUserDetails () {
18
+ String email = principal .getName ();
19
+ return userRepository .findByEmail (email ).orElse (null );
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments