【安全认证框架】- shiro

shiro

shiro的架构

在这里插入图片描述

  • subject:当前的用户
  • SecurityManager:管理所有的用户
  • Realm:负责数据的交互

1.建项目,快速开始

在这里插入图片描述
即把https://github.com/apache/shiro下载下来参照shiro-master\samples\quickstart\来建项目(注意自己添加pom文件的版本)。
或根据
http://shiro.apache.org/10-minute-tutorial.html

建项目

正常运行的结果
在这里插入图片描述

对Quickstart.java类的分析
//获取当前的用户对象
Subject currentUser = SecurityUtils.getSubject();

// 通过当前用户拿到session
Session session = currentUser.getSession();
        
//currentUser.isAuthenticated()测试当前用户是否被认证
if (!currentUser.isAuthenticated()) {

//获取验证信息
currentUser.getPrincipal()

//判断角色是否存在
currentUser.hasRole("schwartz")

//权限判断
currentUser.isPermitted("lightsaber:wield")

//注销
currentUser.logout();

这些东西Spring Security都有

2.springboot集成shiro

在idea中新建一个springboot项目,只添加web就可以

然后在pom中导入thymeleaf的相关依赖

<!--        thymeleaf模板引擎-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

然后写index.html和controller
index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http//www.thymeleaf.org"
      xmlns:shiro="http//www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<p>首页</p>
<br>
<p th:text="${msg}"></p>
</body>
</html>

MyController.java

@Controller
public class MyController {

    @Resource
    private UserService userService;

    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        model.addAttribute("msg","hello shiro");
        return "index";
    }
}

启动项目访问http://localhost:8080/
在这里插入图片描述
说明springboot的web和thymeleaf配置成功

在springboot中整合shiro

1.添加shiro相关的依赖

<!--
Subject:用户
SecurityManager:管理所有用户
Realm:连接数据
 -->
<!-- shiro整合spring的包 -->
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.5.3</version>
</dependency>

2.添加shiro相关的配置文件
shiro需要三个相关的bean

  • 1.定义数据源Realm
  • 2.定义SecurityManager=> DefaultSecurityManager
  • 3.使用SecurityManager管理Subject
    其中第一步数据源需要自己定义

新建UserRealm.java

public class UserRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行力:授权=》doGetAuthorizationInfo");
        return null;
    }

//    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行力:认证=》doGetAuthorizationInfo");
        return null;
    }
}

然后正式编写ShiroConfig.java

@Configuration
public class ShiroConfig {

    //定义数据源Realm
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    //2.定义SecurityManager=>    DefaultSecurityManager
    //@Qualifier("userRealm")把userRealm的bean注入realm
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm realm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联Realm
        securityManager.setRealm(realm);
        return securityManager;
    }

    //3.使用SecurityManager管理Subject
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(securityManager);
        return bean;
    }

    //ShiroDialect:用于整合thymeleaf模板
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}

现在shiro就正式导入进Springboot中了。
为了验证shiro的功能在新建两个页面
add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add</title>
</head>
<body>
<h1>添加</h1>
</body>
</html>

update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update</title>
</head>
<body>
<H2>Update</H2>
</body>
</html>

在MyController中添加相关的路径


    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update(){
        return "user/update";
    }

然后在index.html页面中使用上面两个页面

<body>
<p>首页</p>

<p th:text="${msg}"></p>

<hr>
<div>
<a th:href="@{/user/add}">添加</a>
</div>
|
<div>
<a th:href="@{/user/update}">更新</a>
</div>
</body>

访问8080
在这里插入图片描述
所有我们需要的环境就搭建完成。下面正式来使用Shiro

3.shiro的使用

shiro的登录拦截

shiro的登录拦截是在ShiroConfig中实现的

这是对资源拦截的代码

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(securityManager);

        /**shiro的登录拦截*/

        //shiro的内置过滤器
        /*
            anon:无需认证就可以访问
            authc:必须认证才能访问
            user:必须拥有记住我功能才能访问
            perms:必须对某个资源有权限才能访问
            roles:必须拥有某个角色权限才能访问
         */

        //添加资源过滤器
        Map<String, String> filterMap = new LinkedHashMap<>();

        //除了写具体的资源路径外,shiro还支持通配符
        //授权
        filterMap.put("/user/add","authc");
        filterMap.put("/user/update","authc");
        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录页面
        bean.setLoginUrl("/login");

        return bean;
    }

此时直接访问8080然后点击-添加
在这里插入图片描述
然后一般如果没有权限说明用户没有登录,所以要跳转到登录界面中

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值