Skip to content

Instantly share code, notes, and snippets.

@iamprafful
Created January 16, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamprafful/aa7c5d6215f7ccd63ba33114eb51204c to your computer and use it in GitHub Desktop.
Save iamprafful/aa7c5d6215f7ccd63ba33114eb51204c to your computer and use it in GitHub Desktop.
package com.example.api.controller;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping
public Map<String, Object> getUserName() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Map<String, Object> userMap = new HashMap<>();
userMap.put("username", authentication.getName());
userMap.put("error", false);
return userMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment