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 ...
随机推荐
- 【雕爷学编程】MicroPython动手做(05)——零基础学MaixPy之LCD液晶屏
配套 2.4寸LCD屏 ST7789驱动器芯片(24P 320X240) ST7789驱动器芯片2.4寸LCD屏(24P 320X240)主要参数 1. 模块名称:液晶显示模块2. 型号:KD024C ...
- react+express实现跨域
1. 首先复习一下跨域的几种主要方式: a. jsonp b. cors c. 代理服务(开发环境下常用) 2. 代理服务器:可实现转发请求.即浏览器在3000端口发出请求,通过代理转发,将请求发送给 ...
- scrapy爬取效率提升配置
增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. 降低日志级别 ...
- LSM设计一个数据库引擎
Log-Structured Merge-Tree,简称 LSM. 以 Mysql.postgresql 为代表的传统 RDBMS 都是基于 b-tree 的 page-orented 存储引擎.现代 ...
- SDK内本地化处理 localizedStringForKey:value:table:
参考: 1,https://developer.apple.com/documentation/foundation/nsbundle/1417694-localizedstringforkey 2, ...
- UVALive8518 Sum of xor sum
题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回 ...
- wordpress各种获取路径和URl地址的函数总结
wordpress中的路径也不是很负责,有人为了让wordpress运行速度更快,就直接写了绝对地址,其实这样是很不好的,有可能别人修改了wordpress程序的地址,那么这样你编写的这个插件或者是主 ...
- css3,transition,animation两种动画实现区别
我们为页面设置动画时,往往会用到transition还有animation以及transfrom属性或者用到js. 其实通常情况下,对于使用js我们更加倾向于使用css来设置动画. transfrom ...
- centos 7 vscode cmake 编译c++工程
一.环境说明 1)gcc/g++ cmake安装建议 gcc/g++内核自带的即可,如果需要新的自行安装, cmake也一样,如有需要新的版本自行安装. 2)vscode安装插件 必要的插件c/c+ ...
- 基于Pytest豆瓣自动化测试【1】
-- Pytest基础使用教程[1] 引言 Pytest 是一个非常实用的自动化测试框架,目前来说资料也是非常多了.最近某友人在学习 Python的一些测试技术,帮其网上搜了下教程:发现大多数文章多是 ...