数据库账号密码加密详解及实例
数据库账号密码加密详解及实例
数据库中经常有对数据库账号密码的加密,但是碰到一个问题,在使用UserService对密码进行加密的时候,spring security 也是需要进行同步配置的,因为spring security 中验证的加密方式是单独配置的。如下:
<authentication-manager> <authentication-provider user-service-ref="userDetailService"> <password-encoder ref="passwordEncoder" /> </authentication-provider> </authentication-manager> <beans:bean class="com.sapphire.security.MyPasswordEncoder" id="passwordEncoder"> <beans:constructor-arg value="md5"></beans:constructor-arg> </beans:bean>
如上述配置文件所示,passwordEncoder才是在spring security对账号加密校验的地方。
spring security在拦截之后,会首先对用户进行查找,通过自己定义的userDetailService来找到对应的用户,然后由框架进行密码的匹配验证。
从userDetailService得到user以后,就会进入到DaoAuthenticationProvider中,这是框架中定义的 ,然后跳入其中的authenticate方法中。
该方法会进行两个检查,分别是
* preAuthenticationChecks : 主要进行的是对用户是否过期等信息的校验,调用的方法在userDetail中有定义的。 * additionalAuthenticationChecks : 这个就是用户名密码验证的过程了。
而PasswordEncoder是我们xml中注入的bean,所以了,我们调用的则是我们自己完成的passwordEncoder
public class MyPasswordEncoder extends MessageDigestPasswordEncoder { public MyPasswordEncoder(String algorithm) { super(algorithm); } @Override public boolean isPasswordValid(String encPass, String rawPass, Object salt) { return encPass.equals(DigestUtils.md5DigestAsHex(rawPass.getBytes())); } }
这是我对其实现的一个简单版本,调用的就是spring自带的加密算法,很简单了,当然也可以使用复杂的加密方法,这个就靠自己了
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
Mysql报Table 'mysql.user' doesn't exist问题的解
这篇文章主要给大家介绍了关于Mysql报Table 'mysql.user' doesn't exist问题的解决方法,初学者可能会遇到这个问题,文中通过图文将解决方法介绍的非常详细,需要的朋友可以参考下2022-05-05Mysql数据库报错2003 Can't connect to MySQL server on
最近在用mysql,打开mysql的图形化界面要连接时出现2003错误,所以下面这篇文章主要给大家介绍了关于Mysql数据库报错2003 Can't connect to MySQL server on 'localhost' (10061)的解决方式,需要的朋友可以参考下2022-09-09
最新评论