web登录密码

互联网时代,安全是永恒的主题,威胁无处不在,哪怕是在企业内网。

 

APDPlat充分考虑到了安全的问题:

 

首先,在浏览器中对用户密码加入复杂字符({用户信息})之后进行加密(Secure Hash Algorithm,SHA-512,as defined in FIPS 180-2),在服务器端加入用户名和复杂字符之后再次加密,提高破解复杂度;

 

其次,在浏览器和服务器之间采用安全通道(HTTPS)传输用户信息,避免信息泄露。

 

再次安全易用相互矛盾,不同的应用需要的平衡点不一样,APDPlat充分考虑到了这个问题,提供了可配置的用户密码安全策略,以满足不同的需求。

 

下面详细介绍相关的设计和实现:

 

首先,在浏览器对用户的登陆密码进行加密,使用的sha512.js来源于http://pajhome.org.uk/crypt/md5/

 

Html代码   收藏代码
  1. j_password=hex_sha512(j_password+'{用户信息}');  

 

在服务器端对新增用户修改密码重置密码时候的密码进行加密,使用类org.apdplat.module.security.service.PasswordEncoder的encode方法:

 

Java代码   收藏代码
  1. /** 
  2.  * 用户密码双重加密: 
  3.  * 1、使用SHA-512算法,salt为user.getMetaData(),即:用户信息 
  4.  * 2、使用SHA-256算法,salt为saltSource.getSalt(user),即:用户名+APDPlat应用级产品开发平台的作者是杨尚川,联系方式(邮件:<a href="mailto:ysc@apdplat.org">ysc@apdplat.org</a>)(QQ:281032878) 
  5.  * @author 杨尚川 
  6.  */  
  7. public class PasswordEncoder {  
  8.     public static String encode(String password,User user){  
  9.         password = new ShaPasswordEncoder(512).encodePassword(password,user.getMetaData());  
  10.         SaltSource saltSource = SpringContextUtils.getBean("saltSource");  
  11.         return new ShaPasswordEncoder(256).encodePassword(password,saltSource.getSalt(user));  
  12.     }  
  13.     public static void main(String[] args){  
  14.         User user = new User();  
  15.         user.setUsername("admin");  
  16.         user.setPassword("admin");  
  17.         String password = new ShaPasswordEncoder(512).encodePassword(user.getPassword(),user.getMetaData());  
  18.         System.out.println("Step 1 use SHA-512: "+password+" length:"+password.length());  
  19.         SaltSource saltSource = new APDPlatSaltSource();  
  20.         password = new ShaPasswordEncoder(256).encodePassword(password,saltSource.getSalt(user));  
  21.         System.out.println("Step 2 use SHA-256: "+password+" length:"+password.length());  
  22.     }  
  23. }  

 

Java代码   收藏代码
  1. /** 
  2.  * 用户salt服务,salt为: 
  3.  * 用户名+APDPlat应用级产品开发平台的作者是杨尚川,联系方式(邮件:ysc@apdplat.org)(QQ:281032878) 
  4.  * @author 杨尚川 
  5.  */  
  6. @Service("saltSource")  
  7. public class APDPlatSaltSource implements SaltSource{  
  8.     @Override  
  9.     public Object getSalt(UserDetails user) {  
  10.         //变化的用户名+固定的字符串  
  11.         String text = user.getUsername()+"APDPlat应用级产品开发平台的作者是杨尚川,联系方式(邮件:ysc@apdplat.org)(QQ:281032878)";  
  12.         return text;  
  13.     }  
  14. }  

 

 

用户在登陆的时候,浏览器进行一次加密,服务器进行二次加密,服务器加密的spring security配置如下:

 

Xml代码   收藏代码
  1. <s:authentication-manager alias="authenticationManager">  
  2.     <s:authentication-provider   user-service-ref="userDetailsServiceImpl" >  
  3.         <s:password-encoder  hash="sha-256">  
  4.             <s:salt-source ref="saltSource"/>  
  5.         </s:password-encoder>  
  6.     </s:authentication-provider>  
  7. </s:authentication-manager>  

  

 

其次,使用HTTPS安全通道,在配置文件config.local.properties中指定channel.type的值为https

 

Java代码   收藏代码
  1. #指定数据传输通道,可选值为http或https  
  2. channel.type=https  
  3. http.port=8080  
  4. https.port=8443  

 

再次,提供了可配置的用户密码安全策略,在配置文件config.local.properties中指定user.password.strategy的值,可在安全易用之间进行平衡,这里的值为相应策略类的spring bean name:

 

Java代码   收藏代码
  1. #用户密码安全策略  
  2. user.password.strategy=passwordLengthStrategy;passwordComplexityStrategy  

 

接下来看看跟用户密码安全策略相关的设计与实现:

 

用户密码安全策略接口

 

Java代码   收藏代码
  1. /** 
  2.  * 用户密码安全策略 
  3.  * @author 杨尚川 
  4.  */  
  5. public interface PasswordStrategy {  
  6.     /** 
  7.      * 检查用户的密码是否符合安全策略 
  8.      * @param password 用户密码 
  9.      * @throws PasswordInvalidException 不合法的原因包含在异常里面 
  10.      */  
  11.     public void check(String password) throws PasswordInvalidException;  
  12. }  

 

APDPlat默认提供了2个安全策略,分别是密码长度安全策略密码复杂性安全策略

 

1、密码长度安全策略:

 

Java代码   收藏代码
  1. /** 
  2.  * 密码长度安全策略 
  3.  * 密码长度必须大于等于6 
  4.  * @author 杨尚川 
  5.  */  
  6. @Service  
  7. public class PasswordLengthStrategy implements PasswordStrategy{  
  8.     private static final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(PasswordLengthStrategy.class);  
  9.   
  10.     @Override  
  11.     public void check(String password) throws PasswordInvalidException {  
  12.         if(StringUtils.isBlank(password) || password.length() < 6){  
  13.             String message = "密码长度必须大于等于6";  
  14.             LOG.error(message);  
  15.             throw new PasswordInvalidException(message);  
  16.         }  
  17.         LOG.info("密码符合安全策略");  
  18.     }  
  19. }  

 

2、密码复杂性安全策略

 

Java代码   收藏代码
  1. /** 
  2.  * 密码复杂性安全策略: 
  3.  * 1、密码不能为空 
  4.  * 2、密码不能全是数字 
  5.  * 3、密码不能全是字符 
  6.  * @author 杨尚川 
  7.  */  
  8. @Service  
  9. public class PasswordComplexityStrategy implements PasswordStrategy{  
  10.     private static final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(PasswordComplexityStrategy.class);  
  11.   
  12.     @Override  
  13.     public void check(String password) throws PasswordInvalidException {  
  14.         if(StringUtils.isBlank(password)){  
  15.             String message = "密码不能为空";  
  16.             LOG.error(message);  
  17.             throw new PasswordInvalidException(message);              
  18.         }  
  19.         if(StringUtils.isNumeric(password)){  
  20.             String message = "密码不能全是数字";  
  21.             LOG.error(message);  
  22.             throw new PasswordInvalidException(message);              
  23.         }  
  24.         if(StringUtils.isAlpha(password)){  
  25.             String message = "密码不能全是字符";  
  26.             LOG.error(message);  
  27.             throw new PasswordInvalidException(message);              
  28.         }  
  29.         LOG.info("密码符合安全策略");  
  30.     }  
  31. }  

 

有了不同的策略之后,还需要一个类来执行配置文件指定的策略

 

Java代码   收藏代码
  1. /** 
  2.  * 密码安全策略执行者 
  3.  * 根据配置项user.password.strategy 
  4.  * 指定的spring bean name 
  5.  * 分别执行指定的策略 
  6.  * @author 杨尚川 
  7.  */  
  8. @Service  
  9. public class PasswordStrategyExecuter implements PasswordStrategy, ApplicationListener {  
  10.     private static final APDPlatLogger LOG = APDPlatLoggerFactory.getAPDPlatLogger(ApplicationListener.class);  
  11.     private final List<PasswordStrategy> passwordStrategys = new LinkedList<>();  
  12.   
  13.     @Override  
  14.     public void check(String password) throws PasswordInvalidException {  
  15.         for(PasswordStrategy passwordStrategy : passwordStrategys){  
  16.             passwordStrategy.check(password);  
  17.         }  
  18.     }  
  19.       
  20.     @Override  
  21.     public void onApplicationEvent(ApplicationEvent event){  
  22.         if(event instanceof ContextRefreshedEvent){  
  23.             LOG.info("spring容器初始化完成,开始解析PasswordStrategy");  
  24.             String strategy = PropertyHolder.getProperty("user.password.strategy");  
  25.             if(StringUtils.isBlank(strategy)){  
  26.                 LOG.info("未配置user.password.strategy");  
  27.                 return;  
  28.             }  
  29.             LOG.info("user.password.strategy:"+strategy);  
  30.             String[] strategys = strategy.trim().split(";");  
  31.             for(String item : strategys){  
  32.                 PasswordStrategy passwordStrategy = SpringContextUtils.getBean(item.trim());  
  33.                 if(passwordStrategy != null){  
  34.                     passwordStrategys.add(passwordStrategy);  
  35.                     LOG.info("找到PasswordStrategy:"+passwordStrategy);  
  36.                 }else{  
  37.                     LOG.info("未找到PasswordStrategy:"+passwordStrategy);  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  

 

有了执行安全策略的服务之后,需要在新增用户修改密码重置密码的地方验证用户密码的安全用户服务类中进行验证APDPlat_Core/src/main/java/org/apdplat/module/security/service/UserService.java:

 

首先,注入密码安全策略执行者:

 

Java代码   收藏代码
  1. @Resource(name="passwordStrategyExecuter")  
  2. private PasswordStrategyExecuter passwordStrategyExecuter;  

 

其次,在新增用户的时候验证密码是否符合安全策略:

 

Java代码   收藏代码
  1. //先对用户的密码策略进行验证  
  2. try{  
  3.     passwordStrategyExecuter.check(model.getPassword());  
  4. }catch(PasswordInvalidException e){  
  5.     throw new RuntimeException(e.getMessage());  
  6. }  
  7. LOG.debug("加密用户密码");  
  8. model.setPassword(PasswordEncoder.encode(model.getPassword(), model));  

 

再次,在修改密码的时候验证密码是否符合安全策略:

 

Java代码   收藏代码
  1. for(Property property : properties){  
  2.     if("password".equals(property.getName().trim())){  
  3.         //先对用户的密码策略进行验证  
  4.         try{  
  5.             passwordStrategyExecuter.check(property.getValue().toString());  
  6.         }catch(PasswordInvalidException e){  
  7.             throw new RuntimeException(e.getMessage());  
  8.         }  
  9.         property.setValue(PasswordEncoder.encode(property.getValue().toString(),user));  
  10.         break;  
  11.     }  
  12. }  

 

Java代码   收藏代码
  1. //先对用户的密码策略进行验证  
  2. try{  
  3.     passwordStrategyExecuter.check(newPassword);  
  4. }catch(PasswordInvalidException e){  
  5.     result.put("success"false);  
  6.     result.put("message", e.getMessage());  
  7.     LOG.error(e.getMessage());  
  8.     return result;  
  9. }              
  10. oldPassword=PasswordEncoder.encode(oldPassword.trim(),user);  
  11. if(oldPassword.equals(user.getPassword())){  
  12.     user.setPassword(PasswordEncoder.encode(newPassword.trim(),user));  
  13.     serviceFacade.update(user);  
  14.     message = "修改成功";  
  15.     result.put("success"true);  
  16.     result.put("message", message);  
  17.     LOG.info(message);  
  18. }else{  
  19.     message = "修改失败,旧密码错误";  
  20.     result.put("success"false);  
  21.     result.put("message", message);  
  22.     LOG.error(message);  
  23. }  

 

最后,在重置密码的时候验证密码是否符合安全策略:

 

Java代码   收藏代码
  1. //先对用户的密码策略进行验证  
  2. try{  
  3.     passwordStrategyExecuter.check(password);  
  4. }catch(PasswordInvalidException e){      
  5.     LOG.error(e.getMessage());  
  6.     return e.getMessage();  
  7. }  
  8. int success = 0;  
  9. for(int id : ids){  
  10.     User user = serviceFacade.retrieve(User.class, id);  
  11.     if(user == null){  
  12.         LOG.error("ID为 "+id+" 的用户不存在,无法为其重置密码");  
  13.         continue;  
  14.     }  
  15.     if(PropertyHolder.getBooleanProperty("demo") && "admin".equals(user.getUsername())){  
  16.         LOG.error("演示版本不能重置admin用户的密码");  
  17.         continue;  
  18.     }  
  19.     //设置新密码  
  20.     user.setPassword(PasswordEncoder.encode(password, user));  
  21.     //同步到数据库  
  22.     serviceFacade.update(user);  
  23.     success++;  
  24. }   

 

后记(说明APDPlat中密码安全策略的重要性): 

 

假设用户密码明文为:123456,我们使用http://pajhome.org.uk/crypt/md5/的MD5计算功能进行密文的计算,使用http://www.cmd5.com/的解密功能进行明文的计算。

1、明文123456计算得到密文为:e10adc3949ba59abbe56e057f20f883e,然后对密文进行解密,立马得到明文:123456

2、我们加大密码长度,在密码后面填充3个0,把密码变为123456000,计算得到密文为3bc2fbdd89ef79f3dbfbaf1f2132baa1,然后对密文进行解密,解密页面提示:已查到,这是一条付费记录,密文类型:md5。说明还是能解密。

3、我们加大密码复杂度,在每两个密码之间填充更多的复杂字符,把密码变为{~$(!APDPlat)1(是)2(一个)3(应用级产品)4(开发平台)5(帮助您)6(快速开发企业级应用程序)$~},计算得到密文为:59f1cade9738167f3b4070e29da5af2e,然后对密文进行解密,解密页面提示:未查到,已加入本站后台解密。要想破解这么复杂的密码不是容易的事,何况APDPlat采用了比MD5更安全的SHA-512加密算法。

 

从上面的分析可以看到,保证密码安全并不容易,为了防止用户密码在网络传输中被监听泄露,我们使用HTTPS安全通道,为了避免用户的明文密码在网络上传输,先在客户端进行了加密,当然了,在客户端的加密规则很容易就暴露了,我们需要在服务器端再进行一次加密,也就是把客户端加密过后的密文当做明文,这样最终的用户密码密文就很难再破解出来了。

 

总结一下,用户密码主要面临两方面的威胁:一是在用户登陆的时候,需要将密码提交给服务器以验证身份,用户的密码有可能在网络传输过程中被监听导致泄露,用户在输入密码的时候也有可能被旁边的人看到,用户输入的密码也有可能被计算机中的病毒木马窃取;二是存储在服务器端数据库中的用户密码有可能被网站工作人员或黑客获取到,如果存储的用户密码未加密而是明文存储,那么就相当危险了,就算是加密了,如果采用的算法有缺陷(王小云为首的研发团队破译了MD5和SHA-1)且密码过于简单,也有可能被破解。

 

参考资料:

1、http://www.cmd5.com/

2、http://pajhome.org.uk/crypt/md5/

3、http://zh.wikipedia.org/wiki/%E7%8E%8B%E5%B0%8F%E9%9B%B2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值