Search in sources :

Example 46 with ObjectInputStream

use of java.io.ObjectInputStream in project camel by apache.

the class ShiroSecurityProcessor method applySecurityPolicy.

private void applySecurityPolicy(Exchange exchange) throws Exception {
    ByteSource encryptedToken;
    // if we have username and password as headers then use them to create a token
    String username = exchange.getIn().getHeader(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME, String.class);
    String password = exchange.getIn().getHeader(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD, String.class);
    if (username != null && password != null) {
        ShiroSecurityToken token = new ShiroSecurityToken(username, password);
        // store the token as header, either as base64 or as the object as-is
        if (policy.isBase64()) {
            ByteSource bytes = ShiroSecurityHelper.encrypt(token, policy.getPassPhrase(), policy.getCipherService());
            String base64 = bytes.toBase64();
            exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, base64);
        } else {
            exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, token);
        }
        // and now remove the headers as we turned those into the token instead
        exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_USERNAME);
        exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_PASSWORD);
    }
    Object token = ExchangeHelper.getMandatoryHeader(exchange, ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, Object.class);
    // we support the token in a number of ways
    if (token instanceof ShiroSecurityToken) {
        ShiroSecurityToken sst = (ShiroSecurityToken) token;
        encryptedToken = ShiroSecurityHelper.encrypt(sst, policy.getPassPhrase(), policy.getCipherService());
        // Remove unencrypted token + replace with an encrypted token
        exchange.getIn().removeHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN);
        exchange.getIn().setHeader(ShiroSecurityConstants.SHIRO_SECURITY_TOKEN, encryptedToken);
    } else if (token instanceof String) {
        String data = (String) token;
        if (policy.isBase64()) {
            byte[] bytes = Base64.decode(data);
            encryptedToken = ByteSource.Util.bytes(bytes);
        } else {
            encryptedToken = ByteSource.Util.bytes(data);
        }
    } else if (token instanceof ByteSource) {
        encryptedToken = (ByteSource) token;
    } else {
        throw new CamelExchangeException("Shiro security header " + ShiroSecurityConstants.SHIRO_SECURITY_TOKEN + " is unsupported type: " + ObjectHelper.classCanonicalName(token), exchange);
    }
    ByteSource decryptedToken = policy.getCipherService().decrypt(encryptedToken.getBytes(), policy.getPassPhrase());
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decryptedToken.getBytes());
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    ShiroSecurityToken securityToken;
    try {
        securityToken = (ShiroSecurityToken) objectInputStream.readObject();
    } finally {
        IOHelper.close(objectInputStream, byteArrayInputStream);
    }
    Subject currentUser = SecurityUtils.getSubject();
    // Authenticate user if not authenticated
    try {
        authenticateUser(currentUser, securityToken);
        // Test whether user's role is authorized to perform functions in the permissions list
        authorizeUser(currentUser, exchange);
    } finally {
        if (policy.isAlwaysReauthenticate()) {
            currentUser.logout();
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteSource(org.apache.shiro.util.ByteSource) Subject(org.apache.shiro.subject.Subject) ObjectInputStream(java.io.ObjectInputStream)

Example 47 with ObjectInputStream

use of java.io.ObjectInputStream in project okio by square.

the class TestUtil method reserialize.

/** Serializes original to bytes, then deserializes those bytes and returns the result. */
// Assume serialization doesn't change types.
@SuppressWarnings("unchecked")
public static <T extends Serializable> T reserialize(T original) throws Exception {
    Buffer buffer = new Buffer();
    ObjectOutputStream out = new ObjectOutputStream(buffer.outputStream());
    out.writeObject(original);
    ObjectInputStream in = new ObjectInputStream(buffer.inputStream());
    return (T) in.readObject();
}
Also used : ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 48 with ObjectInputStream

use of java.io.ObjectInputStream in project spring-security-oauth by spring-projects.

the class SerializationUtils method deserialize.

public static <T> T deserialize(byte[] byteArray) {
    ObjectInputStream oip = null;
    try {
        oip = new ConfigurableObjectInputStream(new ByteArrayInputStream(byteArray), Thread.currentThread().getContextClassLoader());
        @SuppressWarnings("unchecked") T result = (T) oip.readObject();
        return result;
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (oip != null) {
            try {
                oip.close();
            } catch (IOException e) {
            // eat it
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ConfigurableObjectInputStream(org.springframework.core.ConfigurableObjectInputStream) ConfigurableObjectInputStream(org.springframework.core.ConfigurableObjectInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 49 with ObjectInputStream

use of java.io.ObjectInputStream in project japid42 by branaway.

the class JapidRenderer method recoverClasses.

/**
	 * @author Bing Ran (bing.ran@gmail.com)
	 */
@SuppressWarnings("unchecked")
private static void recoverClasses() {
    String templateRoot = getClassCacheRoot();
    FileInputStream fos;
    File file = new File(new File(templateRoot), JAPID_CLASSES_CACHE);
    try {
        if (file.exists()) {
            // discard it if the file is too old
            long t = System.currentTimeMillis();
            if (t - file.lastModified() > 10000) {
                // too old
                JapidFlags.debug("the japid cache was too old. discarded.");
                file.delete();
            } else {
                fos = new FileInputStream(file);
                BufferedInputStream bos = new BufferedInputStream(fos);
                ObjectInputStream ois = new ObjectInputStream(bos);
                String version = (String) ois.readObject();
                JapidFlags.debug("Japid version: " + VERSION + ". JapidCache version: " + version);
                if (!version.equals(VERSION)) {
                    JapidFlags.debug("Japid classes mismatch. Discard cache.");
                } else {
                    japidClasses = (Map<String, RendererClass>) ois.readObject();
                    resourceJars = (HashSet<File>) ois.readObject();
                    HashSet<File> versionCheckedDirs = (HashSet<File>) ois.readObject();
                    JapidFlags.setVersionCheckedDirs(versionCheckedDirs);
                    JapidFlags.debug("recovered Japid classes from cache");
                }
                ois.close();
            }
        }
    } catch (Exception e) {
        JapidFlags.info("error in recovering class cache. Ignored: " + e);
    // e.printStackTrace();
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) HashSet(java.util.HashSet)

Example 50 with ObjectInputStream

use of java.io.ObjectInputStream in project HanLP by hankcs.

the class CharTable method loadBin.

private static boolean loadBin(String path) {
    try {
        ObjectInputStream in = new ObjectInputStream(IOUtil.newInputStream(path));
        CONVERT = (char[]) in.readObject();
        in.close();
    } catch (Exception e) {
        logger.warning("字符正规化表缓存加载失败,原因如下:" + e);
        return false;
    }
    return true;
}
Also used : ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20