09_15_第七阶段:微服务开发||01-SpringBoot||14Shiro【观看狂神随笔】

Shiro

Shiro的三大部件

  • subject 用户

  • securityManager 管理所有用户

  • Realm 连接数据

Shiro的快速搭建

  1. 导入依赖
  2. 配置文件
  3. HelloWorld
  • 注:官方用的是commons-logging,我们用log4j

通过Shiro官方提供的快速开始,我们能获得如下:(shiro-main\samples\quickstart

//通过Subject 获取当前的用户对象
Subject currentUser = SecurityUtils.getSubject();
//通过当前用户拿到session
Session session = currentUser.getSession();
//判断当前用户是否被认证
currentUser.isAuthenticated()
//获取当前用户的一个认证
currentUser.getPrincipal()
//获得用户是否拥有什么角色
currentUser.hasRole("schwartz")
//获得用户的一些权限
currentUser.isPermitted("lightsaber:wield")
//注销
currentUser.logout();
  • Spring Security中~都有!——只是名字变了

SpringBoot集成Shiro

  1. 导入Shiro整合依赖
  2. 编写Shiro的两个核心配置
Shiro内置的过滤器如下
/*
	anon:无需认证就可以访问
	authc:必须认证了才能访问
	user:必须拥有 记住我 才能访问(一般不用)
	perms:拥有对某个资源的权限才能访问
	role:拥有某个角色权限才能访问
*/
登录拦截功能
//登录拦截功能
Map<String, String> filterMap = new LinkedHashMap<>();
//上面代码可以写成通配符格式
filterMap.put("/user/*", "authc");
bean.setFilterChainDefinitionMap(filterMap);
//设置登录的请求:(如果没有权限,跳转到login页面)
bean.setLoginUrl("/toLogin");
Shiro实现用户认证登录
MyController.java

    @RequestMapping("/login")
    public String login(String username, String password, Model model){


        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();

        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            //执行登录操作(没有异常就ok了,)
            subject.login(token);
            return "index";
        } catch (UnknownAccountException e) {   //用户名不存在
            model.addAttribute("msg", "用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){   //密码错误
            model.addAttribute("msg", "密码错误");
            return "login";
        }
    }
UserRealm.java

//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws 		AuthenticationException {
	System.out.println("执行了=>认证doGetAuthenticationInfo");

	//用户名,密码从数据库中取(这里伪造一下)
    String username = "root";
    String password = "123456";

    //这里和MyController用的一个token
    UsernamePasswordToken userToken = (UsernamePasswordToken) token;

    if(!userToken.getUsername().equals(username)){
    return null;    //抛出异常UnknownAccountException
    }

    //密码认证,shiro做
    return new SimpleAuthenticationInfo("",password, "");
}
login.html

<p th:text="${msg}" style="color: red;"></p>
<form th:action="@{/login}">
Shiro整合Mybatis
  • 搭建实验数据库

    CREATE DATABASE `mybatis`;
    
    USE `mybatis`;
    
    DROP TABLE IF EXISTS `user`;
    
    CREATE TABLE `user` (
    `id` int(20) NOT NULL,
    `name` varchar(30) DEFAULT NULL,
    `pwd` varchar(30) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    insert  into `user`(`id`,`name`,`pwd`) values (1,'狂神','123456'),(2,'张三','abcdef'),(3,'李四','987654');
    
  • Shiro中的MD5加密和MD5加盐加密区别

    /**默认简单加密(SimpleCredentialsMatcher),可以自己来写加密(被setCredentialsMatcher方法覆盖)
    * 换成MD5加密:缺点是加密后的是固定的,不能变
    *  如:a3f0bec59cebeb60553ec80bbfd5dfdf
    * 换成MD5盐值加密:会在固定的加密代码后面加上姓名的英文
    *  如:a3f0bec59cebeb60553ec80bbfd5dfdfusername
    */
    
在Shiro中将简单加密(SimpleCredentialsMatcher)换成MD5加盐加密的流程
Shiro整合thymeleaf【可以实现授权那个页面就只显示那个页面】

导入整合包

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂野小白兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值