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:br.com.joaops.smt.security.SmtAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication a) throws AuthenticationException {
    String username = a.getName();
    String password = a.getCredentials().toString();

    UserDetails user = this.userDetails.loadUserByUsername(username);

    if (user == null) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }//from   w  w w  . j  ava  2  s. c  om

    if (!user.getUsername().equals(username)) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }

    if (!passwordEncoder.matches(password, user.getPassword())) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",
                "AbstractUserDetailsAuthenticationProvider.badCredentials");
        throw new BadCredentialsException(message);
    }

    if (user.isEnabled() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
                "AbstractUserDetailsAuthenticationProvider.disabled");
        throw new DisabledException(message);
    }

    if (user.isAccountNonLocked() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.locked",
                "AbstractUserDetailsAuthenticationProvider.locked");
        throw new LockedException(message);
    }

    if (user.isAccountNonExpired() == false) {
        String message = this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
                "AbstractUserDetailsAuthenticationProvider.expired");
        throw new AccountExpiredException(message);
    }

    if (user.isCredentialsNonExpired() == false) {
        String message = this.messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.credentialsExpired",
                "AbstractUserDetailsAuthenticationProvider.credentialsExpired");
        throw new CredentialsExpiredException(message);
    }

    return new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.WebAnnoLoggingFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        setLoggingUsername(authentication.getName());
    }//from  ww w. j  a  v  a  2s .c  o  m
    try {
        chain.doFilter(req, resp);
    } finally {
        if (authentication != null) {
            clearLoggingUsername();
        }
    }
}

From source file:org.meruvian.yama.webapi.config.oauth.UserTokenConverter.java

public Map<String, ?> convertUserAuthentication(Authentication authentication) {
    Map<String, Object> response = new LinkedHashMap<String, Object>();
    response.put(USERNAME, authentication.getName());

    if (authentication.getPrincipal() instanceof DefaultUserDetails) {
        DefaultUserDetails details = (DefaultUserDetails) authentication.getPrincipal();
        response.put(USER_ID, details.getId());
    }//from  w  ww  .  j  av a 2 s  . co m

    if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
        response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
    }

    return response;
}

From source file:com.rambird.miles.repository.jdbc.JdbcCatgRepositoryImpl.java

/**
 * Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name <i>starts</i> with
 * the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not
 * already loaded./*from w  w w.j  av a 2  s.c  o m*/
 */
@Override
public Collection<Category> findAll() throws DataAccessException {
    Map<String, Object> params = new HashMap<String, Object>();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    params.put("userName", auth.getName());
    List<Category> categories = this.namedParameterJdbcTemplate.query(
            "SELECT catgid, catg, catg_label, user_name, home, catg_rank FROM category where user_name= :userName  order by catg_rank",
            params, ParameterizedBeanPropertyRowMapper.newInstance(Category.class));

    return categories;
}

From source file:de.itsvs.cwtrpc.sample1.server.service.SampleServiceImpl.java

public String getInfo() {
    final StringBuilder info = new StringBuilder();
    final Authentication auth;
    boolean first;

    auth = SecurityContextHolder.getContext().getAuthentication();
    log.info("User '" + auth.getName() + "' is requesting info");

    info.append("Number of Requests: " + (++infoCount) + "\n");
    info.append("User Name: " + auth.getName() + "\n");
    info.append("Roles: ");

    first = true;/*from w w  w.  j  av a  2s .  co  m*/
    for (GrantedAuthority ga : auth.getAuthorities()) {
        if (!first) {
            info.append(", ");
        }
        first = false;
        info.append(ga.getAuthority());
    }

    return info.toString();
}

From source file:cn.edu.zjnu.acm.judge.controller.MailController.java

@PostMapping("/send")
@SuppressWarnings("AssignmentToMethodParameter")
public String send(@RequestParam("title") String title, @RequestParam("to") String to,
        @RequestParam("content") String content, Authentication authentication) {
    String userId = authentication != null ? authentication.getName() : null;
    if (StringUtils.isEmptyOrWhitespace(title)) {
        title = "No Topic";
    }/*  w  w  w.  j  av a  2  s.  c om*/
    if (content.length() > 40000) {
        throw new MessageException("Sorry, content too long", HttpStatus.PAYLOAD_TOO_LARGE);
    }
    if (userMapper.findOne(to) == null) {
        throw new MessageException("Sorry, no such user:" + to, HttpStatus.NOT_FOUND);
    }

    mailMapper.save(Mail.builder().from(userId).to(to).title(title).content(content).build());
    return "mails/sendsuccess";
}

From source file:com.rambird.miles.repository.jdbc.JdbcCatgRepositoryImpl.java

@Override
public void save(Category category) throws DataAccessException {
    BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(category);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    category.setUserName(auth.getName()); //get logged in username

    if (category.isNew()) {
        Number newKey = this.insertMileCatg.executeAndReturnKey(parameterSource);
        category.setCatgid(newKey.intValue());
    } else {//  ww w. j  a v  a2 s  .c  o m
        this.namedParameterJdbcTemplate.update(
                "UPDATE category SET catg=:catg, catg_label=:catgLabel, home=:home WHERE catgid=:catgid",
                parameterSource);
    }
}

From source file:de.hybris.telcotrail.storefront.security.GUIDAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response,
        final Authentication authentication) throws IOException, ServletException {
    CustomerModel customerModel = (CustomerModel) userService.getUserForUID(authentication.getName());
    customerModel.setAttemptCount(0);/*from   www . j ava 2s  .  c  o  m*/
    modelService.save(customerModel);

    getGuidCookieStrategy().setCookie(request, response);
    getAuthenticationSuccessHandler().onAuthenticationSuccess(request, response, authentication);
}

From source file:org.btc4j.ws.impl.BtcDaemonServicePortImpl.java

private BtcDaemon getDaemon(String method) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    LOG.info(auth.getName() + "@" + daemonUrl + "/" + method);
    return new BtcDaemon(daemonUrl, String.valueOf(auth.getName()), String.valueOf(auth.getCredentials()));
}

From source file:fr.mcc.ginco.audit.tracking.GincoRevListener.java

@Override
public void newRevision(Object revisionEntity) {
    GincoRevEntity gincoRevEntity = (GincoRevEntity) revisionEntity;
    if (RequestContextHolder.getRequestAttributes() == null) {
        logger.error("The RequestContext is empty!!!!!");
    } else {/*from  ww  w .j  a v  a  2 s.c  om*/
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        gincoRevEntity.setUsername(name);
    }
}