springboot集成shiro和前后端分离配置

一,springboot集成shiro

1,导入依赖

        <dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring-boot-starter</artifactId><version>1.4.0</version></dependency>

2,Realm

shiro以来这个来进行认证和授权

package com.chen.admin.Realm;import com.alibaba.fastjson.JSON;
import com.chen.admin.dto.UserDto;
import com.chen.admin.entity.Role;
import com.chen.admin.entity.User;
import com.chen.admin.service.RoleService;
import com.chen.admin.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;import java.util.*;/*** @Author @Chenxc* @Date 24-7-2 10:13*/
@Component
public class UserPasswordRealm extends AuthorizingRealm {@Autowiredprivate RoleService roleService;@Autowiredprivate UserService userService;@Autowiredprivate PasswordEncoder encoder;// @Autowired//private StringRedisTemplate stringRedisTemplate;//授权@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {UserDto user = (UserDto) principalCollection.getPrimaryPrincipal();//使用redis存放用户角色和权限
//        Object o = stringRedisTemplate.opsForValue().get("user:" + user.getId());
//        if(null == o){
//            List<Role> roleList = roleService.getRoleByUserId(user.getId());
//            if(null != roleList){
//                Set<String> roles = new HashSet<>();
//                Set<String> permissions = new HashSet<>();
//                for (Role role : roleList) {
//                    roles.add(role.getName());
//                    List<String> permission = roleService.getPermissionByRoleId(role.getId());
//                    if(null != permission){
//                        permissions.addAll(permission);
//                    }
//                }
//                user.setRoleList(roles);
//                user.setPermissions(permissions);
//            }
//            stringRedisTemplate.opsForValue().set("user:"+user.getId(), JSON.toJSONString(user));
//        }else{
//            String json = (String)o;
//            user = JSON.parseObject(json,UserDto.class);
//        }
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        simpleAuthorizationInfo.addRoles(user.getRoleList());
//        simpleAuthorizationInfo.addStringPermissions(user.getPermissions());//本地存放用户角色和权限List<Role> roleList = roleService.getRoleByUserId(user.getId());if(null != roleList){Set<String> roles = new HashSet<>();Set<String> permissions = new HashSet<>();for (Role role : roleList) {roles.add(role.getName());List<String> permission = roleService.getPermissionByRoleId(role.getId());if (null != permission) {permissions.addAll(permission);}}user.setRoleList(roles);user.setPermissions(permissions);}SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();simpleAuthorizationInfo.addRoles(user.getRoleList());simpleAuthorizationInfo.addStringPermissions(user.getPermissions());return simpleAuthorizationInfo;}//认证@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;String username = token.getUsername();char[] password1 = token.getPassword();if(null == username || null  == password1){throw new AuthenticationException("用户名或密码错误");}String password = new String(password1);User user = userService.getUserByUsername(username);if(null == user){throw new AuthenticationException("用户名或密码错误");}boolean matches = encoder.matches(password, user.getPassword());if (!matches) {throw new AuthenticationException("用户名或密码错误");}if(user.getEnable().equals("0")){throw new DisabledAccountException("用户已禁用");}UserDto dto = new UserDto();BeanUtils.copyProperties(user,dto);SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(dto, password, getName());return authenticationInfo;}
}

3,shiro配置

package com.chen.admin.config;import com.chen.admin.dao.RedisSessionDao;
import com.chen.admin.filter.ShiroFormAuthenticationFilter;
import com.chen.admin.system.Constant;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;/*** @Author @Chenxc* @Date 24-7-2 10:10*/
@Configuration
public class ShiroConfig {@Beanpublic DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();// 设置为true则会在代理对象的方法执行过程中进行权限校验defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);return defaultAdvisorAutoProxyCreator;}@Beanpublic AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();advisor.setSecurityManager(securityManager);return advisor;}//上面的两个开启注解的支持,如果没有这两个方法,doGetAuthorizationInfo不会执行,加了@RequiresPermissions注解后会包找不到url//session保存在内存@Beanpublic DefaultWebSessionManager defaultWebSessionManager(){DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();sessionManager.setGlobalSessionTimeout(Constant.expireTime * 1000);sessionManager.setDeleteInvalidSessions(true);sessionManager.setSessionValidationSchedulerEnabled(true);//修改Cookie中的SessionId的key,默认为JSESSIONID,自定义名称sessionManager.setSessionIdCookie(new SimpleCookie(Constant.SHIRO_COOKIE_ID));return sessionManager;}//session保存在redis@Beanpublic DefaultWebSessionManager defaultWebSessionManager(RedisSessionDao redisSessionDao){DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();sessionManager.setGlobalSessionTimeout(Constant.expireTime * 1000);sessionManager.setDeleteInvalidSessions(true);sessionManager.setSessionDAO(redisSessionDao);sessionManager.setSessionValidationSchedulerEnabled(true);//修改Cookie中的SessionId的key,默认为JSESSIONID,自定义名称sessionManager.setSessionIdCookie(new SimpleCookie(Constant.SHIRO_COOKIE_ID));return sessionManager;}//上面的session存放选一个即可@Beanpublic DefaultWebSecurityManager defaultWebSecurityManager(DefaultWebSessionManager defaultWebSessionManager,Realm realm){DefaultWebSecurityManager manager = new DefaultWebSecurityManager();// 取消Cookie中的RememberMe参数manager.setRememberMeManager(null);manager.setRealm(realm);manager.setSessionManager(defaultWebSessionManager);return manager;}@Beanpublic ShiroFilterFactoryBean shiroFilterChainDefinition(DefaultWebSecurityManager defaultWebSecurityManager) {ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();factoryBean.setSecurityManager(defaultWebSecurityManager);factoryBean.setLoginUrl("/login");//factoryBean.setSuccessUrl("/loginSuccess");Map<String, String> map = new LinkedHashMap<>();//map.put("/login","anon");map.put("/static/**","anon");map.put("/ws/**","authc");map.put("/**", "authc");// 配置shiro默认登录界面地址,前后端分离中登录界面跳转应由前端路由控制,后台仅返回json数据
//        shiroFilterFactoryBean.setLoginUrl("/login/unauth");LinkedHashMap<String, Filter> filtsMap = new LinkedHashMap<>();// 这里使用自定义的filterfiltsMap.put("authc", new ShiroFormAuthenticationFilter());factoryBean.setFilters(filtsMap);factoryBean.setFilterChainDefinitionMap(map);return factoryBean;}}

如果上面使用redis还需要集成 AbstractSessionDAO 来读取redis中的数据来认证和授权

RedisSessionDao:

package com.chen.admin.dao;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chen.admin.system.Constant;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.TimeUnit;/**shiro集成redis* @author @Chenxc* @date 2024/7/3 0:46**/@Component
public class RedisSessionDao extends AbstractSessionDAO {@Autowiredprivate RedisTemplate redisTemplate;@Overrideprotected Serializable doCreate(Session session) {Serializable serializable = this.generateSessionId(session);this.assignSessionId(session,serializable);redisTemplate.opsForValue().set(session.getId(),session, Constant.expireTime, TimeUnit.SECONDS);return serializable;}@Overrideprotected Session doReadSession(Serializable serializable) {if(serializable == null){return null;}SimpleSession o = (SimpleSession)redisTemplate.opsForValue().get(serializable);if(o == null){return null;}return o;}@Overridepublic void update(Session session) throws UnknownSessionException {if(session != null && session.getId() != null){session.setTimeout( Constant.expireTime * 1000);redisTemplate.opsForValue().set(session.getId(),session, Constant.expireTime,TimeUnit.SECONDS);}}@Overridepublic void delete(Session session) {if (session != null && session.getId() != null) {redisTemplate.opsForValue().getOperations().delete(session.getId());}}@Overridepublic Collection<Session> getActiveSessions() {return redisTemplate.keys("*");}
}

二,前后端分离配置

如果是前后端分离中登录界面跳转应由前端路由控制,后台仅返回json数据,可以继承FormAuthenticationFilter重写里面的两个方法即可:

org.apache.shiro.web.filter.authc.FormAuthenticationFilter#onAccessDenied
org.apache.shiro.web.filter.authc.FormAuthenticationFilter#onLoginSuccess
package com.chen.admin.filter;import com.alibaba.fastjson.JSON;
import com.chen.admin.system.Result;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;/*** @author @Chenxc* @date 2024/7/8 1:29**/
public class ShiroFormAuthenticationFilter extends FormAuthenticationFilter {private static final Logger log = LoggerFactory.getLogger(ShiroFormAuthenticationFilter.class);/** 未登录时返回json* @auther: @Chenxc		//作者* @Description //TODO 	//描述* @param: 	//参数* @return: 	//返回值* @date:  	//创建日期*/@Overrideprotected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {if (this.isLoginRequest(request, response)) {if (this.isLoginSubmission(request, response)) {if (log.isTraceEnabled()) {log.trace("Login submission detected.  Attempting to execute login.");}return this.executeLogin(request, response);} else {if (log.isTraceEnabled()) {log.trace("Login page view.");}return true;}} else {if (log.isTraceEnabled()) {log.trace("Attempting to access a path which requires authentication.  Forwarding to the Authentication url [" + this.getLoginUrl() + "]");}//this.saveRequestAndRedirectToLogin(request, response);response.setContentType("application/json; charset=utf-8");response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();out.println(JSON.toJSONString(Result.unauthenticated()));out.flush();out.close();return false;}}/** 登录成功后返回json* @auther: @Chenxc		//作者* @Description //TODO 	//描述* @param: 	//参数* @return: 	//返回值* @date:  	//创建日期*/@Overrideprotected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {if (log.isTraceEnabled()) {log.trace("Attempting to access a path which requires authentication.  Forwarding to the Authentication url [" + this.getLoginUrl() + "]");}//this.saveRequestAndRedirectToLogin(request, response);response.setContentType("application/json; charset=utf-8");response.setCharacterEncoding("UTF-8");PrintWriter out = response.getWriter();Result result = Result.success("登录成功");HttpServletResponse httpServletResponse = (HttpServletResponse) response;String header = httpServletResponse.getHeader("Set-Cookie");if(null != header){String[] split = header.split(";");if(null != split && split.length != 0){String tokenStr = split[0].split("=")[1];result.setData(tokenStr);}}out.println(JSON.toJSONString(result));out.flush();out.close();return false;}
}

最后controller

package com.chen.admin.controller;import com.chen.admin.entity.Menu;
import com.chen.admin.service.MenuService;
import com.chen.admin.system.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author @Chenxc* @date 2024/7/15 23:46**/
@RestController
@RequestMapping("/menu")
@RequiresPermissions({"MENU"})//访问该controller需要的权限
public class MenuController {@Autowiredprivate MenuService menuService;@RequestMapping("/list")public Result<Menu> list(){return menuService.menuList();}@RequestMapping("/save")public Result<Menu> save(Menu menu){return menuService.saveMenu(menu);}@RequestMapping("/delete")public Result delete(Long id){return menuService.delete(id);}@RequestMapping("/update")public Result update(Menu menu){return menuService.updateMenu(menu);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/62003.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

目标检测标注图像

labelimg2 选择使用lableimg2的原因&#xff0c;rolabelimg无法导入labelimg2和labelimg标注数据。 而labelimg2则可以正常导入其它2种标注工具的标注过的数据。不适用labelimg的原因&#xff0c;他不支持画斜框。 下载地址&#xff1a;https://github.com/chinakook/labelIm…

Unity 中 多种资源加载方式的优缺点

在 Unity 中&#xff0c;有多种方式来加载资源&#xff0c;每种方式都有其独特的用途、优缺点和限制。以下是每种方式的详细比较&#xff1a; 1. AssetDatabase 用途: 主要用于编辑器环境中的资源加载。不适合在运行时使用。 特点 编辑器专用: 只能在编辑器模式下使用。同步…

鸿蒙多线程开发——Sendable对象的序列化与冻结操作

1、Sendable对象的序列化与反序列化 Sendable对象的简单介绍参考文章&#xff1a;鸿蒙多线程开发——线程间数据通信对象03(sendable) 与JSON对象的序列化和反序列化类似&#xff0c;Sendable对象的序列化和反序列化是通过ArkTs提供的ASON工具来完成。 与JSON类似&#xff0…

力扣第 66 题 “加一”

题目描述 给定一个由 非负整数组成的非空数组&#xff0c;表示一个整数。在该整数的基础上加一。 最高位数字在数组的首位&#xff0c;数组中每个元素只存储单个数字。 你可以假设除了整数 0 之外&#xff0c;这个整数不会以零开头。 示例 1: 输入: digits [1,2,3] 输出:…

C语言数据结构——详细讲解 双链表

从单链表到双链表&#xff1a;数据结构的演进与优化 前言一、单链表回顾二、单链表的局限性三、什么是双链表四、双链表的优势1.双向遍历2.不带头双链表的用途3.带头双链表的用途 五、双链表的操作双链表的插入操作&#xff08;一&#xff09;双链表的尾插操作&#xff08;二&a…

MYSQL 表的增删改查(上)

目录 1.新增数据 2.查询数据 一般查询 去重查询 排序查询 关于NULL 条件查询 分页查询 1.新增数据 语法&#xff1a;insert into 表名[(字段1&#xff0c;字段2...)] values (值&#xff0c;值....); 插入一条新数据行&#xff0c;前面指定的列&#xff0c;要与后面v…

重学SpringBoot3-异步编程完全指南

更多SpringBoot3内容请关注我的专栏&#xff1a;《SpringBoot3》 期待您的点赞&#x1f44d;收藏⭐评论✍ 重学SpringBoot3-异步编程完全指南 1. 简介2. Async注解2.1 基础配置2.2 基本使用2.3 自定义线程池 3. WebFlux响应式编程3.1 依赖配置3.2 响应式Controller示例3.3 响应…

【Linux】Linux 内存管理机制

前言 Linux 的内存管理机制是一个复杂而高效的系统&#xff0c;旨在确保系统资源的高效利用&#xff0c;同时提供良好的性能和响应能力。本文主要介绍 Linux 内存管理的主要组件和机制。 虚拟内存 概念: 每个进程在 Linux 中拥有自己的虚拟地址空间&#xff0c;这使得进程之…

Docker pull镜像拉取失败

因为一些原因&#xff0c;很多镜像仓库拉取镜像失败&#xff0c;所以需要更换不同的镜像&#xff0c;这是2024/11/25测试可用的仓库。 标题1、 更换镜像仓库的地址&#xff0c;编辑daemon.json文件 vi /etc/docker/daemon.json标题2、然后将下面的镜像源放进去或替换掉都可以…

对智能电视直播App的恶意监控

首先我们要指出中国广电总局推出的一个政策性文件是恶意监控的始作俑者&#xff0c;这个广电总局的政策性文件禁止智能电视和电视盒子安装直播软件。应该说这个政策性文件是为了保护特殊利益集团&#xff0c;阻挠技术进步和发展的。 有那么一些电视机和电视盒子的厂商和电信运…

网安基础知识|IDS入侵检测系统|IPS入侵防御系统|堡垒机|VPN|EDR|CC防御|云安全-VDC/VPC|安全服务

网安基础知识|IDS入侵检测系统|IPS入侵防御系统|堡垒机|VPN|EDR|CC防御|云安全-VDC/VPC|安全服务 IDS入侵检测系统 Intrusion Detection System 安全检测系统&#xff0c;通过监控网络流量、系统日志等信息&#xff0c;来检测系统中的安全漏洞、异常行为和入侵行为。 分为&am…

Golang面经

一、基础 1.make与new的区别 相同点:都是给变量分配内存 不同点: 作用变量类型不同,new给string,int,bool等分配内存,make给切片,map,channel分配内存;返回类型不一样,new返回指向变量的指针,make返回变量本身;new 分配的空间被初始化为零值。make 分配空间后会进…

文件导入-使用java反射修改日期数据

文件导入时&#xff0c;时间类型通常不能直接导出&#xff0c;以下方法为批量处理类中日期类型转字符串类型。 Date/Datetime --> String(yyyy-mm-dd)Field[] declaredFields HrAviationstudentMonitorDTO.class.getDeclaredFields(); for (Field field : declaredFields) …

C语言学习 12(指针学习1)

一.内存和地址 1.内存 在讲内存和地址之前&#xff0c;我们想有个⽣活中的案例&#xff1a; 假设有⼀栋宿舍楼&#xff0c;把你放在楼⾥&#xff0c;楼上有100个房间&#xff0c;但是房间没有编号&#xff0c;你的⼀个朋友来找你玩&#xff0c;如果想找到你&#xff0c;就得挨…

VITE+VUE3+TS环境搭建

前言&#xff08;与搭建项目无关&#xff09;&#xff1a; 可以安装一个node管理工具&#xff0c;比如nvm&#xff0c;这样可以顺畅的切换vue2和vue3项目&#xff0c;以免出现项目跑不起来的窘境。我使用的nvm&#xff0c;当前node 22.11.0 目录 搭建项目 添加状态管理库&…

Zookeeper选举算法与提案处理概览

共识算法(Consensus Algorithm) 共识算法即在分布式系统中节点达成共识的算法&#xff0c;提高系统在分布式环境下的容错性。 依据系统对故障组件的容错能力可分为&#xff1a; 崩溃容错协议(Crash Fault Tolerant, CFT) : 无恶意行为&#xff0c;如进程崩溃&#xff0c;只要…

ffmpeg视频滤镜:提取缩略图-framestep

滤镜描述 官网地址 > FFmpeg Filters Documentation 这个滤镜会间隔N帧抽取一帧图片&#xff0c;因此这个可以用于设置视频的缩略图。总体上这个滤镜比较简单。 滤镜使用 滤镜参数 framestep AVOptions:step <int> ..FV....... set frame st…

微服务篇-深入了解使用 RestTemplate 远程调用、Nacos 注册中心基本原理与使用、OpenFeign 的基本使用

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 认识微服务 1.1 单体架构 1.2 微服务 1.3 SpringCloud 框架 2.0 服务调用 2.1 RestTemplate 远程调用 3.0 服务注册和发现 3.1 注册中心原理 3.2 Nacos 注册中心 …

uniapp在H5使用vue-router路由返回上一页不会触发销毁函数解决方法

问题&#xff1a;uniapp在H5使用vue-router路由&#xff0c;如果在H5平台上进行页面刷新操作&#xff0c;再返回上一页&#xff0c;可能会遇到beforeDestroy、destroyed、onUnload生命周期钩子不被触发的问题。这是因为在H5中&#xff0c;页面的刷新实际上是整个应用的重新加载…

TCP/IP学习笔记

TCP\IP从实际应用的五层结构开始&#xff0c;自顶而下的去分析每一层。 TCP/IP五层架构概述 学术上面是TCP/IP四层架构&#xff0c;OSI/ISO是七层架构&#xff0c;实际中使用的是TCP/IP五层架构。 数据链路层 ICMP数据包分析 Wireshark抓包分析ICMP协议_wireshark抓ping包分析…