2015-02-23 60 views
0

我们有一个运行在Tomcat 7.0.56上的简单Web应用程序。现在我们想使用我们的 自己的领域进行身份验证。如何在与Tomcat的战争中交付领域?

public class SpecialAuth extends DataSourceRealm{ 
    @Override 
    public Principal authenticate(String username, String credentials){ 
     .... 
    } 
} 

这是在/META-INF/context.xml定义的战争

<Context> 
    <Realm className="some.package.SpecialAuth" dataSourceName="jdbc/MySQL" /> 
</Context> 

哪里摆放SpecialAuth.class里面?

我们预计什么只是有SpecialAuth.class我们的战争中,但随后我们正在启动

Caused by: java.lang.ClassNotFoundException: some.package.BackOfficeAuth 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    .... 

越来越folling例外,如果我们做一个罐子,把它变成$ TOMCAT/lib中的一切工作正常。

但这不可能是解决方案!这意味着每次我在这个类上工作时,都必须触摸我的tomcat服务器,并且不能使用正常的部署。

我如何使用内置的身份验证机制不接触tomcat的所有timeß

+0

你可能喜欢它,也可能不喜欢它,但'$ TOMCAT/lib'就是答案。 – EJP 2015-02-23 08:40:26

+0

如果这是您部署的唯一方式?你真的关闭tomcat,复制库,​​做其他部署的东西,启动tomcat?而不是部署它? – Marcel 2015-02-23 09:12:17

+0

你也可以将它添加到tomcat启动类路径中。 – ramp 2015-02-23 11:02:32

回答

1

正如你说的,我不喜欢你的答案:)所以我做了什么(我敢肯定,100%你不喜欢它)是设置可能的方式dirties领域,但现在我可以运行它ontouched tomcat。在163个验收测试之后,似乎没有任何事情可以打破

public final class ContextListener implements ServletContextListener { 
    @Override 
    public void contextInitialized(ServletContextEvent event) { 
     ServletContext servletContext = event.getServletContext(); 
     TomcatContextManipulator tomcat = new TomcatContextManipulator(servletContext); 
     tomcat.applyRealm(new MyOwnRealm()); 
    } 
} 

public class TomcatContextManipulator { 
    private static final String EXPEXCTED_TOMCAT_VERSION = "7.0.52.0"; 

    private ApplicationContextFacade servletContext; 

    /** 
    * @param servletContext must be of type {@link ApplicationContextFacade} 
    */ 
    public TomcatContextManipulator(ServletContext servletContext) { 
     checkTomcatVersion(); 

     ensureEquals(servletContext.getClass(), ApplicationContextFacade.class, "class of servletContext"); 
     this.servletContext = (ApplicationContextFacade) servletContext; 
    } 

    /** 
    * checks if the correct version of tomcat is in use, throws {@link IllegalStateException} if not 
    */ 
    private void checkTomcatVersion() { 
     // we use several internal parts of tomcat (for example with reflection) 
     // by doing this we bind ourself hardly to a explicit version 
     ensureEquals(EXPEXCTED_TOMCAT_VERSION, ServerInfo.getServerNumber(), "Tomcat-Server-Version"); 
    } 

    /** 
    * overrides the existing realm with the given on 
    */ 
    public void applyRealm(Realm realm) { 
     ensureNotNull(realm, "realm"); 
     ApplicationContext applicationContext = (ApplicationContext) ReflectionUtil.get(servletContext, "context"); 
     StandardContext standardContext = (StandardContext) ReflectionUtil.get(applicationContext, "context"); 
     standardContext.setRealm(realm); 
    } 
} 

注:

  • Reflection.get()返回给定对象
  • 保证的(私人)实例变量的值...()是一样的断言......但它抛出异常