参考:https://blog.csdn.net/u013310119/article/details/105270552/
https://www.cnblogs.com/huangzhe1515023110/p/9276055.html
1.Aop实现权限控制(切入点为被指定注解) 1.1 添加aop相关Maven依赖
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.6</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency>
1.2 修改Spring配置,添加下列代码。 <aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代 理。有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
<aop:aspectj-autoproxy proxy-target-class="false"/>
1.3修改SpringMVC配置 1.3.1在beans标签里面引入aop标签支持
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
1.3.2 添加配置。同spring配置一样(猜测controller和Service的aop要分开配置,未证实)
<aop:aspectj-autoproxy proxy-target-class="false"/>
1.4 新建注解接口(切入点)
package org.book.aop; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface PermissionCheck { }
说明: @Documented注解表明这个注解要被javadoc记录。注解默认状态下是不被javadoc记录的。 @Retention注解表明该注解保留到那个阶段,主要有三个值: SOURCE —— 这种注解保留在源代码级别,编译时就会被忽略 CLASS —— 这种注解编译时被保留,在class文件中存在,但JVM将会忽略 RUNTIME —— 这种注解将被JVM保留,利用反射机制可以获取并使用。 @Target注解表明该注解作用的范围。包括package、method、field、构造方法、成员变量、枚举值等属性。 @Inherited注解表明该注解是否影响子类。如果定义的注解上使用了@Inherited标记,则使用该注解的某个父类,它的子类默认继承所有的属性 1.5新建切面类 详情看注解
package org.book.aop; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.book.bean.Manager; import org.book.common.JsonResult; import org.book.service.IManagerService; import org.book.utils.SysContent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @Aspect//声明本类是切面 @Component//声明组件 @Slf4j public class PermissionCheckAspect { @Autowired IManagerService managerService; @Pointcut("@annotation(org.book.aop.PermissionCheck)")//拦截被PermissionCheck注解的方法 private void scall(){} /** * 环绕 */ @Around("scall()") public Object prepareFood(ProceedingJoinPoint joinPoint) throws Throwable{ log.info("进入切面方法"); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); //从获取RequestAttributes中获取HttpServletRequest的信息 HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); HttpSession session= request.getSession();//获取session String name = ""+session.getAttribute("user");//获取登录的信息 Manager manager =null; try { manager = managerService.getById(name); }catch (Exception e){ return JsonResult.renderFail("你没有权限"); } if(manager==null||manager.getUsername()==null||manager.getUsername().equals("")){ return JsonResult.renderFail("你没有权限"); } return joinPoint.proceed(); } }
1.6 在需要切入的方法前添加注解(即之前新建的注解 PermissionCheck),
1.7 前置通知、后置通知等 未完待续。。。。。
Comments | 129 条评论
博客作者 viagra pharmacy 100mg
Thanks in favor of sharing such a good idea, post is good, thats why i have read it entirely
博客作者 canadian pharmaceutical companies
Tremendous things here. I’m very glad to peer your post. Thanks so much and I am looking ahead to touch you. Will you please drop me a e-mail?
博客作者 list of canadian pharmaceuticals online
It is in point of fact a nice and helpful piece of info. I’m glad that you shared this helpful information with us. Please stay us informed like this. Thanks for sharing.
博客作者 canada pharmaceuticals
Hello, Neat post. There’s an issue together with your website in internet explorer, could test this? IE still is the market leader and a big part of people will miss your magnificent writing due to this problem.
博客作者 cheap prescription drugs
Someone necessarily lend a hand to make critically articles I would state. This is the very first time I frequented your web page and thus far? I amazed with the research you made to create this actual put up extraordinary. Fantastic task!
博客作者 canada pharmaceuticals online generic
Just want to say your article is as astonishing. The clarity in your post is just spectacular and i could assume you are an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the enjoyable work.
博客作者 canadian pharmaceuticals online safe
With havin so much content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any methods to help prevent content from being stolen? I’d truly appreciate it.
博客作者 canada pharmaceuticals
Howdy very cool site!! Man .. Excellent .. Superb .. I’ll bookmark your website and take the feeds also? I am glad to seek out a lot of useful information right here within the submit, we need work out more techniques on this regard, thanks for sharing. . . . . .
博客作者 pharmaceuticals online australia
I all the time emailed this webpage post page to all my friends, as if like to read it afterward my friends will too.
博客作者 canada pharmaceuticals online
I’m gone to say to my little brother, that he should also pay a quick visit this webpage on regular basis to take updated from most recent reports.
博客作者 canadian pharmaceuticals for usa sales
There is certainly a great deal to find out about this topic. I really like all the points you made.
博客作者 canada pharmaceuticals online
Having read this I thought it was extremely enlightening. I appreciate you taking the time and energy to put this content together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worth it!
博客作者 aarp recommended canadian pharmacies
Fantastic goods from you, man. I have take note your stuff previous to and you are simply extremely magnificent. I really like what you’ve got right here, certainly like what you are stating and the way by which you are saying it. You’re making it enjoyable and you continue to care for to keep it smart. I cant wait to learn much more from you. That is actually a wonderful website.
博客作者 canadian pharmaceuticals
Neat blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog stand out. Please let me know where you got your design. Bless you
博客作者 canadian pharmaceuticals
Quality articles or reviews is the secret to attract the visitors to pay a quick visit the site, that’s what this site is providing.