shiro使用及原理
  PzxJYpxHkmeC 2023年11月02日 53 0

(目录)

shiro是什么

shiro是一个权限认证框架,可以用来对用户的账号密码进行验证、cookie校验等。

shiro的框架

image

登录验证

  1. 创建subject对象并使用用户名密码生成token对象
  2. 调用subject的login方法进行登录
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName, userPassword);
subject.login(token);
  1. 自定义Realm对,重写
public class MyShiroRealm extends AuthorizingRealm {
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("doGetAuthenticationInfo()");
        // 获取用户输入的账户
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String user = token.getUsername();
        String password = new String(token.getPassword());

        try {
            UserDo userInfo = authService.login(shopNo, user, password);
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    userInfo, // 用户名
                    userInfo.getPasswd(), // 密码
                    ByteSource.Util.bytes(userInfo.getSalt() == null ? "123456" : userInfo.getSalt()), // salt=username+salt
                    getName() // realm name
            );
            return simpleAuthenticationInfo;
        } catch (BError error) {
            throw new AccountException(error.getMessage());
        }
    }
}

cookie校验

权限管理

shiro与spring的结合

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年04月26日   39   0   0 日志Java
PzxJYpxHkmeC
作者其他文章 更多