Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spring boot logged in user

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 :: set image programmatically android 
Java :: android application class 
Java :: open website from android activity 
Java :: formula to calculate area of a triangle java 
Java :: java string static arrat 
Java :: min max heap java 
Java :: java regex 
Java :: array input java 
Java :: java enum length 
Java :: how to get last element in java 
Java :: how to check base64 in java 
Java :: fragment to fragment 
Java :: java set foreach 
Java :: java run code at interval 
Java :: how to use sql file in java 
Java :: spring websocket allow origin 
Java :: brew use java 11 
Java :: login and logout react native and firebase 
Java :: java 2d array length 
Java :: java convert double to int 
Java :: java sample code 
Java :: retrieving parent element from child aWebelement selenium java 
Java :: crear objetos automaticamente java 
Java :: HOW TO PARSE a string into a number in java 
Java :: Java for and while loops 
Java :: pre increment and post increments java 
Java :: how to multiply bigdecimals 
Java :: How to use java 8 comparator 
Java :: java extends 
Java :: java mcq 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =