Example usage for org.springframework.security.ldap.userdetails InetOrgPerson getUsername

List of usage examples for org.springframework.security.ldap.userdetails InetOrgPerson getUsername

Introduction

In this page you can find the example usage for org.springframework.security.ldap.userdetails InetOrgPerson getUsername.

Prototype

@Override
    public String getUsername() 

Source Link

Usage

From source file:org.meruvian.yama.security.UserService.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    String username = null;/*from  w w  w  .  ja  va2 s  .com*/
    String authority = null;

    if (authentication.getPrincipal() instanceof User) {
        User u = (User) authentication.getPrincipal();
        username = u.getUsername();
    } else if (authentication.getPrincipal() instanceof InetOrgPerson) {
        InetOrgPerson person = (InetOrgPerson) authentication.getPrincipal();
        username = person.getUsername();
        authority = person.getAuthorities().isEmpty() ? null
                : person.getAuthorities().iterator().next().getAuthority();
    }

    net.bogor.itu.entity.admin.User us = userService.findByUsername(username);
    BackendUser user = us.getUser();
    if (authority != null)
        user.setRole(authority);

    request.getSession().setAttribute(SessionCredentials.YAMA_SECURITY_USER, user);
    request.getSession().setAttribute(SessionCredentials.YAMA_SECURITY_USER_DETAIL, us);

    if (StringUtils.isBlank(request.getParameter("redirectUri"))) {
        super.onAuthenticationSuccess(request, response, authentication);
    } else {
        setTargetUrlParameter("redirectUri");
        handle(request, response, authentication);
    }
}