参考: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 | 111 条评论
博客作者 נערות ליווי
I was very pleased to uncover this great site. I need to to thank you for ones time for this fantastic read!! I definitely appreciated every bit of it and I have you bookmarked to look at new information on your blog.
博客作者 NEpOuTHmthR
@נערות ליווי USdLlZaMne
博客作者 NEpOuTHmthR
xQUgEdvRmop
博客作者 JsUIDRXur
@NEpOuTHmthR MZIFlPXaC
博客作者 JsUIDRXur
hqxKigupzjwHC
博客作者 Locowin
At this time I am ready to do my breakfast, once having my breakfast coming yet again to read more
news.
博客作者 VDCasino
Hello! I’ve been following your web site for some time now
and finally got the courage to go ahead and give you a shout out from Atascocita Tx!
Just wanted to say keep up the excellent job!
博客作者 Casibom
My spouse and I stumbled over here by a different
web page and thought I should check things out.
I like what I see so now i’m following you. Look forward to looking over your web page
for a second time.
博客作者 Surveillance Camerawoman TTD
hello there and thank you for your info – I have certainly picked up anything new from right
here. I did however expertise several technical points using this website, as I experienced to reload
the website a lot of times previous to I could get it to load correctly.
I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will very frequently affect your placement in google and could
damage your high-quality score if ads and marketing
with Adwords. Anyway I’m adding this RSS to my email and could look out for much more of your respective intriguing content.
Make sure you update this again soon.
博客作者 Buy Elf Camerawoman TTD
I do not even understand how I stopped up right here, but I thought this post
used to be great. I do not know who you’re however definitely you’re going to a well-known blogger
should you aren’t already. Cheers!
博客作者 Purchase Scary Speakerman TTD
Awesome things here. I am very glad to peer your post.
Thank you so much and I am having a look ahead to contact you.
Will you please drop me a e-mail?
博客作者 Pumpkin Farmer TTD Value
This info is invaluable. When can I find out more?
博客作者 Buy Healer TV Woman TTD
Hey there just wanted to give you a quick heads up. The
text in your article seem to be running off the screen in Firefox.
I’m not sure if this is a format issue or something to do
with web browser compatibility but I figured I’d post to let you know.
The layout look great though! Hope you get the issue resolved soon. Thanks
博客作者 roblox toilet day
Excellent post! We will be linking to this particularly great
article on our site. Keep up the good writing.
博客作者 PS99 Huge Anime Unicorn Value
I’m impressed, I must say. Rarely do I encounter a blog that’s both educative and interesting, and let me tell you, you have
hit the nail on the head. The issue is something too
few people are speaking intelligently about. I am very happy I found
this during my hunt for something regarding this.
博客作者 Buy Bunny MM2
hello!,I love your writing very much! percentage we keep in touch extra about your article on AOL?
I need an expert on this space to resolve my problem. May be that is you!
Having a look forward to see you.
博客作者 Buy Huge Abyssal Axolotl PS99
Hello! I just want to give you a big thumbs up for your great information you’ve
got here on this post. I am returning to your blog for more soon.