Example usage for org.springframework.security.core Authentication getPrincipal

List of usage examples for org.springframework.security.core Authentication getPrincipal

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getPrincipal.

Prototype

Object getPrincipal();

Source Link

Document

The identity of the principal being authenticated.

Usage

From source file:com.cb.controllers.WebSocketsController.java

@RequestMapping("/403")
public ModelAndView getAccessDenied() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String username = "";
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        UserDetails userDetail = (UserDetails) auth.getPrincipal();
        username = userDetail.getUsername();
    }//w w  w . ja v  a  2s .  c om

    return new ModelAndView("403", "username", username);
}

From source file:com.web.mavenproject6.controller.MainController.java

@RequestMapping(value = "/403", method = RequestMethod.GET)
public ModelAndView accesssDenied() {

    ModelAndView model = new ModelAndView();

    //check if user is login
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        UserDetails userDetail = (UserDetails) auth.getPrincipal();
        model.addObject("username", userDetail.getUsername());
    }//from  ww  w .  j  a va  2  s. c o  m
    model.setViewName("thy/error/403");
    return model;

}

From source file:org.test.skeleton.core.security.MessagePermissionEvaluator.java

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    if (authentication == null) {
        return false;
    }// w ww.  j a  v a  2s . c o m
    Message message = (Message) targetDomainObject;
    if (message == null) {
        return true;
    }
    User currentUser = (User) authentication.getPrincipal();
    Long currentId = currentUser.getId();
    return currentId.equals(message.getTo().getId()) || currentId.equals(message.getFrom().getId());
}

From source file:sample.security.MessagePermissionEvaluator.java

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    if (authentication == null) {
        return false;
    }/*from  w ww.j a  v  a 2s. c o  m*/
    Message message = (Message) targetDomainObject;
    if (message == null) {
        return true;
    }
    User currentUser = (User) authentication.getPrincipal();
    return currentUser.getId().equals(message.getTo().getId());
}

From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelper.java

/**
 * /*www  .j a  v a2 s .  c om*/
 * @param registration
 * @throws IneligibleException
 * @throws InputFormatException
 * @throws ParseException
 */
public void executeDelegateRegistration(final Registration registration)
        throws IneligibleException, InputFormatException, ParseException {
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    DelegateCalendarAccountUserDetailsImpl currentUser = (DelegateCalendarAccountUserDetailsImpl) authentication
            .getPrincipal();
    IScheduleOwner delegateOwner = ownerDao.register(currentUser.getCalendarAccount());
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.LOCATION, registration.getLocation());

    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.DURATIONS,
            registration.durationPreferenceValue());
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.MEETING_PREFIX,
            registration.getTitlePrefix());
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.NOTEBOARD,
            registration.getNoteboard());
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.VISIBLE_WINDOW,
            registration.visibleWindowPreferenceKey());
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.DEFAULT_VISITOR_LIMIT,
            Integer.toString(registration.getDefaultVisitorsPerAppointment()));
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.MEETING_LIMIT,
            Integer.toString(registration.getMeetingLimitValue()));
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.REFLECT_SCHEDULE,
            Boolean.toString(registration.isReflectSchedule()));
    delegateOwner = ownerDao.updatePreference(delegateOwner, Preferences.REMINDERS,
            registration.emailReminderPreferenceKey());

    if (registration.isScheduleSet()) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Set<AvailableBlock> blocks = AvailableBlockBuilder.createBlocks(registration.getStartTimePhrase(),
                registration.getEndTimePhrase(), registration.getDaysOfWeekPhrase(),
                dateFormat.parse(registration.getStartDatePhrase()),
                dateFormat.parse(registration.getEndDatePhrase()),
                registration.getDefaultVisitorsPerAppointment());
        availableScheduleDao.addToSchedule(delegateOwner, blocks);
    }

    if (registration.isReflectSchedule()) {
        reflectionService.reflectAvailableSchedule(delegateOwner);
    }
}

From source file:org.xaloon.wicket.security.spring.SpringSecurityFacade.java

@Override
public boolean isRegistered() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Object principal = authentication.getPrincipal();
    if (principal instanceof AuthenticationToken) {
        return false;
    }/*from   ww  w .  j av a2  s  .c  o  m*/
    return true;
}

From source file:com.sharmila.hibernatespringsecurity.controller.DefaultController.java

@RequestMapping(value = "/403", method = RequestMethod.GET)
public ModelAndView errorPage() {

    ModelAndView model = new ModelAndView();

    //check if user is login
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        UserDetails userDetail = (UserDetails) auth.getPrincipal();
        model.addObject("username", userDetail.getUsername());
    }/*from   ww  w  .  j  av  a  2s.  c  o m*/

    model.setViewName("error page");
    return model;
}

From source file:org.carewebframework.security.spring.AbstractAuthenticationProvider.java

/**
 * Authentication Provider. Produces a trusted <code>UsernamePasswordAuthenticationToken</code>
 * if/*from  w  w w . j a v a2  s  .  c o m*/
 * 
 * @param authentication The authentication context.
 * @return authentication Authentication object if authentication succeeded. Null if not.
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    CWFAuthenticationDetails details = (CWFAuthenticationDetails) authentication.getDetails();
    String username = (String) authentication.getPrincipal();
    String password = (String) authentication.getCredentials();
    String domain = null;

    if (log.isDebugEnabled()) {
        log.debug("User: " + username);
        log.debug("Details, RA: " + details == null ? "null" : details.getRemoteAddress());
    }

    if (username != null) {
        String pcs[] = username.split("\\\\", 2);
        domain = pcs[0];
        username = pcs.length > 1 ? pcs[1] : null;
    }

    ISecurityDomain securityDomain = domain == null ? null
            : SecurityUtil.getSecurityService().getSecurityDomain(domain);

    if (username == null || password == null || securityDomain == null) {
        throw new BadCredentialsException("Missing security credentials.");
    }

    IUser user = authenticate(username, password, securityDomain, details);
    details.setDetail("user", user);
    List<GrantedAuthority> userAuthorities = new ArrayList<GrantedAuthority>();
    List<String> list = getAuthorities(user);
    Set<String> authorities = list == null ? new HashSet<String>() : new HashSet<String>(list);

    for (String grantedAuthority : grantedAuthorities) {
        if (grantedAuthority.startsWith("-")) {
            authorities.remove(grantedAuthority.substring(1));
        } else {
            authorities.add(grantedAuthority);
        }
    }

    for (String authority : authorities) {
        if (!authority.isEmpty()) {
            userAuthorities.add(new SimpleGrantedAuthority(authority));
        }
    }

    User principal = new User(username, password, true, true, true, true, userAuthorities);

    authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
            principal.getAuthorities());
    ((UsernamePasswordAuthenticationToken) authentication).setDetails(details);
    return authentication;
}

From source file:org.xaloon.wicket.security.spring.SpringSecurityFacade.java

@Override
public KeyValue<String, String> getAlias() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Object principal = authentication.getPrincipal();
    if (principal instanceof AuthenticationToken) {
        AuthenticationToken token = (AuthenticationToken) principal;
        String loginType = token.getLoginType();
        return new DefaultKeyValue<String, String>(loginType, token.getName());
    }/*from   w  w w  .j a v a 2 s  . c  om*/
    return null;
}

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();// ww w.  jav  a 2 s.  c om
    final Authentication authentication = authenticate("http://localhost");
    Assertions.assertNotNull(authentication);
    Assertions.assertEquals("junit", authentication.getName());
    Assertions.assertEquals("junit", authentication.getPrincipal().toString());
}