使用单个 htpasswd 之类的密码保护 devel grails 应用程序的安全

发布于 2024-10-04 01:17:24 字数 208 浏览 3 评论 0原文

我正在向一些同事展示一个公共领域的 grails 应用程序。到目前为止,我正在开发模式下工作,尚未通过战争进行部署。

我需要保护应用程序的安全,以防止任何人查看/使用它。我已经有了一个用户管理,但在某人看到任何东西之前我希望有类似 .htpasswd 的保护。如果可能的话,我不想用插件(例如shiro)来扩大应用程序本身。

有什么想法/建议吗?

多谢!

I am showing a grails app to some colleagues on a public domain. So far I am working in devel mode and have not deployed via war.

I need to secure the application in order to keep onybody from checking it out / playing with it. I have a user mgmt in place already, but before sb sees anything I would like to have .htpasswd-like protection. If possible, I do not want to enlarge the application itself with plugins (e.g., shiro).

Any thoughts/suggestions?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-10-11 01:17:24

您可以使用 HTTP 身份验证。 HTTP 身份验证实现起来非常简单,但它不是很安全或不可用。您最好使用 shiro 或 spring-security 来获得真正的解决方案。也就是说,一个简单的过滤器可以检查 HTTP 授权标头,如果不存在则返回 401 状态代码。这将迫使浏览器弹出用户名/密码框,并重新提交表单,并在标题中编码用户名和密码。

Grails 过滤器必须有一个以“Filters”结尾的类名,并且位于 grails-app/conf 目录中。这是一个例子:

class SimpleAuthFilters {
    def USERNAME = "foo"
    def PASSWORD = "bar"

    static filters = {
        httpAuth(uri:"/**") {
            before = {
                def authHeader = request.getHeader('Authorization')
                if (authHeader) {
                    def usernamePassword = new String(authHeader.split(' ')[1].decodeBase64())
                    if (usernamePassword == "$USERNAME:$PASSWORD") {
                        return true
                    }
                }
                response.setHeader('WWW-Authenticate', 'basic realm="myRealm"')
                response.sendError(response.SC_UNAUTHORIZED)
                return false
            }
        }
    }
}

You could use HTTP authentication. HTTP authentication is dead simple to implement, but it's not very secure or usable. You're better off using shiro or spring-security for a real solution. That said, a simple filter can check for an HTTP Authorization header and return 401 status code if not present. That will force the browser to pop up a username/password box, and resubmit the form with the username and password encoded in the headers.

Grails filters must have a class name that ends with "Filters" and go in the grails-app/conf directory. Here's an example:

class SimpleAuthFilters {
    def USERNAME = "foo"
    def PASSWORD = "bar"

    static filters = {
        httpAuth(uri:"/**") {
            before = {
                def authHeader = request.getHeader('Authorization')
                if (authHeader) {
                    def usernamePassword = new String(authHeader.split(' ')[1].decodeBase64())
                    if (usernamePassword == "$USERNAME:$PASSWORD") {
                        return true
                    }
                }
                response.setHeader('WWW-Authenticate', 'basic realm="myRealm"')
                response.sendError(response.SC_UNAUTHORIZED)
                return false
            }
        }
    }
}
猫卆 2024-10-11 01:17:24

将以下内容添加到 $CATALINA_HOME/conf/tomcat-users.xml 并重新启动 Tomcat:

<role rolename="role1"/>
<user username="user1" password="password1" roles="role1"/>

在 Grails 项目根目录中,执行 grails install-templates。这会将src/templates/war/web.xml放入项目中。
(如果该文件在 IDE 中不可见,这可能是一个错误。然后在文件系统中找到它。)

将以下内容添加到 web.xml(作为 web-app 标记的子级):

<security-constraint>
  <web-resource-collection>
    <web-resource-name>
      Entire Application
    </web-resource-name>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
      <role-name>role1</role-name>
  </auth-constraint>
</security-constraint>

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>Restricted Area</realm-name>
</login-config>

Add the following to $CATALINA_HOME/conf/tomcat-users.xml and restart Tomcat:

<role rolename="role1"/>
<user username="user1" password="password1" roles="role1"/>

At your Grails project root, execute grails install-templates. This will place src/templates/war/web.xml into the project.
(In case the file's not visible in your IDE, this might be a bug. Then find it in the file system.)

Add the following to web.xml (as a child of the web-app tag) :

<security-constraint>
  <web-resource-collection>
    <web-resource-name>
      Entire Application
    </web-resource-name>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
      <role-name>role1</role-name>
  </auth-constraint>
</security-constraint>

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>Restricted Area</realm-name>
</login-config>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文