Search in sources :

Example 81 with UserDetails

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;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails)

Example 82 with 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;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) DirContextOperations(org.springframework.ldap.core.DirContextOperations) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 83 with UserDetails

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;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails)

Example 84 with UserDetails

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));
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) UserTransfer(com.weibo.model.UserTransfer) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 85 with 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)97 Test (org.junit.Test)37 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)32 Authentication (org.springframework.security.core.Authentication)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 User (org.springframework.security.core.userdetails.User)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)9 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)3