代码之家  ›  专栏  ›  技术社区  ›  fluxon

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

  •  5
  • fluxon  · 技术社区  · 15 年前

    我正在向一些公共领域的同事展示一个grails应用程序。到目前为止,我在devel模式下工作,还没有通过war部署。

    我需要保护应用程序,以防止onybody检查它/玩它。我已经有了一个用户管理系统,但是在某人看到任何我想拥有的东西之前。如果可能的话,我不想用插件(例如shiro)扩展应用程序本身。

    有什么想法/建议吗?

    谢谢!

    2 回复  |  直到 15 年前
        1
  •  5
  •   ataylor    15 年前

    您可以使用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
                }
            }
        }
    }
    
        2
  •  4
  •   robbbert    15 年前

    在$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中不可见,这可能是一个错误。然后在文件系统中找到它。)

    Add the following 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>