Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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