Java Code Examples for org.springframework.security.core.context.SecurityContextHolder#setStrategyName()

The following examples show how to use org.springframework.security.core.context.SecurityContextHolder#setStrategyName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PurRepositoryIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
protected void setUpUser() {
  StandaloneSession pentahoSession = new StandaloneSession( userInfo.getLogin() );
  pentahoSession.setAuthenticated( userInfo.getLogin() );
  pentahoSession.setAttribute( IPentahoSession.TENANT_ID_KEY, "/pentaho/" + EXP_TENANT );
  List<GrantedAuthority> authorities = new ArrayList<>( 2 );
  authorities.add( new SimpleGrantedAuthority( "Authenticated" ) );
  authorities.add( new SimpleGrantedAuthority( "acme_Authenticated" ) );
  final String password = "ignored"; //$NON-NLS-1$
  UserDetails userDetails = new User( userInfo.getLogin(), password, true, true, true, true, authorities );
  Authentication authentication = new UsernamePasswordAuthenticationToken( userDetails, password, authorities );
  // next line is copy of SecurityHelper.setPrincipal
  pentahoSession.setAttribute( "SECURITY_PRINCIPAL", authentication );
  SecurityContextHolder.setStrategyName( SecurityContextHolder.MODE_GLOBAL );
  PurRepositoryTestingUtils.setSession( pentahoSession, authentication );
  repositoryLifecyleManager.newTenant();
  repositoryLifecyleManager.newUser();
}
 
Example 2
Source File: UIEERepositoryDirectoryIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void setUpUser() {
  StandaloneSession pentahoSession = new StandaloneSession( userInfo.getLogin() );
  pentahoSession.setAuthenticated( userInfo.getLogin() );
  pentahoSession.setAttribute( IPentahoSession.TENANT_ID_KEY, "/pentaho/" + EXP_TENANT );
  List<GrantedAuthority> authorities = new ArrayList<>( 2 );
  authorities.add( new SimpleGrantedAuthority( "Authenticated" ) );
  authorities.add( new SimpleGrantedAuthority( "acme_Authenticated" ) );
  final String password = "ignored"; //$NON-NLS-1$
  UserDetails userDetails = new User( userInfo.getLogin(), password, true, true, true, true, authorities );
  Authentication authentication = new UsernamePasswordAuthenticationToken( userDetails, password, authorities );
  // next line is copy of SecurityHelper.setPrincipal
  pentahoSession.setAttribute( "SECURITY_PRINCIPAL", authentication );
  PentahoSessionHolder.setSession( pentahoSession );
  SecurityContextHolder.setStrategyName( SecurityContextHolder.MODE_GLOBAL );
  SecurityContextHolder.getContext().setAuthentication( authentication );
  repositoryLifecyleManager.newTenant();
  repositoryLifecyleManager.newUser();
}
 
Example 3
Source File: SecurityConfig.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(HttpSecurity http) throws Exception {
    final String ADMIN = ApplicationProperties.get("jwala.role.admin");
    final String AUTH = ApplicationProperties.get(JWALA_AUTH_ENABLED, "true");

    http.authorizeRequests().antMatchers(GEN_PUBLIC_RESOURCES, PUBLIC_RESOURCES, LOGIN_PAGE, LOGIN_API, LOGOUT_API)
            .permitAll().and().formLogin().loginPage(LOGIN_PAGE).permitAll().and().authorizeRequests().anyRequest()
            .authenticated();
    http.csrf().disable();

    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_THREADLOCAL);
}
 
Example 4
Source File: CurrentUserResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
private void setCurrentUserDetails(final UserDetails userDetails) {
    final Authentication authentication = mock(Authentication.class);
    final UserEntity userEntity = new UserEntity();
    userEntity.setId(ID);
    userEntity.setRoles(Collections.emptySet());

    when(authentication.getPrincipal()).thenReturn(userDetails);
    when(userService.findByIdWithRoles(USER_NAME)).thenReturn(userEntity);

    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
    SecurityContextHolder.setContext(new SecurityContextImpl(authentication));
}
 
Example 5
Source File: AuthResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldLogin() {
    final UserDetails userDetails = new UserDetails(USER_NAME, "PASSWORD", Collections.emptyList());
    
    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(userDetails);
    
    final SecurityContext securityContext = mock(SecurityContext.class);
    when(securityContext.getAuthentication()).thenReturn(authentication);

    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
    SecurityContextHolder.setContext(securityContext);

    Cookie bearer = new Cookie("FOO", "BAR");
    doReturn(bearer).when(cookieGenerator).generate(any());
    
    final Response response = target().path("login").request().post(null);
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    
    Token token = response.readEntity(Token.class);
    assertNotNull(token);
    assertNotNull(token.getToken());
    assertNotEquals("", token.getToken());
    assertEquals(TokenTypeEnum.BEARER, token.getTokenType());
    
    //APIPortal: can't test Cookie, since servletResponse is mocked
    
}
 
Example 6
Source File: SecurityContextHolderConfig.java    From agile-service-old with Apache License 2.0 4 votes vote down vote up
public SecurityContextHolderConfig() {
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
}
 
Example 7
Source File: CurrentUserResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void afterClass() {
    // Clean up Spring security context.
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_THREADLOCAL);
}
 
Example 8
Source File: AuthResourceTest.java    From gravitee-management-rest-api with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void afterClass() {
    // Clean up Spring security context.
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_THREADLOCAL);
}
 
Example 9
Source File: WebSecurityContext.java    From syncope with Apache License 2.0 4 votes vote down vote up
public WebSecurityContext() {
    super(true);
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
}
 
Example 10
Source File: SecurityJavaConfig.java    From tutorials with MIT License 4 votes vote down vote up
public SecurityJavaConfig() {
    super();
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
}