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 ...
随机推荐
- 轻量级熔断降级框架 alibaba sentinel 应用
一.简介: wiki:https://github.com/alibaba/Sentinel/wiki 选择: ♥ 开源,成熟(功能完备.实际应用),活跃(功能维护及拓展) ♥ 更轻量:依赖资源少:a ...
- NetCore项目实战篇07---服务保护之polly
1. 为什么要用polly 前面的项目中,一个服务调用另一个(Zhengwei.Identity调用Zhengwei.Use.Api)服务时是直接调用的,在这个调用的过程中可能会发生各种瞬态故障,这 ...
- VMware如何克隆一个虚拟机
如何在Vmware克隆一个虚拟机,并修改哪些配置. 克隆虚拟机步骤 其中模板虚拟机的安装部署可参见:「VMware安装Linux CentOS 7.7系统」 找到克隆的模板机,并选择克隆. 进入克隆虚 ...
- POJ1661
题目链接:http://poj.org/problem?id=1661 解题思路: 离散化处理 + DP. 首先,纵坐标除了用来判断老鼠是否会摔死之外基本没用,主要考虑横坐标,只要求出在横坐标上必须走 ...
- HDU1160
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目大意:给出多个数据组(最多1000个),terminated by end of file, ...
- PHP关于syntax error语法错误的问题(Parse error: syntax error, unexpected end of file in xxxxxxxx)
在php程序出现类似 Parse error: syntax error, unexpected end of file in xxxxxxxx on line xx 的错误. 如图 如果发现php ...
- 微软Azure IoT驱动数字化变革线上分享会(6月4号)
微软Azure IoT驱动数字化变革线上分享会(6月4号) 微软作为全球范围内IoT领域的领军者,以微软智能云Azure为基础和核心,推动包括物联网.机器学习.微服务.人工智能等在内的新技术的发展 ...
- [批处理教程之MySQL]001.MySQL 常用命令大全
连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MySQL 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p ...
- List<T> 的扩展方法
//List<T>.Take(m) //取出 前m行 IEnumerable<Person> takeList = lstPerson.Take(4); foreac ...
- SpringBoot系列—简单的邮件系统
1. 效果发送效果图 2. 邮件开发准备工作 3. springboot引入mail服务 4. 启动应用,开始4种邮件发送测试 1. 效果发送效果图 连续发送了四封邮件:普通文本邮件,带附件的邮件,内 ...