`

【飞天奔月出品】一步一步升级配置10:给测试环境的tomcat也加把锁

阅读更多

背景:

 

大家都知道,只要一个应用有外部地址, 就会有人来访问,各种你都不知道的什么途径来访问你的应用

 

如果你的应用,只是小规模测试使用的,不想让外部顾客访问,但是又要对外公布外部广域网链接,那么就得加把锁(密码)

 

(比如 staging环境 ,这个大家都知道 通过nginx 加上 basic http security)

 

但是如果测试 环境 (没有nginx 做load balance)该如何配置呢? 如果这个测试环境代码是使用hudson 集成的该怎么做呢?

 

就有一个顾客在这上面 下了一个订单,  即使你头部有明显的 测试环境的提示, 没用

 
  

配置方法: 


纯tomcat 如何配置 访问权限 ,大家可以参考 这个文章

http://wiki.metawerx.net/wiki/SecuringYourSiteWithContainerManagedSecurity

 

 

大家可以看到里面 的配置 是配置在 应用的 /WEB-INF/web.xml 里面的 , 如果使用hudson 会有缺陷 ,  SVN上面的这个文件也要加,不然一更新会覆盖掉这个文件

这样一来 开发环境也要配置这样的安全访问,很烦人

 

有没有 测试环境需要安全访问,但是本地开发环境不需要的呢?答案是肯定的

 

follow me:

 

1.找到 tomcat 下面 的 conf/tomcat-users.xml 文件

加入

 

  <rolerolename="feilong_role"/>
  <userusername="feilong_test"password="feilong_test1234"roles="feilong_role"/>

 

 

当然 你也可以在这个里面 加入 manager 控制台的 用户名 密码

 

完整的 代码

 

 

 



 

 

2.找到 tomcat 下面 的 conf/context.xml 文件

加入

 

 

<RealmclassName="org.apache.catalina.realm.MemoryRealm"pathname="conf/tomcat-users.xml"/>

 

 

 

这里我们使用 内存级的  , 还可以有很多其他选择 比如  UserDatabaseRealm

 

3.找到 tomcat 下面 的 conf/web.xml 文件 ,

在文件的 末尾  加上

 

 

<!-- Define the roles we want to use in the application -->
<security-role>
    <role-name>feilong_role</role-name>
</security-role>
 
<security-constraint>
    <display-name>Security constraint for the  folder</display-name>
 
    <!-- Define the resource, a /admin folder -->
    <web-resource-collection>
        <web-resource-name>feilong-frontend</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
 
    <!-- Only administrators can access this resource -->
    <auth-constraint>
        <role-name>feilong_role</role-name>
    </auth-constraint>
</security-constraint>
 
<!-- Use BASIC security -->
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Secure Area</realm-name>
</login-config>

 

 

至此 ,收工, 第一次访问 这个tomcat 下面 任意 一个应用 都需要输入密码

(一般来说,我们一个tomcat 只配置一个应用, 如果配置多个,而且只是部分应用需要密码,可以参考自行修改)

 



 附录: tomcat其实很强大, 有很多插件 ,很多好玩的东西,只是平时被大家忽视了

  • 大小: 6.7 KB
  • 大小: 11.1 KB
  • 大小: 9.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics