Search in sources :

Example 61 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project cia by Hack23.

the class HttpSessionDestroyedEventListener method onApplicationEvent.

@Override
public void onApplicationEvent(final HttpSessionDestroyedEvent event) {
    final HttpSession httpSession = event.getSession();
    final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
    authorities.add(new SimpleGrantedAuthority(ROLE_ANONYMOUS));
    final DestroyApplicationSessionRequest destroyApplicationSessionRequest = new DestroyApplicationSessionRequest();
    destroyApplicationSessionRequest.setSessionId(httpSession.getId());
    SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken(KEY, PRINCIPAL, authorities));
    applicationManager.service(destroyApplicationSessionRequest);
    SecurityContextHolder.getContext().setAuthentication(null);
    LOGGER.info(LOG_MSG_SESSION_DESTROYED_SESSION_ID, httpSession.getId());
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) DestroyApplicationSessionRequest(com.hack23.cia.service.api.action.application.DestroyApplicationSessionRequest) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 62 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project irida by phac-nml.

the class IridaApiServicesConfig method createAnalysisTaskSecurityContext.

/**
 * Creates a security context object for the analysis tasks.
 *
 * @param userService
 *            A {@link UserService}.
 *
 * @return A {@link SecurityContext} for the analysis tasks.
 */
private SecurityContext createAnalysisTaskSecurityContext(UserService userService) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    Authentication anonymousToken = new AnonymousAuthenticationToken("nobody", "nobody", ImmutableList.of(Role.ROLE_ANONYMOUS));
    Authentication oldAuthentication = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(anonymousToken);
    User admin = userService.getUserByUsername("admin");
    SecurityContextHolder.getContext().setAuthentication(oldAuthentication);
    Authentication adminAuthentication = new PreAuthenticatedAuthenticationToken(admin, null, Lists.newArrayList(Role.ROLE_ADMIN));
    context.setAuthentication(adminAuthentication);
    return context;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 63 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project irida by phac-nml.

the class IridaScheduledTasksConfig method createSchedulerSecurityContext.

/**
 * Creates a security context object for the scheduled tasks.
 *
 * @return A {@link SecurityContext} for the scheduled tasks.
 */
private SecurityContext createSchedulerSecurityContext() {
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    Authentication anonymousToken = new AnonymousAuthenticationToken("nobody", "nobody", ImmutableList.of(Role.ROLE_ANONYMOUS));
    Authentication oldAuthentication = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(anonymousToken);
    User admin = userService.getUserByUsername("admin");
    SecurityContextHolder.getContext().setAuthentication(oldAuthentication);
    Authentication adminAuthentication = new PreAuthenticatedAuthenticationToken(admin, null, Lists.newArrayList(Role.ROLE_ADMIN));
    context.setAuthentication(adminAuthentication);
    return context;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 64 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project irida by phac-nml.

the class PasswordResetController method setAuthentication.

/**
 * Set an anonymous authentication token
 */
private void setAuthentication() {
    AnonymousAuthenticationToken anonymousToken = new AnonymousAuthenticationToken("nobody", "nobody", ImmutableList.of(Role.ROLE_ANONYMOUS));
    SecurityContextHolder.getContext().setAuthentication(anonymousToken);
}
Also used : AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 65 with AnonymousAuthenticationToken

use of org.springframework.security.authentication.AnonymousAuthenticationToken in project beetl2.0 by javamonkey.

the class AuthenticationFunction method call.

@Override
public Object call(Object[] paras, Context ctx) {
    // 获取安全上下文
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext != null ? securityContext.getAuthentication() : null;
    // 获取认证凭证
    return ((authentication == null) || (authentication instanceof AnonymousAuthenticationToken)) ? null : authentication;
}
Also used : Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Aggregations

AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)87 Authentication (org.springframework.security.core.Authentication)37 Test (org.junit.jupiter.api.Test)22 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)17 ArrayList (java.util.ArrayList)14 GrantedAuthority (org.springframework.security.core.GrantedAuthority)13 SecurityContext (org.springframework.security.core.context.SecurityContext)10 MidpointAuthentication (com.evolveum.midpoint.authentication.api.config.MidpointAuthentication)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 ModuleAuthentication (com.evolveum.midpoint.authentication.api.config.ModuleAuthentication)6 Before (org.junit.Before)5 Test (org.junit.Test)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 FilterChain (jakarta.servlet.FilterChain)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3