use of org.springframework.security.authentication.AnonymousAuthenticationToken in project ORCID-Source by ORCID.
the class IdentifierApiServiceDelegatorTest method init.
@Before
public void init() {
// setup security context
ArrayList<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
roles.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
Authentication auth = new AnonymousAuthenticationToken("anonymous", "anonymous", roles);
SecurityContextHolder.getContext().setAuthentication(auth);
}
use of org.springframework.security.authentication.AnonymousAuthenticationToken in project ORCID-Source by ORCID.
the class PublicV2ApiServiceDelegatorTest method before.
@Before
public void before() {
ArrayList<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
roles.add(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
Authentication auth = new AnonymousAuthenticationToken("anonymous", "anonymous", roles);
SecurityContextHolder.getContext().setAuthentication(auth);
}
use of org.springframework.security.authentication.AnonymousAuthenticationToken in project motan by weibocom.
the class UserController method getUser.
/**
* Retrieves the currently logged in user.
*
* @return A transfer containing the username and the roles.
*/
@RequestMapping(value = "", method = RequestMethod.GET)
public UserTransfer getUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof AnonymousAuthenticationToken) {
throw new CustomException.UnauthorizedException();
}
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return new UserTransfer(userDetails.getUsername(), createRoleMap(userDetails));
}
use of org.springframework.security.authentication.AnonymousAuthenticationToken in project motan by weibocom.
the class LoggingAspect method getUsername.
private String getUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof AnonymousAuthenticationToken) {
throw new CustomException.UnauthorizedException();
}
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return TokenUtils.getUserNameFromToken(userDetails.getUsername());
}
use of org.springframework.security.authentication.AnonymousAuthenticationToken in project midpoint by Evolveum.
the class SecurityEnforcerImpl method runPrivileged.
@Override
public <T> T runPrivileged(Producer<T> producer) {
LOGGER.debug("Running {} as privileged", producer);
Authentication origAuthentication = SecurityContextHolder.getContext().getAuthentication();
LOGGER.trace("ORIG auth {}", origAuthentication);
// Try to reuse the original identity as much as possible. All we need to is add AUTZ_ALL
// to the list of authorities
Authorization privilegedAuthorization = createPrivilegedAuthorization();
Object newPrincipal = null;
if (origAuthentication != null) {
Object origPrincipal = origAuthentication.getPrincipal();
if (origAuthentication instanceof AnonymousAuthenticationToken) {
newPrincipal = origPrincipal;
} else {
LOGGER.trace("ORIG principal {} ({})", origPrincipal, origPrincipal != null ? origPrincipal.getClass() : null);
if (origPrincipal != null) {
if (origPrincipal instanceof MidPointPrincipal) {
MidPointPrincipal newMidPointPrincipal = ((MidPointPrincipal) origPrincipal).clone();
newMidPointPrincipal.getAuthorities().add(privilegedAuthorization);
newPrincipal = newMidPointPrincipal;
}
}
}
Collection<GrantedAuthority> newAuthorities = new ArrayList<>();
newAuthorities.addAll(origAuthentication.getAuthorities());
newAuthorities.add(privilegedAuthorization);
PreAuthenticatedAuthenticationToken newAuthorization = new PreAuthenticatedAuthenticationToken(newPrincipal, null, newAuthorities);
LOGGER.trace("NEW auth {}", newAuthorization);
SecurityContextHolder.getContext().setAuthentication(newAuthorization);
} else {
LOGGER.debug("No original authentication, do NOT setting any privileged security context");
}
try {
return producer.run();
} finally {
SecurityContextHolder.getContext().setAuthentication(origAuthentication);
LOGGER.debug("Finished running {} as privileged", producer);
LOGGER.trace("Security context after privileged operation: {}", SecurityContextHolder.getContext());
}
}
Aggregations