use of org.springframework.security.core.userdetails.UserDetails in project opennms by OpenNMS.
the class OpenNMSUserDetailsService method loadUserByUsername.
/** {@inheritDoc} */
@Override
public UserDetails loadUserByUsername(final String rawUsername) throws UsernameNotFoundException, DataAccessException {
final String username;
if (m_trimRealm && rawUsername.contains("@")) {
username = rawUsername.substring(0, rawUsername.indexOf("@"));
} else {
username = rawUsername;
}
final UserDetails userDetails = m_userDao.getByUsername(username);
if (userDetails == null) {
throw new UsernameNotFoundException("Unable to locate " + username + " in the userDao");
}
return userDetails;
}
use of org.springframework.security.core.userdetails.UserDetails in project opennms by OpenNMS.
the class KerberosLdapAuthenticationProvider method authenticate.
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
/*
* The incoming username will be in the form of a Kerberos user principal name,
* e.g. user@EXAMPLE.ORG. We typically need to strip off the realm name before
* doing any LDAP operations with the username.
*/
String validatedUsername = trimRealmFromUsername(m_kerberosClient.login(auth.getName(), auth.getCredentials().toString()));
DirContextOperations ldapUserEntry = m_ldapUserSearch.searchForUser(validatedUsername);
Collection<? extends GrantedAuthority> grantedAuthorities = m_ldapAuthoritiesPopulator.getGrantedAuthorities(ldapUserEntry, validatedUsername);
UserDetails userDetails = new User(validatedUsername, "notUsed", true, true, true, true, grantedAuthorities);
UsernamePasswordAuthenticationToken output = new UsernamePasswordAuthenticationToken(userDetails, auth.getCredentials(), grantedAuthorities);
return output;
}
use of org.springframework.security.core.userdetails.UserDetails in project opennms by OpenNMS.
the class SpringSecurityContextService method hasRole.
@Override
public boolean hasRole(String role) {
boolean hasRole = false;
UserDetails userDetails = getUserDetails();
if (userDetails != null) {
Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
if (isRolePresent(authorities, role)) {
hasRole = true;
}
}
return hasRole;
}
use of org.springframework.security.core.userdetails.UserDetails in project motan by weibocom.
the class UserController method getUser.
/**
* Retrieves the currently logged in user.
*
* @return A transfer containing the username and the roles.
*/
@RequestMapping(value = "", method = RequestMethod.GET)
public UserTransfer getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof AnonymousAuthenticationToken) {
throw new CustomException.UnauthorizedException();
}
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return new UserTransfer(userDetails.getUsername(), createRoleMap(userDetails));
}
use of org.springframework.security.core.userdetails.UserDetails in project motan by weibocom.
the class AuthenticationTokenProcessingFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = getAsHttpRequest(request);
String authToken = extractAuthTokenFromRequest(httpServletRequest);
String username = TokenUtils.getUserNameFromToken(authToken);
if (username != null) {
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if (TokenUtils.validateToken(authToken, userDetails)) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
chain.doFilter(request, response);
}
Aggregations