Search in sources :

Example 86 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project head by mifos.

the class AuthenticationAuthorizationServiceFacadeImpl method reloadUserDetailsForSecurityContext.

@Override
public void reloadUserDetailsForSecurityContext(String username) {
    UserDetails userSecurityDetails = loadUserByUsername(username);
    MifosUser reloadedUserDetails = (MifosUser) userSecurityDetails;
    SecurityContext securityContext = SecurityContextHolder.getContext();
    if (securityContext == null) {
        securityContext = new SecurityContextImpl();
        SecurityContextHolder.setContext(securityContext);
    }
    Authentication authentication = new UsernamePasswordAuthenticationToken(reloadedUserDetails, reloadedUserDetails, reloadedUserDetails.getAuthorities());
    securityContext.setAuthentication(authentication);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 87 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project uPortal by Jasig.

the class PortalPersonUserDetailsTest method testUserDetails.

@Test
public void testUserDetails() {
    UserDetails details = new PortalPersonUserDetails(person);
    assertEquals("testuser", details.getUsername());
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) Test(org.junit.Test)

Example 88 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project Settler by EmhyrVarEmreis.

the class XAuthTokenFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    try {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        String authToken = httpServletRequest.getHeader(XAUTH_TOKEN_HEADER_NAME);
        if (!StringUtils.hasText(authToken) && httpServletRequest.getMethod().equals("POST"))
            authToken = httpServletRequest.getParameter(XAUTH_TOKEN_HEADER_NAME);
        if (StringUtils.hasText(authToken)) {
            String username = this.tokenProvider.getUserNameFromToken(authToken);
            UserDetails details = this.detailsService.loadUserByUsername(username);
            if (tokenProvider.validateToken(authToken, details)) {
                UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(details, details.getPassword(), details.getAuthorities());
                SecurityContextHolder.getContext().setAuthentication(token);
                httpServletResponse.addHeader(XAUTH_TOKEN_HEADER_NAME, tokenProvider.createToken(details));
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserDetails(org.springframework.security.core.userdetails.UserDetails) HttpServletResponse(javax.servlet.http.HttpServletResponse) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 89 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 90 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)111 Test (org.junit.Test)42 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)33 Authentication (org.springframework.security.core.Authentication)32 GrantedAuthority (org.springframework.security.core.GrantedAuthority)17 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)15 User (org.springframework.security.core.userdetails.User)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)10 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)9 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 Transactional (org.springframework.transaction.annotation.Transactional)6 Date (java.util.Date)4 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 AuthenticationException (org.springframework.security.core.AuthenticationException)4