启用/禁用Shiro Security - Apache Camel

时间:2015-12-28 09:19:05

标签: java security apache-camel shiro blueprint-osgi

我正在使用Apache Camel路由,我想启用/禁用Shiro安全性。

这是路线(blueprint.xml):

<bean id="shiroPolicy" class="org.apache.camel.component.shiro.security.ShiroSecurityPolicy">
     <argument value="shiro.ini"/>
 </bean>

<route>
<from uri="bean:com.ngt.secured.ShiroSecurity?method=tokeninject(Exchange)"/>
                <policy ref="shiroPolicy">
                        <to uri="bean:com.ngt.secured.transform?method=addrip(Exchange)"/>
                         ...
                         some process..
                </policy>
</route>

这是令牌注入(ShiroSecurity.java):

public void tokeninject(Exchange exchange) throws Exception
           {
           ShiroSecurityToken shiroSecurityToken = new ShiroSecurityToken(login,password);
           TestShiroSecurityTokenInjector shiroSecurityTokenInjector = new     TestShiroSecurityTokenInjector(shiroSecurityToken, passPhrase);
           shiroSecurityTokenInjector.process(exchange);
}
private static class TestShiroSecurityTokenInjector extends ShiroSecurityTokenInjector {
                   public TestShiroSecurityTokenInjector(ShiroSecurityToken shiroSecurityToken, byte[] bytes)
                   {
                        super(shiroSecurityToken, bytes);
                   }

                    public void process(Exchange exchange) throws Exception {
                        exchange.getIn().setHeader("SHIRO_SECURITY_TOKEN", encrypt());
                        //exchange.getIn().setBody("Beatle Mania");
                    }
           }

在这种情况下启用安全性。我该如何禁用它?是否有设置开/关的选项?

1 个答案:

答案 0 :(得分:0)

我在寻找解决我遇到的驼峰问题的过程中偶然发现了这一点。希望回应不会太迟。

在apache camel中没有选项/属性来设置shiro安全开/关。您可以通过在路由设置中包含一个选项来实现此目的,如下所示,并在注入安全令牌时将on / off选项注入头属性:

<route>
<from uri="bean:com.ngt.secured.ShiroSecurity?method=tokeninject(Exchange)"/>
<choice>
    <when>
        <simple>${in.header.isSecured} == 'true'</simple>
        <policy ref="shiroPolicy">
            <to uri="bean:com.ngt.secured.transform?method=addrip(Exchange)"/>
             ...
             some process..
        </policy>
    </when>
    <otherwise>
        <to uri="bean:com.ngt.secured.transform?method=addrip(Exchange)"/>
         ...
         some process..
    </otherwise>
</choice>            
</route>

public void process(Exchange exchange) throws Exception {
    exchange.getIn().setHeader("SHIRO_SECURITY_TOKEN", encrypt());
    exchange.getIn().setHeader("isSecured", Boolean.FALSE);
    //exchange.getIn().setBody("Beatle Mania");
}