Java Code Examples for org.springframework.security.core.userdetails.User#getUsername()
The following examples show how to use
org.springframework.security.core.userdetails.User#getUsername() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 2
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); // String email = user.getEmail(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 3
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); // String email = user.getEmail(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 4
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); // String email = user.getEmail(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 5
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); // String email = user.getEmail(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 6
Source File: SpringSecurityUserContext.java From Spring-Security-Third-Edition with MIT License | 6 votes |
/** * Get the {@link CalendarUser} by obtaining the currently logged in Spring Security user's * {@link Authentication#getName()} and using that to find the {@link CalendarUser} by email address (since for our * application Spring Security usernames are email addresses). */ @Override public CalendarUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } User user = (User)authentication.getPrincipal(); String email = user.getUsername(); // String email = user.getEmail(); if (email == null) { return null; } CalendarUser result = calendarService.findUserByEmail(email); if (result == null) { throw new IllegalStateException( "Spring Security is not in synch with CalendarUsers. Could not find user with email " + email); } return result; }
Example 7
Source File: AccountResource.java From flair-registry with Apache License 2.0 | 6 votes |
/** * GET /account : get the current user. * * @return the ResponseEntity with status 200 (OK) and the current user in body, or status 500 (Internal Server * Error) if the user couldn't be returned */ @GetMapping("/account") @Timed public ResponseEntity<UserVM> getAccount() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); try { String login; if (authentication.getPrincipal() instanceof User) { User user = (User) authentication.getPrincipal(); login = user.getUsername(); log.debug("The username `{}` has been found using JWT", login); } else if (authentication.getPrincipal() instanceof String) { login = (String) authentication.getPrincipal(); log.debug("The username `{}` has been found using OpenID Connect", login); } else { log.debug("The username could not be found"); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } UserVM userVM = new UserVM(login, authentication.getAuthorities().stream() .map(GrantedAuthority::getAuthority).collect(Collectors.toSet())); return new ResponseEntity<>(userVM, HttpStatus.OK); } catch (NullPointerException | ClassCastException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
Example 8
Source File: UserService.java From codenjoy with GNU General Public License v3.0 | 5 votes |
@Override public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { User predefinedUser = predefinedUsers.get(email); if (predefinedUser == null) { return Optional.ofNullable(players.getByEmail(email)) .map(player -> buildUserDetails(player.getEmail(), player.getPassword(), ROLE_USER)) .orElse(null); } return new User(predefinedUser.getUsername(), predefinedUser.getPassword(), predefinedUser.getAuthorities()); }
Example 9
Source File: HerdDaoSecurityHelper.java From herd with Apache License 2.0 | 5 votes |
/** * Gets the currently logged in username. If no user is logged in, then the "SYSTEM" user is returned. * * @return the currently logged in user. */ public String getCurrentUsername() { String username = SYSTEM_USER; if (SecurityContextHolder.getContext().getAuthentication() != null) { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); username = user.getUsername(); } return username; }
Example 10
Source File: UserController.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> myProfile() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com"; UserProfile profile = new UserProfile(user.getUsername(), email); return ResponseEntity.ok(profile); }
Example 11
Source File: UserController.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> myProfile() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com"; UserProfile profile = new UserProfile(user.getUsername(), email); return ResponseEntity.ok(profile); }
Example 12
Source File: OneOpsUser.java From secrets-proxy with Apache License 2.0 | 5 votes |
/** * Creates a new OneOps user from the {@link UserDetails} object. * * @param user user details. */ public OneOpsUser(User user) { this( user.getUsername(), user.getPassword(), user.getAuthorities(), user.getUsername(), AuthDomain.PROD); }
Example 13
Source File: UserController.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> hello() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com"; UserProfile profile = new UserProfile(); profile.setName(user.getUsername()); profile.setEmail(email); return ResponseEntity.ok(profile); }
Example 14
Source File: UserController.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> myProfile() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com"; UserProfile profile = new UserProfile(user.getUsername(), email); return ResponseEntity.ok(profile); }
Example 15
Source File: UserController.java From oauth2lab with MIT License | 5 votes |
@RequestMapping("/api/userinfo") public ResponseEntity<UserInfo> getUserInfo() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@spring2go.com"; UserInfo userInfo = new UserInfo(); userInfo.setName(user.getUsername()); userInfo.setEmail(email); return ResponseEntity.ok(userInfo); }
Example 16
Source File: LoginSuccessHandler.java From secrets-proxy with Apache License 2.0 | 5 votes |
/** * Helper method to create {@link OneOpsUser} for authentication principal. * * @param principal authentication principal * @return oneops user. */ private OneOpsUser getOneOpsUser(User principal) { log.debug("Found user details in authentication. Creating OneOps User."); String userName = principal.getUsername(); String password = principal.getPassword(); if (password == null) { log.debug(userName + " credentials are already erased."); password = ""; } return new OneOpsUser( userName, password, principal.getAuthorities(), userName, AuthDomain.PROD); }
Example 17
Source File: UserController.java From OAuth-2.0-Cookbook with MIT License | 5 votes |
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> hello() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com"; UserProfile profile = new UserProfile(); profile.setName(user.getUsername()); profile.setEmail(email); return ResponseEntity.ok(profile); }
Example 18
Source File: UserController.java From oauth2lab with MIT License | 5 votes |
@RequestMapping("/api/userinfo") public ResponseEntity<UserInfo> getUserInfo() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@spring2go.com"; UserInfo userInfo = new UserInfo(); userInfo.setName(user.getUsername()); userInfo.setEmail(email); return ResponseEntity.ok(userInfo); }
Example 19
Source File: DefaultJwtSecurityTokenService.java From onetwo with Apache License 2.0 | 5 votes |
@Override public JwtSecurityTokenInfo generateToken(Authentication authentication){ if(authentication==null){ return null; } LoginUserDetails userDetails = null; if(authentication.getPrincipal() instanceof LoginUserDetails){ userDetails = (LoginUserDetails)authentication.getPrincipal(); }else{ User user = (User)authentication.getPrincipal(); userDetails = new LoginUserDetails(0L, user.getUsername(), "N/A", user.getAuthorities()); } Collection<String> authorities = userDetails.getAuthorities() .stream() .map(auth->auth.getAuthority()) .collect(Collectors.toSet()); String authoritiesString = GuavaUtils.join(authorities, ","); DateTime issuteAt = DateTime.now(); Date expirationDate = issuteAt.plusSeconds(getExpirationInSeconds().intValue()).toDate(); String token = Jwts.builder() .setSubject(userDetails.getUsername()) .claim(JwtSecurityUtils.CLAIM_USER_ID, userDetails.getUserId()) .claim(JwtSecurityUtils.CLAIM_AUTHORITIES, authoritiesString) .setIssuedAt(issuteAt.toDate()) .setExpiration(expirationDate) .signWith(SignatureAlgorithm.HS512, securityConfig.getJwt().getSigningKey()) .compact(); return JwtSecurityTokenInfo.builder() .token(token) .build(); }
Example 20
Source File: Log4jMdcLoggingFilter.java From herd with Apache License 2.0 | 5 votes |
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "The ServletRequest is cast to an HttpServletRequest which is always the case since all requests use the HTTP protocol.") public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { try { // Store the session Id. HttpSession session = ((HttpServletRequest) servletRequest).getSession(); MDC.put(SESSION_ID_KEY, "sessionId=" + session.getId()); String userId = ""; // Try to extract the actual username from the security context. if (SecurityContextHolder.getContext().getAuthentication() != null) { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof User) { User user = (User) principal; userId = user.getUsername(); } else { userId = principal.toString(); } } MDC.put(USER_ID_KEY, "userId=" + userId); // Call the next filter in the chain. chain.doFilter(servletRequest, servletResponse); } finally { // Remove the MDC properties to ensure they don't accidentally get used by anybody else. MDC.remove(USER_ID_KEY); MDC.remove(SESSION_ID_KEY); } }