java-web - javaWeb中shiro的使用问题

查看:194
本文介绍了java-web - javaWeb中shiro的使用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在学习shiro框架,看了好多资料都没弄清楚shiro是如何验证用户名 与密码是否匹配的,好多代码注释中写道用户名与密码匹配由shiro自动完成,可我看了很久还是没能明白,求指点。

public class MyRealm extends AuthorizingRealm {
    private static final Logger logger = LoggerFactory.getLogger(MyRealm.class);
    @Autowired
    private IUserService userService;

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        logger.info("--- MyRealm doGetAuthorizationInfo ---");

        // 获得经过认证的主体信息
        User user = (User)principalCollection.getPrimaryPrincipal();
        Integer userId = user.getId();
        // UserService userService = (UserService)InitServlet.getBean("userService");
        List<Resource> resourceList = userService.listAllResource(userId);
        List<String> roleSnList = userService.listRoleSnByUser(userId);

        List<String> resStrList = new ArrayList<>();
        for(Resource resource:resourceList){
            resStrList.add(resource.getUrl());
        }

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(new HashSet<>(roleSnList));
        info.setStringPermissions(new HashSet<>(resStrList));

        // 以上完成了动态地对用户授权
        logger.debug("role => " + roleSnList);
        logger.debug("permission => " + resStrList);

        return info;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        logger.info("--- MyRealm doGetAuthenticationInfo ---");
        String username = authenticationToken.getPrincipal().toString();
        String password = new String((char[])authenticationToken.getCredentials());
        // 以后我们使用 Spring 管理 Shiro 的时候,就不必要这样得到 UserService 了
        // userService = (IUserService) InitServlet.getBean("userService");
        // User user = userService.login(username,password);
        // 这里应该使用 load 方法,比对用户名的密码的环节应该交给 Shiro 这个框架去完成

        // 在测试调试的时候发现,这里还是应该使用 login 判断,因为登录不成功的原因有很多,
        // 可以在登录的逻辑里面抛出各种异常
        // 再到 subject.login(token) 里面去捕获对应的异常
        // 显示不同的消息到页面上
        User user = userService.login(username,password);
        if(user!=null){
            // 第 1 个参数可以传一个实体对象,然后在认证的环节可以取出
            // 第 2 个参数应该传递在数据库中正确的数据,然后和 token 中的数据进行匹配
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getPassword(),getName());
            // 设置盐值
            info.setCredentialsSalt(ByteSource.Util.bytes(username.getBytes()));
            return info;
        }
        return null;
}

解决方案

是通过org.apache.shiro.authc.credential.CredentialsMatcher来做账号密码匹配的, 你可以实现自己的matcher, 如果你没有提供org.apache.shiro.realm.AuthenticatingRealm#setCredentialsMatcher(CredentialsMatcher), 默认使用的是org.apache.shiro.authc.credential.SimpleCredentialsMatcher, 就是你说的shiro自动帮你做的

这篇关于java-web - javaWeb中shiro的使用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆