需要的依赖的坐标:

        <!-- 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. sql server 怎么在原有表上加自增长主键列,并指定主键名

    sql server 怎么在原有表上加自增长主键列,并指定主键名: ALTER TABLE [Merchant_black] ADD  Id bigint identity(1,1)   constr ...

  2. Zenlayer如何将万台设备监控从Zabbix迁移到Flashcat

    作为全球首家以超连接为核心的云服务商,Zenlayer 致力于将云计算.内容服务和边缘技术融合,为客户提供全面的解决方案.通过构建可靠的网络架构和高效的数据传输,Zenlayer 帮助客户实现更快速. ...

  3. vitepress 如何更换 favicon.ico

    favicon.ico 它出现在浏览器标签页上,是网站的标识之一. 准备图标 首先,你需要准备一个符合您要求的图标.通常,favicon.ico 使用的是 .ico 格式的图标文件,大小为 16x16 ...

  4. Spring扩展——BeanPostProcessor(BPP)

    BeanPostProcess简介 在Spring中 BeanPostProcessor 是一个非常重要的接口,它用于在每个bean对象初始化前后修改Bean的属性信息,比如我们最常用的@Autowi ...

  5. flutter+Springboot的结合

    我们团队的开发 前端采用flutter 后端采用spring boot 首先 完成了app的图标名字的修改 在app/src/main/res/mipmap 目录中 存放app图标 图片 在Andro ...

  6. es创建索引及别名更新mapping方法 elasticsearch [nested] nested object under path [XXX] is not of nested type

    [nested] nested object under path [XXX] is not of nested type这是因为在创建索引时没有指定类型为数组,这就是一个大坑,ES官方说可以不用指定 ...

  7. Nuxt 3 路由系统详解:配置与实践指南

    title: Nuxt 3 路由系统详解:配置与实践指南 date: 2024/6/21 updated: 2024/6/21 author: cmdragon excerpt: 摘要:本文是一份关于 ...

  8. AWX+gitlab

    目录 AWX+gitlab 1. Awx配置 1.1 添加机构 1.2 添加团队 1.3 添加主机 1.4 测试主机连通性 2. 对接gitlab 2.1 添加凭证 2.2 添加项目 2.3 上传pl ...

  9. Android/SELinux 添加 AVC 权限

    Android/SELinux 添加 AVC 权限 背景 在Android应用层中编写c/c++应用时,发现接口调用出现问题,logcat才知道是因为:权限不够. type=1400 audit(0. ...

  10. 【论文阅读】自动驾驶光流任务 DeFlow: Decoder of Scene Flow Network in Autonomous Driving

    再一次轮到讲自己的paper!耶,宣传一下自己的工作,顺便完成中文博客的解读 方便大家讨论. Title Picture Reference and pictures paper: https://a ...