2. SpringBoot 整合 Shiro

0.版本

  • springBoot : 2.4.4
  • shiro : 1.5.3



1. 关键

  • Shiro 提供一个 ShiroFilter 完成对所有的请求的拦截,进行认证。
  • 认证完毕,对于受限资源需要认证,公共资源则直接放行。

在这里插入图片描述




2. 整合


2.1 创建SpringBoot项目

        



2.2 引入shiro依赖
  • pom.xml
<dependency>
     <groupId>org.apache.shiro</groupId>
     <artifactId>shiro-spring-boot-starter</artifactId>
     <version>1.5.3</version>
 </dependency>


2.3 配置Shiro
  • com.xxx.xxx.config.ShiroConfig

         CustomerRealm 查看 2.4

@Configuration
public class ShiroConfig {
   

    // 1. 创建ShiroFilter,使用一个ShiroFilterFactorBean 进行生产即可
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
   
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        // 给 filter 设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 配置系统的受限资源,与公共资源
        Map<String, String> map = new HashMap<>();
        // authc : 请求i这个资源需要认证 和 授权, 从 servlet-contextPath 以后开
        // /** 所有的资源
        // 当发现页面需要认证的时候,会自动跳转到默认认证页面,默认为/login.jsp
        map.put("/index.jsp", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        // 手动设置默认认证页面路径
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");

        return shiroFilterFactoryBean;
    }

    // 2. 创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customerRealm") Realm realm) {
   
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }

    // 3. 创建自定义Realm
    @Bean("customerRealm")
    public Realm getRealm() {
   
        return new CustomerRealm();
    }
}


2.3.1 常见过滤器

        shiro提供了多个默认的过滤器,我们可以用这些过滤器来配置 url 的权限。
在这里插入图片描述

2.4 登录认证
2.4.1 流程解析
  • 根据 2.3 配置完毕 ShiroFilter等内容,并在其中设置 index.jsp 为受限资源,其他为非受限资源。
  • 用户未登录过,直接访问 index.jsp, ShiroFilter 拦截到了以后,直接跳转到设置好的登录页面 login.jsp;
  • 用户在 login.jsp 页面当中填写完毕登录信息,提交到地址 user/login,由 UserController 进行处理。
  • 如果使用 subject 登录成功,则重定向到 index.jsp 页面(此时响应头已经写入JSESSIONID)。此后进入认证受限的页面不会再跳转到login.jsp了。
  • 登录失败,则重定向到 login.jsp 继续登录.

在这里插入图片描述



2.4.1 index.jsp / login.jsp
  • src/webapp/index.jsp
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>主页</h1>
</body>
</html>
  • src/webapp/login.jsp
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>登录</h1>
    <form action="${pageContext.request.contextPath}/user/login" method="post">
        用户名: <input type="text" name="username"><br/>
        密码: <input type="password" name="password"><br/>
        <input type="submit" value="登录">
    </form>
</body>
</html>


2.4.2 编写 Realm 模拟认证
  • com.xxx.xxx.shiro.realms.CustomerRealm
public class CustomerRealm extends AuthorizingRealm {
   
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
   
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
   
        String principal = (String) authenticationToken.getPrincipal();
        if
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值