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:co.com.carpco.altablero.spring.web.controller.TeacherController.java

@RequestMapping(value = "/admin/profesor", method = RequestMethod.GET)
public ModelAndView generalInformation() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (!(auth instanceof AnonymousAuthenticationToken)) {

        ModelAndView model = roleUtils.createModelWithUserDetails(auth.getName());
        model.setViewName("admin/teacher/edit");
        return model;
    } else {/*from  www  . j a  v  a 2  s.  co m*/
        return new ModelAndView("redirect:/login");
    }
}

From source file:org.openinfinity.core.aspect.AuditTrailAspect.java

private void writeUsernameToAuditTrailIfEnabled(ArgumentBuilder builder, AuditTrail auditTrail,
        Authentication authentication) {
    if (auditTrail.isUsernameEnabled()) {
        String username = authentication != null ? authentication.getName() : "user not authenticated";
        builder.append(" Username: ").append("[").append(username).append("] ");
    }/*  w w w .j  a va 2  s . c  o  m*/
}

From source file:com.traffitruck.web.JsonController.java

@RequestMapping(value = "/load_for_truck_by_radius", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Load> getLoadsForTruckByDistance(@RequestParam("licensePlateNumber") String licensePlateNumber,
        @RequestParam("sourceLat") Double sourceLat, @RequestParam("sourceLng") Double sourceLng,
        @RequestParam("destinationLat") Double destinationLat,
        @RequestParam("destinationLng") Double destinationLng,
        @RequestParam(value = "source_radius", required = false) Integer source_radius,
        @RequestParam(value = "destination_radius", required = false) Integer destination_radius) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String username = authentication.getName();
    // verify the truck belongs to the logged-in user
    if (licensePlateNumber == null || licensePlateNumber.isEmpty() || licensePlateNumber.equals("NA")) {
        // set default value for radius if not set
        if (sourceLat != null && sourceLng != null && source_radius == null) {
            source_radius = DEFAULT_RADIUS_FOR_SEARCHES;
        }//from w  ww. ja va2s .c  o m
        if (destinationLat != null && destinationLng != null && destination_radius == null) {
            destination_radius = DEFAULT_RADIUS_FOR_SEARCHES;
        }
        return dao.getLoadsWithoutTruckByFilter(sourceLat, sourceLng, source_radius, destinationLat,
                destinationLng, destination_radius);
    } else {
        Truck truck = dao.getTruckByUserAndLicensePlate(username, licensePlateNumber);
        if (truck == null) { // the logged in user does not have a truck with this license plate number
            return Collections.emptyList();
        }
        // set default value for radius if not set
        if (sourceLat != null && sourceLng != null && source_radius == null) {
            source_radius = DEFAULT_RADIUS_FOR_SEARCHES;
        }
        if (destinationLat != null && destinationLng != null && destination_radius == null) {
            destination_radius = DEFAULT_RADIUS_FOR_SEARCHES;
        }

        return dao.getLoadsForTruckByFilter(truck, sourceLat, sourceLng, source_radius, destinationLat,
                destinationLng, destination_radius);
    }
}

From source file:de.thm.arsnova.controller.LoginControllerTest.java

@Test
public void testReuseGuestLogin() throws Exception {
    mockMvc.perform(get("/doLogin").param("type", "guest").param("user", "Guest1234567890"))
            .andExpect(status().isOk());

    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
    assertEquals("Guest1234567890", auth.getName());

}

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

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication.isAuthenticated()) {
        return authentication;
    }//from w  w  w .  j  a v  a 2  s. c o m
    return login(authentication.getName(), (String) authentication.getCredentials());
}

From source file:de.hybris.platform.acceleratorstorefrontcommons.security.AbstractAcceleratorAuthenticationProvider.java

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

    if (getBruteForceAttackCounter().isAttack(username)) {
        try {/* w  ww  .j a  v  a2  s.  com*/
            final UserModel userModel = getUserService().getUserForUID(StringUtils.lowerCase(username));
            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 " + username);
        }

        throw new BadCredentialsException(
                messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));

    }

    return super.authenticate(authentication);

}

From source file:cz.PA165.vozovyPark.controller.DriveController.java

public String getLoggedUsername() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    return auth.getName();
}

From source file:com.github.jens_meiss.blog.server.service.json.user.UserController.java

@Override
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {

    final String userName = authentication.getName();

    final UserDetailsDTO userDetailsDTO = userService.findByUserName(userName);
    if (userDetailsDTO == null) {
        logger.error("username not found");
        return null;
    }//from  ww  w . j  a v  a  2s  .  c  om

    final String crendentials = authentication.getCredentials().toString();
    if (crendentials.equals(userDetailsDTO.getPassword()) == false) {
        logger.error("password mismatch");
        return null;
    }

    logger.debug("user successfully authenticated");
    return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
            authentication.getCredentials(), new ArrayList<GrantedAuthority>());
}

From source file:org.ligoj.app.http.security.RestAuthenticationProviderTest.java

@Test
public void authenticateOverrideSameUser() {
    httpServer.stubFor(post(urlPathEqualTo("/"))
            .willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT).withHeader("X-Real-User", "junit")));
    httpServer.start();/*w ww . j a v a2 s . c o  m*/
    final Authentication authentication = authenticate("http://localhost");
    Assertions.assertNotNull(authentication);
    Assertions.assertEquals("junit", authentication.getName());
    Assertions.assertEquals("junit", authentication.getPrincipal().toString());
}