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:com.folion.config.SocialConfiguration.java

@Override
public UserIdSource getUserIdSource() {
    return () -> {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
        }// w w  w.jav a2  s . c  o  m
        return authentication.getName();
    };
}

From source file:de.blizzy.documentr.web.system.SystemController.java

@RequestMapping(value = "/save", method = RequestMethod.POST)
@PreAuthorize("hasApplicationPermission(ADMIN)")
public String saveSettings(@ModelAttribute @Valid SystemSettingsForm form, BindingResult bindingResult,
        Authentication authentication) throws IOException {

    if (bindingResult.hasErrors()) {
        return "/system/edit"; //$NON-NLS-1$
    }//w ww . j av  a 2s  .co m

    User user = userStore.getUser(authentication.getName());

    Map<String, String> settings = Maps.newHashMap();
    String documentrHost = form.getDocumentrHost();
    // remove trailing slash
    documentrHost = StringUtils.removeEnd(documentrHost, "/"); //$NON-NLS-1$

    settings.put(SystemSettingsStore.DOCUMENTR_HOST, documentrHost);
    settings.put(SystemSettingsStore.SITE_NOTICE, form.getSiteNotice());
    settings.put(SystemSettingsStore.MAIL_HOST_NAME, form.getMailHostName());
    settings.put(SystemSettingsStore.MAIL_HOST_PORT, String.valueOf(form.getMailHostPort()));
    settings.put(SystemSettingsStore.MAIL_SENDER_EMAIL, form.getMailSenderEmail());
    settings.put(SystemSettingsStore.MAIL_SENDER_NAME, form.getMailSenderName());
    settings.put(SystemSettingsStore.MAIL_SUBJECT_PREFIX, form.getMailSubjectPrefix());
    settings.put(SystemSettingsStore.MAIL_DEFAULT_LANGUAGE, form.getMailDefaultLanguage());
    settings.put(SystemSettingsStore.BCRYPT_ROUNDS, String.valueOf(form.getBcryptRounds()));
    settings.put(SystemSettingsStore.PAGE_FOOTER_HTML, form.getPageFooterHtml());
    settings.put(SystemSettingsStore.UPDATE_CHECK_INTERVAL, form.getUpdateCheckInterval());
    systemSettingsStore.saveSettings(settings, user);

    for (Map.Entry<String, SortedMap<String, String>> entry : form.getMacroSettings().entrySet()) {
        systemSettingsStore.setMacroSetting(entry.getKey(), entry.getValue(), user);
    }

    return "redirect:/system/edit"; //$NON-NLS-1$
}

From source file:com.yj.config.SocialConfig.java

@Override
public UserIdSource getUserIdSource() {
    return new UserIdSource() {
        @Override//from  w w w  . ja  v  a2  s.co m
        public String getUserId() {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication == null) {
                throw new IllegalStateException("ConnectionRepository : no user signed in");
            }
            return authentication.getName();
        }
    };
}

From source file:com.solxiom.social.config.SocialConfig.java

@Bean
public UserIdSource userIdSource() {
    return new UserIdSource() {
        @Override/*from   w  ww  .  j av a2  s  . c  o m*/
        public String getUserId() {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication == null) {
                throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
            }
            return authentication.getName();
        }
    };
}

From source file:com.razorfish.security.AcceleratorAuthenticationProvider.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
    final String username = (authentication.getPrincipal() == null) ? "NONE_PROVIDED"
            : authentication.getName();
    String usernameResult = username;

    UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;

    if (!usernameResult.isEmpty()) {
        final List<CustomerModel> result = getCustomerDao().findCustomerByMobileNumber(usernameResult);
        if (!result.isEmpty()) {
            usernameResult = result.iterator().next().getOriginalUid();
            token = new UsernamePasswordAuthenticationToken(usernameResult,
                    (String) authentication.getCredentials());
            token.setDetails(authentication.getDetails());
        }/*ww w. j a  va  2 s  . c  om*/
    }

    if (getBruteForceAttackCounter().isAttack(usernameResult)) {
        try {
            final UserModel userModel = getUserService().getUserForUID(StringUtils.lowerCase(usernameResult));
            userModel.setLoginDisabled(true);
            getModelService().save(userModel);
            bruteForceAttackCounter.resetUserCounter(userModel.getUid());
        } catch (final UnknownIdentifierException e) {
            LOG.warn("Brute force attack attempt for non existing user name " + usernameResult);
        } finally {
            throw new BadCredentialsException(
                    messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
        }
    }

    checkCartForUser(usernameResult);
    return super.authenticate(token);
}

From source file:com.notemyweb.config.SocialConfig.java

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
    }/*from w w w.  j a  v a  2s  .co m*/
    return usersConnectionRepository.createConnectionRepository(authentication.getName());
}

From source file:de.uni_koeln.spinfo.maalr.login.LoginManager.java

public String getCurrentUserName() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (!loggedIn(currentUser))
        return null;
    // FIXME: Do not query for the current user if it is already known!!
    MaalrUserInfo current = backend.getByLogin(currentUser.getName());
    if (current != null && current.getFirstname() != null) {
        return current.getFirstname();
    }//from w  ww . j a  v a  2s  .c o  m
    return currentUser.getName();
}

From source file:de.uni_koeln.spinfo.maalr.login.LoginManager.java

public LightUserInfo getCurrentUser() {
    Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
    if (!loggedIn(currentUser))
        return null;
    // FIXME: Do not query for the current user if it is already known!!
    MaalrUserInfo current = backend.getByLogin(currentUser.getName());
    if (current != null) {
        return current.toLightUser();
    }//  w w  w .jav a  2 s  . c  o  m
    return null;
}

From source file:com.github.djabry.platform.service.security.DefaultSpringAuthenticationService.java

/**
 * @return The current logged in user/*from   w ww.  j a  v a 2 s.  com*/
 */

@Override
public DBUser getCurrentUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth.isAuthenticated() && currentUser != null) {
        String username = auth.getName();
        if (username.equals(currentUser.getUsername())) {
            return currentUser;
        }
    }

    currentUser = null;

    return null;
}

From source file:org.jutge.joc.porra.entitystash.service.EntityStashEntityService.java

public Account getAccount() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        final Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
        if (authorities != null && authorities.contains(USER_AUTHORITY)) {
            final String name = auth.getName();
            return this.accountService.getByName(name);
        }/*from  w  w  w .  j a  v a2 s . c  o m*/
    }
    return null;
}