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:tds.tdsadmin.web.backingbean.UserBean.java

public boolean hasPermission() {
    SbacUser user = (SbacUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (user.hasPermission("Opportunity Read") || user.hasPermission("Opportunity Modify"))
        return true;
    return false;
}

From source file:com.healthcit.analytics.service.ReportTemplateService.java

@RemoteMethod
public boolean checkIfReportTitleExists(String title, boolean shared) {
    Long userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
    boolean exists = manager.checkIfReportTitleExists(title, userId, shared);

    log.info(title + " exists: " + exists);

    return exists;
}

From source file:com.epam.ipodromproject.controller.MainPageController.java

@RequestMapping(value = "changeNameOfUser", method = RequestMethod.POST)
@ResponseBody/*from  w  w  w  .j  a v  a 2s  . co  m*/
public String changeNameOfUser(@RequestParam(value = "name") String name) {
    User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    if (user.getEnabled() == false) {
        return "this_user_is_blocked";
    }
    if (userService.changeNameOfUser(user.getUsername(), name)) {
        return "name_changed_successfully";
    } else {
        return "Name_4_to_15_characters";
    }
}

From source file:org.hdiv.logs.UserData.java

public String getUsername(HttpServletRequest request) {

    // Find username in JEE standard security
    Principal principal = request.getUserPrincipal();
    if (principal != null && principal.getName() != null) {
        return principal.getName();
    }/*from w  ww  . ja  v  a2 s . c o  m*/

    // Find username in Spring Security
    if (springSecurityPresent) {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        if (securityContext != null && securityContext.getAuthentication() != null) {
            return securityContext.getAuthentication().getName();
        }
    }

    // Return anonymous
    return IUserData.ANONYMOUS;
}

From source file:no.dusken.aranea.admin.control.MenuController.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Person p = null;/*  ww w .  j  av  a  2 s .c  o  m*/
    if (authentication != null) {
        String username = authentication.getName();
        p = personService.getPersonByUsername(username);
    }
    if (p != null) {
        map.put("loggedInUser", p);
    }
    return new ModelAndView("no/dusken/aranea/base/admin/common/menu", map);
}

From source file:com.company.project.web.controller.service.CustomAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    SecurityContext context = SecurityContextHolder.getContext();
    Object principalObj = context.getAuthentication().getPrincipal();
    String principal = ((UserDetails) principalObj).getUsername();
    // Create the User Cookie    
    if (((UserDetails) principalObj).getAuthorities() != null && ((UserDetails) principalObj).getAuthorities()
            .contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
        response.sendRedirect("/admin");
    } else {// www  .  ja va 2s. c  o m
        response.sendRedirect("/welcome");
    }
}

From source file:com.auditbucket.helper.SecurityHelper.java

public String getLoggedInUser() {
    Authentication a = SecurityContextHolder.getContext().getAuthentication();
    if (a == null)
        throw new SecurityException("User is not authenticated");
    return a.getName();
}

From source file:com.jeanchampemont.notedown.security.AuthenticationService.java

public User getCurrentUser() {
    String email = SecurityContextHolder.getContext().getAuthentication().getName();
    return userRepository.findByEmailIgnoreCase(email);
}

From source file:com.mec.Security.JWTAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    //System.out.println("JWTAuthenticationFilter doFilter");
    HttpServletRequest res = (HttpServletRequest) request;
    Authentication authentication = tokenService.getAuthentication(res);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    filterChain.doFilter(request, response);
}

From source file:io.syndesis.rest.v1.handler.user.UserHandlerTest.java

@Test
public void successfulWhoAmI() {
    openShiftServer.expect().get().withPath("/oapi/v1/users/~").andReturn(200,
            new UserBuilder().withFullName("Test User").withNewMetadata().withName("testuser").and().build())
            .once();// ww  w.ja  v a2  s . com

    SecurityContextHolder.getContext()
            .setAuthentication(new PreAuthenticatedAuthenticationToken("testuser", "doesn'tmatter"));

    UserHandler userHandler = new UserHandler(null,
            new OpenShiftServiceImpl(openShiftServer.getOpenshiftClient(), null));
    User user = userHandler.whoAmI();
    Assertions.assertThat(user).isNotNull();
    Assertions.assertThat(user.getUsername()).isEqualTo("testuser");
    Assertions.assertThat(user.getFullName()).isNotEmpty().hasValue("Test User");
}