Spring Boot Shiro 密码加密(加盐)

此篇文章是在Shiro整合Spring Boot入门的基础上写的,如果你没有shiro基础看不懂的话,可以去看看。

1、给Realm添加CredentialsMatcher

@Bean
public UserRealm userRealm() {
   UserRealm userRealm = new UserRealm();

   // 设置加密算法
   HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher("md5");
   // 设置加密次数
   credentialsMatcher.setHashIterations(1);
   userRealm.setCredentialsMatcher(credentialsMatcher);

   return userRealm;
}

在身份认证期间,Shiro会尝试使用credentialsMatcher去比对用户名密码是否正确。

2、保存在数据库中的密码也需要是加密后的密码

public class UserRealm extends AuthorizingRealm {

    // 模拟数据库中的数据
    private Map<String, User> users = new HashMap<>();

    {
    	// Realm中方法的密码必须也是加密过得
        String password = new SimpleHash("md5", "123", "salt").toString();
        users.put("xiaowang", new User("xiaowang", password, "user:add,user:update"));
        users.put("xiaoming", new User("xiaoming", password, "user:update"));
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了认证=>doGetAuthenticationInfo");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        // 模拟从数据库中根据用户名查用户信息
        User user = users.get(token.getUsername());
        if (user == null) {
            throw new UnknownAccountException();
        }

        // 返回AuthenticationInfo中,还需要携带盐值
        return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes("salt"),  "");
    }
	。。。
}

这里说明一下,为什么AuthenticationInfo要携带盐值。其实Shiro的比对规则是这样的,对用户输入的密码进行加密,然后与数据库中的密文进行比对,判断其是否相等。

所以有加盐的话,Shiro就需要知道盐是什么,而这个盐就从AuthenticationInfo获取。

最后来一个credentialsMatcher的使用案例:

public static void main(String[] args) {
	// 密码匹配器
    HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher("md5");
    // 设置加密次数
    credentialsMatcher.setHashIterations(1);

    // 模拟用户登录输入的用户名密码
    UsernamePasswordToken token = new UsernamePasswordToken("xiaowang", "123");

    // 模拟从Realm中获取的认证信息
    String password = new SimpleHash("md5", "123").toString();
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("xiaowang", password, "");

    // 使用比较器比较
    System.out.println(credentialsMatcher.doCredentialsMatch(token, info));

    // 加盐
    password = new SimpleHash("md5", "123", "salt").toString();
    info = new SimpleAuthenticationInfo("xiaowang", password, ByteSource.Util.bytes("salt"), "");
    System.out.println(credentialsMatcher.doCredentialsMatch(token, info));
}

码云地址

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue是一种现代化的JavaScript框架,用于构建用户界面。Element UI是基于Vue的组件库,提供了丰富的界面组件,方便开发者进行界面设计和开发。Spring Boot是一个快速开发Java应用的框架,可以轻松地创建独立的、基于Spring的应用程序。Shiro是一个强大且易于使用的Java安全框架,可用于身份验证、授权、加密和会话管理等。 结合Vue、Element UI、Spring BootShiro,可以实现权限到按钮的控制。首先,在Vue和Element UI中,可以根据当前用户的权限动态显示或隐藏按钮。在Vue组件中,根据后端返回的权限信息,可以通过v-if或v-show等指令来控制按钮的显示或隐藏。例如,只有具有特定权限的用户才能看到或操作某个按钮。 其次,在后端使用Spring BootShiro,可以进行更细粒度的权限控制。通过在后端创建角色和权限的映射关系,并根据用户的角色信息进行权限验证,可以确保只有具有相应权限的用户才能执行特定的操作。在需要进行权限验证的后端接口上,可以使用Shiro提供的注解来进行权限控制。例如,在Controller方法上添加@RequiresRoles注解,只允许拥有指定角色的用户调用该接口。 综上所述,通过结合Vue、Element UI、Spring BootShiro,可以实现权限到按钮的控制。在前端Vue和Element UI中,根据用户的权限信息来显示或隐藏按钮;在后端Spring BootShiro中,根据角色和权限的映射关系进行权限验证,确保只有具有相应权限的用户才能执行特定的操作。这种权限控制的方式可以提高系统的安全性和用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值