Example usage for org.springframework.security.core.context SecurityContextHolder getContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder getContext

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContextHolder getContext.

Prototype

public static SecurityContext getContext() 

Source Link

Document

Obtain the current SecurityContext.

Usage

From source file:dtu.ds.warnme.utils.SecurityUtils.java

public static String getCurrentUserUsername() {
    if (SecurityContextHolder.getContext() == null
            || SecurityContextHolder.getContext().getAuthentication() == null) {
        return null;
    }//from   w w  w .j  av  a  2s .  c om
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication.getName() == null) {
        throw new IllegalStateException("There is user authenticated without username?");
    }
    return authentication.getName();
}

From source file:mb.MbKonsultacije.java

public void dodajKonsultacije(Konsultacije k) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    k.setProfesorId((Profesor) mbKorisnik.nadjiPoMailu(auth.getName()));
    sBKonsultacijeLocal.dodajKonsultacije(k);
}

From source file:com.mycompany.apps.web.resources.MemberResource.java

@GET
@Path("/getMyInfo")
public String getMyInfo() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    com.mycompany.apps.domain.dto.Users principal = (com.mycompany.apps.domain.dto.Users) auth.getPrincipal();

    Users users = usersMapper.selectByUsername(principal.getUsername());
    if (users == null) {
        throw new UnsupportedOperationException("User not found...");
    }/*from   ww  w .j  ava 2s  . c  o m*/
    return "\n\tProtected Resource(getMyInfo) Accessed !!!! Returning from Myresource getMyInfo";
}

From source file:org.zkoss.reference.developer.spring.security.SecurityUtil.java

/**
 * Return the current Authentication object.
 *//*from   w w w  . j av  a 2  s  .com*/
public static User getUser() {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        try {
            Object p = auth.getPrincipal();
            if (p instanceof User)
                return (User) p;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }
    return null;
}

From source file:mvc.CheckUserController.java

private boolean isAnonymous() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    return (auth instanceof AnonymousAuthenticationToken);
}

From source file:it.geosolutions.geobatch.ui.security.CurrentUser.java

public String getUserName() {
    return SecurityContextHolder.getContext().getAuthentication().getName();
}

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 {/*w  ww  . j a v  a 2s.  co m*/
        return new ModelAndView("redirect:/login");
    }
}

From source file:cs544.letmegiveexam.controller.LoginController.java

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(Model model, HttpSession session) {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName();

    if (session.getAttribute("user") == null) {
        User user = userServices.getUserByUsername(name);
        session.setAttribute("user", user);
    }//from w  w w.  ja v  a  2 s.c o  m
    //ModelAndView model = new ModelAndView();
    List<Subject> subjects = subjectService.getAllSubjects();

    //        for (Subject sub : subjects) {
    //            System.out.println("Subject:" + sub.getName() + "  Description:" + sub.getDescription());
    //
    //        }
    model.addAttribute("subjects", subjects);
    return "welcome";
}

From source file:org.n52.oss.ui.controllers.RemoteController.java

@RequestMapping("/index")
public String index(ModelMap map) {
    UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal();//  w  ww.  j a  v a  2s . co m
    String token = userDetails.getPassword();
    map.addAttribute("token", token);
    return "remote/index";
}

From source file:pl.altkom.gemalto.spring.ws.CrmWebServiceImpl.java

private void bySpringScurity() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username

    Collection<? extends GrantedAuthority> roles = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();/*from  www  .  jav  a2  s. c  o  m*/

    System.out.println(roles);

    System.out.println("logged user by spring = " + name);
}