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.mitre.oauth2.model.SavedUserAuthentication.java

/**
 * Create a Saved Auth from an existing Auth token
 *///w ww .  ja  v  a 2s  .  com
public SavedUserAuthentication(Authentication src) {
    setName(src.getName());
    setAuthorities(src.getAuthorities());
    setAuthenticated(src.isAuthenticated());

    if (src instanceof SavedUserAuthentication) {
        // if we're copying in a saved auth, carry over the original class name
        setSourceClass(((SavedUserAuthentication) src).getSourceClass());
    } else {
        setSourceClass(src.getClass().getName());
    }
}

From source file:ru.trett.cis.controllers.HomeController.java

@RequestMapping("/profile")
public String profile(Model model) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = inventoryService.getUserByLoginName(auth.getName());
    if (user == null) {
        user = new User();
        user.setLoginName(auth.getName());
    }/*from   www . java  2  s .co  m*/
    model.addAttribute("user", user);
    return "home/profile";
}

From source file:com.alehuo.wepas2016projekti.controller.UploadController.java

/**
 * Kuvan lataussivu/*from w  ww  .  java  2  s. c  om*/
 * @param a Autentikointi
 * @param m Malli
 * @param l Locale
 * @return
 */
@RequestMapping(method = RequestMethod.GET)
public String upload(Authentication a, Model m, Locale l) {
    UserAccount u = userService.getUserByUsername(a.getName());
    m.addAttribute("user", u);
    m.addAttribute("imageUploadFormData", new ImageUploadFormData());
    return "upload";
}

From source file:cz.sohlich.workstack.security.StatelessLoginFilter.java

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        FilterChain chain, Authentication authentication) throws IOException, ServletException {

    User authenticatedUser = userDetailsService.loadUserByUsername(authentication.getName());
    final UserAuthentication userAuthentication = new UserAuthentication(authenticatedUser);
    tokenAuthenticationService.addAuthentication(response, userAuthentication);
    SecurityContextHolder.getContext().setAuthentication(userAuthentication);

    //Return info
    UserDTO userDTO = new UserDTO();
    userDTO.setFirstname(authenticatedUser.getFirstname());
    userDTO.setLastname(authenticatedUser.getLastname());
    userDTO.setUsername(authenticatedUser.getUsername());
    try (PrintWriter writer = response.getWriter()) {
        new ObjectMapper().writeValue(writer, userDTO);
    }//  www  . j a v a  2 s  .c om

}

From source file:controller.ChangepasswordController.java

@RequestMapping(value = "/process", method = RequestMethod.POST)
public String changepassProcess(Authentication authen, @RequestParam String newpass,
        @RequestParam String oldpass, ModelMap mm, HttpServletRequest req) {
    Customer cus = cusModel.find(authen.getName(), "username", false).get(0);
    if (!cus.getPassword().equals(oldpass)) {
        mm.put("check", true);
        mm.put("alert", "Old password invalid");
        mm.put("link", req.getContextPath() + "/customer/changepassword.html");
    } else {/*from ww  w  . j  a  v a  2  s.  c o m*/
        cus.setPassword(newpass);
        mm.put("check", cusModel.addOrUpdate(cus));
        mm.put("alert", "Password updated");
        mm.put("link", req.getContextPath());
    }
    return "changepassProcess";
}

From source file:io.onedecision.engine.decisions.web.UserProfileController.java

@RequestMapping(method = RequestMethod.GET, value = "/", headers = "Accept=application/json")
@ResponseBody/* w w w .ja v a 2s.com*/
public final Profile getProfile(Authentication auth) throws DecisionException {
    LOGGER.info(String.format("getProfile"));

    Profile profile = new Profile();
    profile.setUsername(auth.getName());
    for (GrantedAuthority authority : auth.getAuthorities()) {
        // trim ROLE_prefix
        profile.getRoles().add(authority.getAuthority().substring(5));
    }

    return profile;
}

From source file:ispok.helper.SessionListener.java

@Override
public void sessionDestroyed(HttpSessionEvent hse) {

    logger.entry();/*www. j a  va 2s . c  o  m*/
    logger.debug("Session destroyed: {}", hse.getSession().toString());

    //        Enumeration<String> attributeNames = hse.getSession().getAttributeNames();
    //        logger.debug("Attributes: ");
    //        while (attributeNames.hasMoreElements()) {
    //            logger.debug(attributeNames.nextElement());
    //        }
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null) {
        logger.debug("Username: {}", authentication.getName());
    }

    logger.exit();
}

From source file:de.blizzy.documentr.web.access.RoleController.java

@RequestMapping(value = "/delete/{roleName:" + DocumentrConstants.ROLE_NAME_PATTERN
        + "}", method = RequestMethod.GET)
@PreAuthorize("hasApplicationPermission(ADMIN) and !isLastAdminRole(#roleName)")
public String deleteRole(@PathVariable String roleName, Authentication authentication) throws IOException {
    User user = userStore.getUser(authentication.getName());
    userStore.deleteRole(roleName, user);
    return "redirect:/roles"; //$NON-NLS-1$
}

From source file:shiver.me.timbers.security.web.advanced.spring.UserAuthenticationConverterTest.java

@Test
public void Can_create_an_authentication() {

    final UserRepository userRepository = mock(UserRepository.class);

    final String subject = someString();

    final User user = mock(User.class);

    // Given/*from  w  w w. j av  a2 s.co  m*/
    given(userRepository.findByUsername(subject)).willReturn(user);
    given(user.getUsername()).willReturn(subject);

    // When
    final Authentication actual = new UserAuthenticationConverter(userRepository).convert(subject);

    // Then
    assertThat(actual.getName(), equalTo(subject));
}

From source file:springchat.service.MessagesServiceImpl.java

@Override
@Secured({ "ROLE_CHATUSER" })
public void postMessage(final String message) {
    jmsTemplate.send("java:comp/env/NewTextMessageChannel", new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
            Message notification = session.createMessage();
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            notification.setStringProperty("user", auth.getName());
            notification.setLongProperty("date", new Date().getTime());
            notification.setStringProperty("message", message);
            return notification;
        }//w w w .ja  va2s . c  o  m
    });
}