Example usage for org.apache.shiro.util ThreadContext remove

List of usage examples for org.apache.shiro.util ThreadContext remove

Introduction

In this page you can find the example usage for org.apache.shiro.util ThreadContext remove.

Prototype

public static void remove() 

Source Link

Document

ThreadLocal#remove Remove s the underlying ThreadLocal ThreadLocal from the thread.

Usage

From source file:com.gemstone.gemfire.internal.security.GeodeSecurityUtil.java

License:Apache License

/**
 * @return null if security is not enabled, otherwise return a shiro subject
 *///from  ww  w.j a  v a  2  s.com
public static Subject login(String username, String password) {
    if (!isIntegratedSecurity) {
        return null;
    }

    // this makes sure it starts with a clean user object
    ThreadContext.remove();

    Subject currentUser = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    try {
        logger.info("Logging in " + username);
        currentUser.login(token);
    } catch (ShiroException e) {
        logger.info(e.getMessage(), e);
        throw new AuthenticationFailedException("Authentication error. Please check your username/password.",
                e);
    }

    return currentUser;
}

From source file:com.gemstone.gemfire.internal.security.GeodeSecurityUtil.java

License:Apache License

public static void logout() {
    Subject currentUser = getSubject();
    if (currentUser == null) {
        return;// w  w w . j a  va2 s.  c  o  m
    }

    try {
        logger.info("Logging out " + currentUser.getPrincipal());
        currentUser.logout();
    } catch (ShiroException e) {
        logger.info(e.getMessage(), e);
        throw new GemFireSecurityException(e.getMessage(), e);
    }
    // clean out Shiro's thread local content
    ThreadContext.remove();
}

From source file:com.gemstone.gemfire.internal.security.GeodeSecurityUtil.java

License:Apache License

public static void close() {
    if (securityManager != null) {
        securityManager.close();// w w w  . j av a  2  s.  c  o m
        securityManager = null;
    }

    if (postProcessor != null) {
        postProcessor.close();
        postProcessor = null;
    }
    ThreadContext.remove();
    isIntegratedSecurity = false;
    isClientAuthenticator = false;
    isPeerAuthenticator = false;
}

From source file:com.github.sdorra.shiro.ShiroRule.java

License:Open Source License

/**
 * Method description/*from  w w  w .  j  a va2 s . c  om*/
 *
 */
private void tearDownShiro() {
    try {
        SecurityManager securityManager = SecurityUtils.getSecurityManager();

        LifecycleUtils.destroy(securityManager);
        ThreadContext.unbindSecurityManager();
        ThreadContext.unbindSubject();
        ThreadContext.remove();
    } catch (UnavailableSecurityManagerException e) {

        // we don't care about this when cleaning up the test environment
        // (for example, maybe the subclass is a unit test and it didn't
        // need a SecurityManager instance because it was using only mock Subject instances)
    }

    SecurityUtils.setSecurityManager(null);
}

From source file:no.priv.bang.ukelonn.api.resources.JobResourceTest.java

License:Apache License

/**
 * To provoked the internal server error, the user isn't logged in.
 * This causes a NullPointerException in the user check.
 *
 * (In a production environment this request without a login,
 * will be stopped by Shiro)//from   w  ww  .j  av  a  2s .co  m
 *
 * @throws Exception
 */
@Test(expected = InternalServerErrorException.class)
public void testRegisterJobInternalServerError() throws Exception {
    // Create the request
    Account account = new Account();
    List<TransactionType> jobTypes = getUkelonnServiceSingleton().getJobTypes();
    PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(),
            jobTypes.get(0).getTransactionAmount(), new Date());

    // Create the object to be tested
    JobResource resource = new JobResource();

    // Create mock OSGi services to inject and inject it
    MockLogService logservice = new MockLogService();
    resource.logservice = logservice;

    // Inject fake OSGi service UkelonnService
    resource.ukelonn = getUkelonnServiceSingleton();

    // Clear the Subject to ensure that Shiro will fail
    // no matter what order test methods are run in
    ThreadContext.remove();

    // Run the method under test
    resource.doRegisterJob(job);
}

From source file:no.priv.bang.ukelonn.api.UkelonnRestApiServletTest.java

License:Apache License

/**
 * To provoked the internal server error, the user isn't logged in.
 * This causes a NullPointerException in the user check.
 *
 * (In a production environment this request without a login,
 * will be stopped by Shiro)//from   w  w w .j av  a 2s. co  m
 *
 * @throws Exception
 */
@Test
public void testRegisterJobInternalServerError() throws Exception {
    // Create the request
    Account account = new Account();
    List<TransactionType> jobTypes = getUkelonnServiceSingleton().getJobTypes();
    PerformedTransaction job = new PerformedTransaction(account, jobTypes.get(0).getId(),
            jobTypes.get(0).getTransactionAmount(), new Date());
    String jobAsJson = ServletTestBase.mapper.writeValueAsString(job);
    HttpServletRequest request = buildRequestFromStringBody(jobAsJson);
    when(request.getRequestURL())
            .thenReturn(new StringBuffer("http://localhost:8181/ukelonn/api/job/register"));
    when(request.getRequestURI()).thenReturn("/ukelonn/api/job/register");

    // Create a response object that will receive and hold the servlet output
    MockHttpServletResponse response = mock(MockHttpServletResponse.class, CALLS_REAL_METHODS);

    // Create mock OSGi services to inject
    MockLogService logservice = new MockLogService();

    // Create the servlet
    UkelonnRestApiServlet servlet = new UkelonnRestApiServlet();
    servlet.setLogservice(logservice);
    servlet.setUkelonnService(getUkelonnServiceSingleton());

    // Activate the servlet DS component
    servlet.activate();

    // When the servlet is activated it will be plugged into the http whiteboard and configured
    ServletConfig config = createServletConfigWithApplicationAndPackagenameForJerseyResources();
    servlet.init(config);

    // Clear the Subject to ensure that Shiro will fail
    // no matter what order test methods are run in
    ThreadContext.remove();

    // Run the method under test
    servlet.service(request, response);

    // Check the response
    assertEquals(500, response.getStatus());
}

From source file:org.apache.batchee.shiro.ShiroTest.java

License:Apache License

@AfterClass
public static void resetShiro() {
    ThreadContext.unbindSubject();
    ThreadContext.unbindSecurityManager();
    ThreadContext.remove();
}

From source file:org.apache.geode.internal.security.IntegratedSecurityService.java

License:Apache License

/**
 * @return null if security is not enabled, otherwise return a shiro subject
 *///from  w ww  .j a  v  a 2  s .c om
public Subject login(Properties credentials) {
    if (!isIntegratedSecurity()) {
        return null;
    }

    if (credentials == null)
        return null;

    // this makes sure it starts with a clean user object
    ThreadContext.remove();

    Subject currentUser = SecurityUtils.getSubject();
    GeodeAuthenticationToken token = new GeodeAuthenticationToken(credentials);
    try {
        logger.info("Logging in " + token.getPrincipal());
        currentUser.login(token);
    } catch (ShiroException e) {
        logger.info(e.getMessage(), e);
        throw new AuthenticationFailedException("Authentication error. Please check your credentials.", e);
    }

    return currentUser;
}

From source file:org.apache.geode.internal.security.IntegratedSecurityService.java

License:Apache License

public void logout() {
    Subject currentUser = getSubject();
    if (currentUser == null) {
        return;//from  ww  w  .  j a  v  a 2s  .  c  o m
    }

    try {
        logger.info("Logging out " + currentUser.getPrincipal());
        currentUser.logout();
    } catch (ShiroException e) {
        logger.info(e.getMessage(), e);
        throw new GemFireSecurityException(e.getMessage(), e);
    }
    // clean out Shiro's thread local content
    ThreadContext.remove();
}

From source file:org.apache.geode.internal.security.IntegratedSecurityService.java

License:Apache License

public void close() {
    if (securityManager != null) {
        securityManager.close();/*from   w w w .  j ava 2  s  . com*/
        securityManager = null;
    }

    if (postProcessor != null) {
        postProcessor.close();
        postProcessor = null;
    }
    ThreadContext.remove();
    SecurityUtils.setSecurityManager(null);
    isIntegratedSecurity = null;
    isClientAuthenticator = false;
    isPeerAuthenticator = false;
}