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 :: jave arrayList to Array 
Java :: change drawable color programmatically android 
Java :: java print text 
Java :: jagged array java 
Java :: java thread lambda 
Java :: printing in java 
Java :: junit 5 expected exception 
Java :: print queue in java 
Java :: how to loop through a 2d array java 
Java :: java get constructor 
Java :: how to iterate hashmap java 
Java :: creating array java 
Java :: java run system command 
Java :: shell sort java 
Java :: display an image java 
Java :: expected exception junit 
Java :: java else nothing 
Java :: java obtain list string from list object 
Java :: copy arraylist java 
Java :: java xmx example 
Java :: java guid 
Java :: char array to arraylist java 
Java :: Java List Sort Using sort() method 
Java :: java find last element in array 
Java :: java string to decimal 
Java :: initialize arraylist 
Java :: toCharArray() method java 
Java :: jpa default value enum 
Java :: android studio proportional width 
Java :: simple login password and username java 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =