【问题标题】:Is there a way to add a third party servlet in a grails application?有没有办法在 grails 应用程序中添加第三方 servlet?
【发布时间】:2013-02-22 00:52:53
【问题描述】:

我是 grails 新手,想知道是否有办法在 grails 应用程序中添加第三方 servlet?

我正在尝试将Waffle 与 grails 一起使用。我成功地使用了 Spring Security 在 MVC 应用程序中使用了 Waffle,如下所述:https://github.com/dblock/waffle/blob/master/Docs/spring/SpringSecurityAuthenticationProvider.md

在我的 MVC 应用程序中,我能够添加这样的 bean 进行身份验证:

<bean id="waffleNegotiateSecurityFilter" class="waffle.spring.NegotiateSecurityFilter">
    <property name="provider" ref="waffleSecurityFilterProviderCollection"/>
    <property name="allowGuestLogin" value="false"/>
    <property name="principalFormat" value="fqn"/>
    <property name="roleFormat" value="both"/>
</bean>

【问题讨论】:

  • 您是否尝试过使用 DSL 将该 bean 声明添加到您的 grails-app/conf/spring/resources.groovy 中?
  • 按照提供的链接,您不需要添加任何额外的过滤器。只需配置 Spring Security,使用标准 Spring Security 插件即可
  • @IgorArtamonov 今天下午我将尝试这种方法。我会回来报告的。谢谢

标签: grails spring-mvc spring-security waffle


【解决方案1】:

您必须添加过滤器映射到 web.xml

使用 grails 命令安装 web.xml

> grails install-templates

比编辑 web.xml 文件(在 src/templates 中)

并按照您向我们展示的文档中的说明添加映射。

然后将bean定义添加到grails资源中

/conf/spring/resources.groogy

将 xml bean 定义转换为 grails spring groovy DSL 可能有点困难。如果您有任何问题,请参考指南about grails and spring 或在此处提问。

【讨论】:

  • 您可以在 grails-app/conf/spring/resources.xml 中按原样使用 XML bean 配置,您不必将其转换为 groovy。
【解决方案2】:

我已经为此苦苦挣扎了几天,最终在一个普通的 Grails 2.4.4 项目中做了以下事情:

grails create-app
grails install-templates

然后修改BuildConfig.groovy

dependencies {
...
compile "com.google.guava:guava:18.0"
compile "com.github.dblock.waffle:waffle-jna:1.7.3"
compile "net.java.dev.jna:jna:4.1.0"
compile "net.java.dev.jna:jna-platform:4.1.0"
compile "org.slf4j:slf4j-api:1.7.9"
....

}

然后我在 ..\META-INF 下面创建了 context.xml,内容如下:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE Context>
<Context>
    <Valve className="waffle.apache.NegotiateAuthenticator" principalFormat="fqn" roleFormat="both" protocols="Negotiate,NTLM" />
    <Realm className="waffle.apache.WindowsRealm" />
</Context>

然后将以下内容添加到 ..\templates\web.xml 文件中:

<display-name>/@grails.project.key@</display-name>
<security-constraint>
    <display-name>Waffle Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>Protected Area</web-resource-name>
        <url-pattern>/</url-pattern>
        <http-method>DELETE</http-method>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>Everyone</role-name>
    </auth-constraint>
</security-constraint>
<security-role>
    <role-name>Everyone</role-name>
</security-role>
....
....

为了验证它是否确实有效,我在 index.gsp

中添加了一行
<p>You are logged in as remote user <b>${request.getRemoteUser()}</b> in session <b>${session.getId()}</b>.</p>

我在 Tomcat 7.0.57 下对此进行了测试,并且必须向 Tomcat 库添加一些 jar 才能让我工作。我添加了 slf4j-api-1.7.9.jar、guava-18.0.jar、jna-platform-4.1.0.jar、jna-4.1.0.jar、waffle-tomcat7-1.7.3.jar、waffle -jna-1.7.3.jar。仍然想知道为什么当同样的 jars 也被添加到 BuildConfig.groovy 时这实际上是必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2017-03-31
    相关资源
    最近更新 更多