spring security对登录密码加密

一.应用场景

需要对用户注册的密码进行加密保存,同时登陆验证时也要验证加密后的密码,这里主要记录了利用spring security模块进行这两方面的操作。

二.参考文献

1.Spring Security 3.1 中功能强大的加密工具 PasswordEncoderhttp://jsczxy2.iteye.com/blog/1423778

2.《Spring实战》第四版第262页

三.对登陆密码加密

1.spring+springmvc+mybatis

2.代码如下:这里的代码是摘录的部分,只涉及了利用security的PasswordEncoder对象的

StandardPasswordEncoder方法进行
SHA-256加密处理
@Override
	public int register(User user) {
		PasswordEncoder encoder=new StandardPasswordEncoder("hfahdf3");
		String rowPassword=user.getPassword();
		String password=encoder.encode(rowPassword);
		user.setPassword(password);
		userDao.creatUser(user);// 将注册用户信息插入数据库
		return 0;
	}

四.security配置基于数据库对加密后密码进行验证登录

关键是:passwordEncoder(new StandardPasswordEncoder("hfahdf3"));

注意上面的hfahdf3是秘钥,自己随便设置,但是要和注册时的相同

@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.jdbcAuthentication().dataSource(dataSource)
				.usersByUsernameQuery("select username,password,true from shop_user WHERE username=?")
				.authoritiesByUsernameQuery("select username,role from shop_user where username=?").passwordEncoder(new StandardPasswordEncoder("hfahdf3"));
	}

 

转载于:https://my.oschina.net/Cubicluo/blog/856199

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值