【问题标题】:Camel Shiro Security骆驼士郎保安
【发布时间】:2015-12-15 09:19:23
【问题描述】:

我正在使用camel blueprint.xml,我想用Shiro 保护路线。我创建了以下类:

public class ShiroSecurity {
// Shiro configuration file path
   private static final String iniResourcePath = "shiro.ini";
   // Object used to encrypt/decrypt the token in the message.
   private static final byte[] passPhrase = {
         (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
         (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17};

   // list of token injectors used in this example
   private static ShiroSecurityTokenInjector shiroSecurityTokenInjector;

   /**
    * @param args
    * @throws Exception
    */
   public void test()
   {

      // Creation of the permission list supported by the policy.
      List<Permission> permissionsList = new ArrayList<Permission>();
      Permission permission = new WildcardPermission("zone1:readwrite:*");
      permissionsList.add(permission);

      ShiroSecurityPolicy policy = new ShiroSecurityPolicy(iniResourcePath, passPhrase, true, permissionsList);

 SimpleRegistry reg = new SimpleRegistry();
          reg.put("securityPolicy", policy);

          // Instanciating the Shiro tokens
      // The ShiroSecurityToken constructor receives the userName and the password as
      // parameters.
      ShiroSecurityToken shiroSecurityToken = new ShiroSecurityToken("paul", "mccartney");

      // Instanciating the Shiro TokenInjectors objects
      shiroSecurityTokenInjector = new ShiroSecurityTokenInjector(shiroSecurityToken, passPhrase);

   } }

使用 Java DSL,我们添加如下策略:

   final CamelContext context = new DefaultCamelContext(reg);
          context.addRoutes(new RouteBuilder()
         {
            public void configure() throws Exception
            {
               onException(UnknownAccountException.class).handled(true).to("log:UnknownAccountException").to(
                     "file:Error/UnknownAccountException");
               onException(IncorrectCredentialsException.class).handled(true).to("log:IncorrectCredentialsException")
                     .to("file:Error/IncorrectCredentialsException");
               onException(LockedAccountException.class).handled(true).to("log:LockedAccountException").to(
                     "file:Error/LockedAccountException");
               onException(AuthenticationException.class).handled(true).to("log:AuthenticationException").to(
                     "file:Error/AuthenticationException");
               onException(CamelAuthorizationException.class).handled(true).to(
                     "log:CamelAuthorizationException ${in.header}").to("file:Error/CamelAuthorizationException");
               onException(NullPointerException.class).handled(true).to("log:Message not secure").to(
                     "file:Error/NotSecureMessage");

               from("direct:client").process(shiroSecurityTokenInjector).policy(
                         (Policy) context.getRegistry().lookup("securityPolicy")).to("log:success");;

            }
         });

         context.start();

         ProducerTemplate template = context.createProducerTemplate();

         Endpoint endpoint = context.getEndpoint("direct:client");
         Exchange exchange = endpoint.createExchange();
         exchange.getIn().setBody("Data");
         template.send(endpoint, exchange);

但是我如何在 blueprint.xml 中做到这一点?

我读到应该是这样的:

    <route id="timerToLog">
          <from uri="timer:foo?period=5000"/>
          <log message="Start"/>
          <to uri="bean:com.ngt.shiro.ShiroSecurity?method=tokeninject()"/>
          <policy>..</policy>
    </route> 

【问题讨论】:

    标签: java apache-camel shiro blueprint


    【解决方案1】:

    可以在&lt;route&gt; 级别引用路由策略,即:

    鉴于“securityPolicy”bean 是在骆驼简单注册表中定义的。

    【讨论】:

    • 感谢您的回复!无法将 org.apache.camel.component.shiro.security.ShiroSecurityPolicy 转换为 org.apache.camel.spi.RoutePolicy
    • 看来路由本身应该使用 ShiroSecurityPolicy,比如
    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2015-05-05
    相关资源
    最近更新 更多