需要的依赖的坐标:

        <!-- Shiro依赖 -->
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency> <dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.5.3</version>
</dependency> <!-- Thymeleaf模版引擎 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency> <dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency> <!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

【配置编写】

先编写自定义Realm:

package cn.dai.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:44
*/
public class UserRealm extends AuthorizingRealm { @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
} @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String principal = authenticationToken.getPrincipal().toString(); if ("xxx".equals(principal)) return new SimpleAuthenticationInfo(principal, "123456", this.getName()); return null;
} }

再编写配置类:

package cn.dai.config;

import cn.dai.shiro.UserRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:39
*/
@Configuration
public class ShiroConfiguration { @Bean("realm") /* 自定义Realm配置 */
public Realm getRealm() {
return new UserRealm();
} @Bean("defaultWebSecurityManager") /* 注入安全管理器 */
public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
} @Bean("shiroFilterFactoryBean") /* 注入Shiro过滤器工厂Bean */
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
return shiroFilterFactoryBean;
}
}

配置Controller:

package cn.dai.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:53
*/
@Controller
public class AccessController { @RequestMapping("logout")
public String logout() {
SecurityUtils.getSubject().logout();
return "redirect:/loginview"; // 账号退出,重定向到登录页
} @RequestMapping("loginview")
public String login() {
return "login"; // 跳转登陆页面
} @RequestMapping("login")
public String login(String username, String password, HttpSession session) {
try {
Subject subject = SecurityUtils.getSubject();
subject.login(new UsernamePasswordToken(username, password));
return "redirect:/index"; // login方法执行没出现异常,登陆正常
} catch (UnknownAccountException unknownAccountException) {
unknownAccountException.printStackTrace();
System.out.println("用户名错误");
} catch (IncorrectCredentialsException incorrectCredentialsException) {
incorrectCredentialsException.printStackTrace();
System.out.println("密码错误");
} catch (Exception exception) {
exception.printStackTrace();
} return "redirect:/loginview";
} }

然后再更改权限控制:

package cn.dai.config;

import cn.dai.shiro.UserRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.HashMap; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 22:39
*/
@Configuration
public class ShiroConfiguration { @Bean("realm") /* 自定义Realm配置 */
public Realm getRealm() {
return new UserRealm();
} @Bean("defaultWebSecurityManager") /* 注入安全管理器 */
public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) {
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
defaultWebSecurityManager.setRealm(realm);
return defaultWebSecurityManager;
} @Bean("shiroFilterFactoryBean") /* 注入Shiro过滤器工厂Bean */
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager); HashMap<String, String> map = new HashMap<>(); map.put("/login", "anon"); // 登录shiro控制程序,随意访问
map.put("/loginview", "anon"); // 登录页面随意访问
map.put("/logout", "anon"); //退出页面随意访问
map.put("/**", "authc"); // 其余资源都必须授权访问 shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
shiroFilterFactoryBean.setLoginUrl("/login");
return shiroFilterFactoryBean;
}
}

【Shiro】08 SpringBoot整合的更多相关文章

  1. 18.Shiro与Springboot整合下登陆验证UserService未注入的问题

    Shiro与Springboot整合下登陆验证UserService未注入的问题 前言: 刚开始整合的情况下,UserService一执行,就会报空指针异常. 看了网上各位大神的讲解,什么不能用ser ...

  2. 【SpringBoot】08.SpringBoot整合jsp

    SpringBoot整合jsp 1.修改pom文件加入两个坐标jstl标签库和jasper <project xmlns="http://maven.apache.org/POM/4. ...

  3. SpringBoot系列十二:SpringBoot整合 Shiro

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初 ...

  4. 补习系列(6)- springboot 整合 shiro 一指禅

    目标 了解ApacheShiro是什么,能做什么: 通过QuickStart 代码领会 Shiro的关键概念: 能基于SpringBoot 整合Shiro 实现URL安全访问: 掌握基于注解的方法,以 ...

  5. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例

    1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...

  6. SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建

    SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...

  7. 转:30分钟了解Springboot整合Shiro

    引自:30分钟了解Springboot整合Shiro 前言:06年7月的某日,不才创作了一篇题为<30分钟学会如何使用Shiro>的文章.不在意之间居然斩获了22万的阅读量,许多人因此加了 ...

  8. springboot整合Shiro功能案例

    Shiro 核心功能案例讲解 基于SpringBoot 有源码 从实战中学习Shiro的用法.本章使用SpringBoot快速搭建项目.整合SiteMesh框架布局页面.整合Shiro框架实现用身份认 ...

  9. springboot系列(十)springboot整合shiro实现登录认证

    关于shiro的概念和知识本篇不做详细介绍,但是shiro的概念还是需要做做功课的要不无法理解它的运作原理就无法理解使用shiro: 本篇主要讲解如何使用shiro实现登录认证,下篇讲解使用shiro ...

  10. SpringBoot整合Shiro实现权限控制,验证码

    本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...

随机推荐

  1. INFINI Labs 产品更新 | Easysearch 新增跨集群复制 (CCR)、支持快照生命周期管理 (SLM) 功能等

    INFINI Labs 产品重量级更新!!!本次更新了很多亮点功能,如 Easysearch 新增跨集群复制 (CCR).支持快照生命周期管理 (SLM) 功能等:支持多集群.跨版本的搜索基础设施统一 ...

  2. Flashcat与出行科技企业一起实践多云可观测

    当前架构 某出行科技企业从单个公有云往多云转型,依托于国内领先的公有云提供商,采用多云架构,在可用性.弹性.成本.供应商依赖方面,拥有了显著的优势. 相应的,多云架构也给技术团队带来了一定的复杂度和技 ...

  3. Vue学习:22.Vue组件库-Vant

    Vue组件库是一系列预先构建好的.可复用的UI组件集合,它们设计用于加速Vue.js应用程序的开发过程.这些组件通常遵循一定的设计规范,提供统一的外观和交互体验,让开发者能够快速搭建用户界面. 组件库 ...

  4. HttpServletRequest获取header参数 sign

    HttpServletRequest获取header参数 sign //从请求头中获取参数 private static Map<String, String> getHeaders(Ht ...

  5. MoneyPrinterPlus:AI自动短视频生成工具,详细使用教程

    MoneyPrinterPlus是一款使用AI大模型技术,一键批量生成各类短视频,自动批量混剪短视频,自动把视频发布到抖音,快手,小红书,视频号上的轻松赚钱工具. 之前有出过一期基本的介绍,但是后台收 ...

  6. Windows10(or windows11) Hyper-V 创建虚拟交换机后宿主机上传速度变特别慢的问题解决

    问题 我在我的win11上启用了Hyper-v,装了个虚拟机跑了个CentOS7.6,为了让centos和宿主机通信在同个网段搞了个桥接网络,网络环境如下 然后我测试一个文件上传功能的时候发现网络上传 ...

  7. 记录vue和js操作——尽管很快实现了功能,可总感觉到不爽

    需求产生的原因是:后端有一些数据是从旧平台直接迁移过来的,新平台需要根据迁移过来的数据,自动生产新的数据格式. 操作符有如下几种,分项.支路和数字配合操作符可以自定义组合,例如 [0000000000 ...

  8. QuartzNet暂停恢复会执行多次的问题解决

    ' var config = new System.Collections.Specialized.NameValueCollection { { "quartz.jobStore.misf ...

  9. 使用kafka作为生产者生产数据到hdfs

    关键:查看kafka官网的userGuide 配置文件: agent.sources = r1agent.sinks = k1agent.channels = c1 ## sources config ...

  10. GUI测试稳定性的关键技术

    标签(空格分隔): GUI测试稳定性 GUI测试稳定性的关键技术 GUI 自动化测试稳定性,最典型的表现形式就是,同样的测试用例在同样的环境上,时而测试通过,时而测试失败. 这也是影响 GUI 测试健 ...