Spring-AOP之工作实践(二)
案例二、前端页面权限控制
对controllor控制器中的某写方法进行增强,如实现页面的按钮权限控制。
/**
* 保存session的容器
*/
public class SessionContext {
private static Map<String, HttpSession> sessionMap; // 单例
private SessionContext() { sessionMap = new ConcurrentHashMap<>(); } private enum SessionContextSingle {
INSTANCE;
private SessionContext sessionContext;
SessionContextSingle() {
sessionContext = new SessionContext();
}
public SessionContext getInstance() { return sessionContext; }
} public static SessionContext getInstance() {
return SessionContextSingle.INSTANCE.getInstance();
} // 添加session
public synchronized void addSession(HttpSession httpSession) {
if (httpSession != null) {
sessionMap.put(httpSession.getId(), httpSession);
}
} // 删除session
public synchronized void deleteSession(HttpSession httpSession) {
if (httpSession != null) {
sessionMap.remove(httpSession.getId());
}
} // 根据sessionId获取session
public HttpSession getSession(String sessionId) {
if (StringUtils.isBlank(sessionId)) {
return null;
}
return sessionMap.get(sessionId);
}
}
/**
* session监听器
*/
public class SessionListener implements HttpSessionListener {
private SessionContext sessionContext = SessionContext.getInstance(); // 在会话中第一次登录时,就调用该方法创建session
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
HttpSession httpSession = httpSessionEvent.getSession();
httpSession.setMaxInactiveInterval(10);
sessionContext.addSession(httpSession);
} @Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession httpSession = httpSessionEvent.getSession();
sessionContext.deleteSession(httpSession);
}
}
/**
* main方法处理切面
*/
@Component
@Aspect
@Order(-1)
public class MainAspect {
@Autowired
private UserService userService; // 切入点
@Pointcut("execution(* com.demo.*.controller.*Controller.*main(String, ..))")
private void pointCut() {} // 前置通知,在执行目标方法之前执行
@Before("pointCut()")
public void main(Joinpoint joinpoint) {
// 获取sessionid
String sessionId = (String) joinpoint.getArgs()[0];
// 获取当前上下文的session对象
HttpSession httpSession = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
// 根据sessionId获取session对象
User user = SessionContext.getInstance().getSession(sessionId).getAttribute("user");
// 对当前上下文的session赋值
httpSession.setAttribute("user", user);
// 权限传到前端
ModelAndView modelAndView = (ModelAndView) joinpoint.getArgs()[1];
Map<String, Object> model = Maps.newHashMap();
model.put("hasAdminRole", userService.hasRole(NeedRole.ADMIN));
modelAndView.addAllObjects(model);
}
}
/**
* 前端处理器
*/
@Controller
public class DemoController {
@PostMapping("/main")
public String main(String sessionId, ModelAndView modelAndView) {
Map<String, Object> model = Maps.newHashMap();
modelAndView.setViewName("demo/main");;
return modelAndView;
}
}
<!--页面:可以使用切面中保存到request域中的权限值来判断,进而实现页面按钮角色权限控制-->
<a th:if="${hasAdminRole}" href="javascript:void(0)" onclick="submit()">提交</a>
Spring-AOP之工作实践(二)的更多相关文章
- spring cloud微服务实践二
在上一篇,我们已经搭建了spring cloud微服务中的注册中心.但只有一个注册中心还远远不够. 接下来我们就来尝试提供服务. 注:这一个系列的开发环境版本为 java1.8, spring boo ...
- 5.2 Spring5源码--Spring AOP源码分析二
目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...
- 5.2 spring5源码--spring AOP源码分析二--切面的配置方式
目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring AOP日志实现(二)--获取访问者IP及访问路径
获取类及方法上的@RequestMapping注解: 应该是不等于: 获取访问者的ip地址,首先配置一个监听器: 配置完监听器后,就可以在类中注入一个HttpServletRequest: 获取ip:
- Spring aop 原始的工作原理的理解
理解完aop的名词解释,继续学习spring aop的工作原理. 首先明确aop到底是什么东西?又如何不违单一原则并实现交叉处理呢? 如果对它的认识只停留在面向切面编程,那就脏了.从oop(Objec ...
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- spring aop介绍和示例
参考:<Spring in Action> 一.AOP介绍 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程. 应用中有一些功能使用非常普遍,比如事 ...
- spring Aop设计原理
转载至:https://blog.csdn.net/luanlouis/article/details/51095702 0.前言 Spring 提供了AOP(Aspect Oriented Prog ...
随机推荐
- ArrayList详解-源码分析
ArrayList详解-源码分析 1. 概述 在平时的开发中,用到最多的集合应该就是ArrayList了,本篇文章将结合源代码来学习ArrayList. ArrayList是基于数组实现的集合列表 支 ...
- Spark_Streaming整合Kafka
Spark Streaming 整合 Kafka 一.版本说明二.项目依赖三.整合Kafka 3.1 ConsumerRecord 3.2 生产者属性 3 ...
- vue中mixins的使用方法和注意点(详2)(异步请求的情况)
当混合里面包含异步请求函数,而我们又需要在组件中使用异步请求函数的返回值时,我们会取不到此返回值,如下: mixin中 组件中 控制台 解决方案:不要返回结果而是直接返回异步函数 mixin中 组件中 ...
- TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
我的项目在mac上运行的很好,结果windows电脑,就一直报这个错误 解决方案: babel增加 @babel/plugin-transform-modules-commonjs 参考文章: htt ...
- nodejs+express搭建小程序后台服务器
本文使用node.js和express来为小程序搭建服务器.node.js简单说是运行在服务端的javascript:而express是node.js的一个Web应用框架,使用express可以非常简 ...
- Docker 入门:容器
容器看着像机器,实际是进程,是一个运行时程序. 要操作一个 Docker 容器,只需要执行 docker container 命令. 可以通过 help 查看 run 运行容器 基础使用: docke ...
- 【SocketIoClientDotNet】Nuget包安装问题
问题: Nuget安装[SocketIoClientDotNet]失败 错误信息: Operation failed Expected 1 export(s) with contract name & ...
- Python——关于定义过程
def sum(a,b): a = a + b return a print(sum(1,2)) s = 3 t = 5 print(sum(s,t)) 题目:你觉得前三行代码会输出什么? 1.输入两 ...
- python pexpect总结
基本使用流程 pexpect 的使用说来说去,就是围绕3个关键命令做操作: 首先用 spawn 来执行一个程序 然后用 expect 来等待指定的关键字,这个关键字是被执行的程序打印到标准输出上面的 ...
- jQuery-stop方法
1.stop()方法解析 停止所有在指定元素上正在运行的动画 stop(clearQueue,gotoEnd) 这个两个参数可选值是布尔值 stop(flase,flase):不请空动画队列,不立即跳 ...