Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

get logged-in user in Spring Security

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// Get user name
authentication.getName()
Code language: JavaScript (javascript)
Comment

How to find the logged-in user in Spring Boot?

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Comment

Retrieve User information in Spring Security

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
    String currentUserName = authentication.getName();
    return currentUserName;
}
Comment

Retrieve User information in Spring Security

UserDetails userDetails = (UserDetails) authentication.getPrincipal();
System.out.println("User has authorities: " + userDetails.getAuthorities());
Comment

Retrieve User information in Spring Security

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
Comment

Retrieve User information in Spring Security

@Controller
public class SecurityController {

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserName(Principal principal) {
        return principal.getName();
    }
}
Comment

Retrieve User information in Spring Security

@Controller
public class SecurityController {

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserName(Authentication authentication) {
        return authentication.getName();
    }
}
Comment

Retrieve User information in Spring Security

@Controller
public class GetUserWithHTTPServletRequestController {

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserNameSimple(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        return principal.getName();
    }
}
Comment

Retrieve User information in Spring Security

public interface IAuthenticationFacade {
    Authentication getAuthentication();
}
@Component
public class AuthenticationFacade implements IAuthenticationFacade {

    @Override
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}
Comment

Retrieve User information in Spring Security

@Controller
public class GetUserWithCustomInterfaceController {
    @Autowired
    private IAuthenticationFacade authenticationFacade;

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserNameSimple() {
        Authentication authentication = authenticationFacade.getAuthentication();
        return authentication.getName();
    }
}
Comment

Retrieve User information in Spring Security

<html xmlns:th="https://www.thymeleaf.org" 
  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<body>
    <div sec:authorize="isAuthenticated()">
      Authenticated as <span sec:authentication="name"></span></div>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Java :: java clear scanner2 
Java :: jframe calculator 
Java :: array copy java 
Java :: android send fragment context 
Java :: display 2d array 
Java :: share location in android programmatically 
Java :: Java if...else 
Java :: how to sort the arraylist without changing the original arraylist 
Java :: feign client url from properties 
Java :: Java read in 
Java :: how to assign null to a variable in Java 
Java :: is c# similar to java 
Java :: reverse a doubly linked list 
Java :: double number java 
Java :: qr code spring boot 
Java :: lambda function java 
Java :: import image icon 
Java :: how to convert double to int in java 
Java :: encapsulation java 
Java :: java stream findany 
Java :: The Longest Substring 
Java :: binary tree traversal 
Java :: adding watermark to excel 
Java :: new date api in java 8 
Java :: java long literal 
Java :: list of arrays 
Java :: abstract class java 
Java :: Java How to use NavigableMap? 
Java :: java exception handling 
Java :: HOW TO MAKE ENUM START WITH ONE 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =