Example usage for org.springframework.security.core Authentication getName

List of usage examples for org.springframework.security.core Authentication getName

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getName.

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:org.appverse.web.framework.backend.api.services.presentation.impl.live.AuthenticationServiceFacadeImpl.java

@Override
public String getPrincipal() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication.getName();
}

From source file:se.kth.csc.auth.UserService.java

@Transactional
@Override//from   ww w  .j  ava 2s .  c o m
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {
    if (!token.getName().startsWith("u1")) {
        // See http://intra.kth.se/it/driftsinformation-webbtjanster/anstallda/inloggning-maste-ske-med-sma-bokstaver-1.475521
        // which allows an exploit. Counter-measured by only allowing usernames starting with "u1"
        throw new UsernameNotFoundException("This username is not in the u1 realm and was probably forged");
    }

    Account account = accountStore.fetchAccountWithPrincipalName(token.getName());

    if (account == null) {
        account = new Account();
        account.setPrincipalName(token.getName());
        for (GrantedAuthority grantedAuthority : token.getAuthorities()) {
            if (Role.ADMIN.getAuthority().equals(grantedAuthority.getAuthority())) {
                account.setAdmin(true);
                break;
            }
        }
        accountStore.storeAccount(account);

        log.info("Created user called \"{}\" with id {} and principal {}", account.getName(), account.getId(),
                account.getPrincipalName());
    }
    String name = nameService.nameUser(token.getName());

    if (account.getName() == null || !account.getName().equals(name)) {
        account.setName(name);
        log.info("User with id {} and principal {} is now called \"{}\"", account.getId(),
                account.getPrincipalName(), name);
    }

    return createUser(account);
}

From source file:com.pokerweb.servlets.holdem.SelectUserCommand.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   ww  w  .ja v a2 s  .  c o  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    StringBuilder jb = new StringBuilder();
    String line = null;
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null)
        jb.append(line);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    TableStatus.GetInstance().SetButtonSettingsResponce(jb.toString(), auth.getName());
    response.setContentType("application/json; charset=utf-8");
    response.setHeader("Cache-Control", "no-cache");
    response.getWriter().write("{error:null}");
}

From source file:no.dusken.aranea.admin.control.GenericEditController.java

@Override
protected Map referenceData(HttpServletRequest httpServletRequest, Object o, Errors errors) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String username = authentication.getName();
    Person p = personService.getPersonByUsername(username);
    if (p != null) {
        map.put("loggedInUser", p);
    }/*from w w w .j  av  a 2 s  .  c  o  m*/
    return map;
}

From source file:uk.co.threeonefour.ifictionary.web.user.service.DaoUserService.java

public uk.co.threeonefour.ifictionary.web.user.model.User getLoggedInUser() {

    // TODO use session
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        String username = auth.getName();
        if (auth.isAuthenticated() && username != null && !username.equals("anonymousUser")) {
            org.springframework.security.core.userdetails.User userDetails = (org.springframework.security.core.userdetails.User) auth
                    .getPrincipal();/*from  w w w  . jav  a  2  s . co  m*/
            User user = userDao.findUser(userDetails.getUsername());

            List<Role> roles = new ArrayList<Role>();
            for (GrantedAuthority authority : userDetails.getAuthorities()) {
                roles.add(Role.valueOf(authority.getAuthority()));
            }

            user.setRoles(roles);

            return user;
        }
    }
    return null;
}

From source file:org.socialsignin.springsocial.security.signin.SpringSocialSecurityAuthenticationFactory.java

public Authentication updateAuthenticationForNewProvider(Authentication existingAuthentication,
        String providerId) {//from   www. j a  va2s  .c  om
    return createNewAuthentication(existingAuthentication.getName(),
            existingAuthentication.getCredentials() == null ? null
                    : existingAuthentication.getCredentials().toString(),
            addAuthority(existingAuthentication, userAuthoritiesService.getProviderAuthority(providerId)));
}

From source file:authentication.RoleVetor.java

@Override
public int vote(final Authentication authentication, final Object object,
        final Collection<ConfigAttribute> attributes) {
    int result = ACCESS_ABSTAIN;
    final String username = authentication.getName();
    for (ConfigAttribute attribute : attributes) {
        if (this.supports(attribute)) {
            result = ACCESS_DENIED;//www  .  ja v  a2s .  c o m
            final String stringedAtribute = attribute.getAttribute();
            final String shortedAtribute = attribute.getAttribute().substring(stringedAtribute.indexOf("_") + 1,
                    stringedAtribute.length());
            if (shortedAtribute.equalsIgnoreCase(username)) {
                return ACCESS_GRANTED;
            }
        }
    }
    return result;
}

From source file:com.orchestra.portale.controller.UserEditController.java

@RequestMapping("userEditProfile")
@Secured("ROLE_USER")
public ModelAndView editUser(HttpServletRequest request) {
    ModelAndView model = new ModelAndView("userEditProfile", "command", new User());
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = pm.findUserByUsername(auth.getName());

    HttpSession session = request.getSession();
    ServletContext sc = session.getServletContext();
    File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
            + File.separator + user.getId() + File.separator + "avatar.jpg");
    if (dir.exists()) {
        model.addObject("avatar", "./dist/user/img/" + user.getId() + "/avatar.jpg");
    } else {// w  ww.j av a  2s  . c om

        model.addObject("avatar", "./dist/img/default_avatar.png");
    }

    model.addObject("user", user);

    return model;

}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.AuthzAuthenticationManagerTests.java

@Test
public void successfulAuthenticationReturnsTokenAndPublishesEvent() throws Exception {
    when(db.retrieveUserByName("auser")).thenReturn(user);
    Authentication result = mgr.authenticate(createAuthRequest("auser", "password"));

    assertNotNull(result);/*www .  java 2  s  . c  o  m*/
    assertEquals("auser", result.getName());
    assertEquals("auser", ((UaaPrincipal) result.getPrincipal()).getName());

    verify(publisher).publishEvent(isA(UserAuthenticationSuccessEvent.class));
}

From source file:by.bsuir.finance.controllers.AccountantController.java

public String getUserName() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username
    return name;/*from  www.j  a  va 2 s  .c om*/
}