tomcat如何加密

1.tomcat安全配置之证书密码加密存储

http://www.2cto.com/Article/201308/239948.html


<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" 
           maxThreads="150" scheme="https" secure="true" 
           keystoreFile="./conf/keystore" keystorePass="jackie.123" 
           clientAuth="false" sslProtocol="TLS" />
           
<Connector port="8443" protocol="demo.MyHttp11Protocol" SSLEnabled="true" 
           maxThreads="150" scheme="https" secure="true" 
           keystoreFile="./conf/keystore" keystorePass="ADFADLJYNGHYVM==" 
           clientAuth="false" sslProtocol="TLS" />
           
           
package demo;  
   
import org.apache.coyote.http11.Http11Protocol;  
   
public class MyHttp11Protocol extends Http11Protocol {  
    @Override 
    public void init() throws Exception {  
        final String password = getKeypass();  
        final String realpassword = decipher(password);  
        setAttribute("keypass", realpassword);  
        super.init();  
    }  
   
    private String decipher(final String password) {  
        // 这里执行密码的解码操作;  
    }  
}                      

我打开Tomcat7.0.42的源代码,发现Http11Protocol里面只有这种方法,看来init是过时了?

不要着急,继续看继承树上面的类AbstractProtocol,找到了
   /*
     * NOTE: There is no maintenance of state or checking for valid transitions
     * within this class. It is expected that the connector will maintain state
     * and prevent invalid state transitions.
     */


    @Override
    public void init() throws Exception {
        if (getLog().isInfoEnabled())
            getLog().info(sm.getString("abstractProtocolHandler.init",
                    getName()));


        if (oname == null) {
            // Component not pre-registered so register it
            oname = createObjectName();
            if (oname != null) {
                Registry.getRegistry(null, null).registerComponent(this, oname,
                    null);
            }
        }


        if (this.domain != null) {
            try {
                tpOname = new ObjectName(domain + ":" +
                        "type=ThreadPool,name=" + getName());
                Registry.getRegistry(null, null).registerComponent(endpoint,
                        tpOname, null);
            } catch (Exception e) {
                getLog().error(sm.getString(
                        "abstractProtocolHandler.mbeanRegistrationFailed",
                        tpOname, getName()), e);
            }
            rgOname=new ObjectName(domain +
                    ":type=GlobalRequestProcessor,name=" + getName());
            Registry.getRegistry(null, null).registerComponent(
                    getHandler().getGlobal(), rgOname, null );
        }


        String endpointName = getName();
        endpoint.setName(endpointName.substring(1, endpointName.length()-1));


        try {
            endpoint.init();
        } catch (Exception ex) {
            getLog().error(sm.getString("abstractProtocolHandler.initError",
                    getName()), ex);
            throw ex;
        }
    }

================

2.如何实现admin用户密码非明文保存

可以把host-manager 这个应用deploy掉或者干脆删除了;

我们专注在manager这个app的安全设置上面就可以了

web.xml的设置

  <!-- Define the Login Configuration for this Application -->
  <login-config>
    <auth-method>DIGEST</auth-method>
    <realm-name>Tomcat Manager Application</realm-name>
  </login-config>


server.xml的设置

     <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase" digest="MD5" />
      </Realm>


cd %Catalina_home%

bin\digest.bat -a MD5 <user-password>

user-password:9afdaff99sdfasdgfd3467a54b1

就把这个得到的一长串16进制的数字替换tomcat-user.xml中

原先的明文就成功成为密文了。




### Tomcat 中实现加密及使用盐值的方法 #### 配置 Jasypt 加密器 为了在 Tomcat 启动时加载特定的加密配置,可以在 `catalina.sh` 文件中添加环境变量来设置加密所需的参数。具体操作是在 Tomcat 的 bin 目录下的 catalina.sh 文件里追加如下内容: ```bash export jasypt.encryptor.password="${salt}" ``` 这行命令定义了一个名为 `jasypt.encryptor.password` 的环境变量,并将其值设为 `${salt}`,这里的`${salt}`代表用于加密过程中的盐值[^1]。 #### 用户认证与密码保护 对于基于表单的身份验证(Form-authentication),可以通过修改 `tomcat-users.xml` 来增强安全性。当用户尝试登录时,提交的数据会被发送到服务器端进行校验。此时如果希望对用户的密码实施更安全的管理,则应该考虑采用带盐的哈希算法如 MD5 或 SHA-256 对明文密码做预处理后再保存至 XML 文件内。例如,在创建新账户或更改现有账户信息的时候,先利用带有固定字符串作为附加输入(即所谓的“盐”)的方式计算出散列值并记录下来;而在后续验证过程中同样应用相同的逻辑重现该散列以便比较两者是否一致[^2]。 #### 自定义 Realm 进行加盐摘要运算 Tomcat 提供了灵活的安全机制允许开发者自定义身份验证领域(Realm)。通过继承 `org.apache.catalina.realm.RealmBase` 类可构建自己的 realm 实现方案。在此基础上能够轻松集成额外的功能模块,比如支持多种类型的凭证编码方式或是引入复杂的密码策略等特性。特别是针对需要频繁变更敏感数据的应用场景而言,这种方法尤为适用因为它提供了接口可以直接参与到整个会话生命周期当中去执行必要的转换工作——像前面提到过的那样生成含有所谓‘盐’成分的消息摘要[^3]。 #### 增强型 MD5 散列函数 简单依赖于标准版 MD5 函数来进行密码保护存在潜在风险,因为这种做法很容易受到彩虹表攻击的影响从而被轻易破解。因此建议采取更加稳健的做法:不仅限于单一轮次的迭代而是多次重复调用 hash 方法直至达到预期强度级别为止;与此同时还应加入随机数序列充当辅助因子参与最终结果形成的过程之中以提高整体复杂度降低逆向工程的可能性。值得注意的是某些在线平台确实具备快速解析常规模式下产生的 MD5 输出的能力所以务必重视起这一点[^4]。 #### 数据库驱动认证服务扩展案例 假设正在开发一套 CAS (Central Authentication Service) 协议兼容的服务端程序并且打算连接外部关系型数据库完成用户注册/登陆流程的话,那么除了遵循官方文档指示外还需特别关注几个方面的问题。一方面是要确保所有涉及个人隐私的信息都经过妥善处置不会泄露出去;另一方面则是关于如何高效地检索匹配项进而返回给客户端正确的响应消息体。为此可能涉及到调整 `<Context>` 节点属性以及相应监听资源路径等内容[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值