参考: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标签支持
![](https://dancun.top/wp-content/uploads/2021/07/clipboard.png)
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),
![](http://121.40.42.148:1111/wp-content/uploads/2021/07/clipboard-1.png)
1.7 前置通知、后置通知等 未完待续。。。。。
Comments | 111 条评论
I do not even understand how I ended up here, but I assumed this publish used
to be great. I do not recognize who you’re however certainly you
are going to a famous blogger should you aren’t already. Cheers!
Appreciating the hard work you put into your website and detailed information you present.
It’s great to come across a blog every once in a while that
isn’t the same outdated rehashed material.
Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google
account.
The other day, while I was at work, my cousin stole my iphone and tested to see if it
can survive a forty foot drop, just so she can be a
youtube sensation. My apple ipad is now broken and she has 83 views.
I know this is totally off topic but I had to share it with someone!
Nice response in return of this difficulty with solid arguments and
describing the whole thing about that.
I love your blog.. very nice colors & theme. Did you create
this website yourself or did you hire someone to do it for you?
Plz reply as I’m looking to create my own blog and would like to know
where u got this from. thanks a lot
This piece of writing will help the internet users for creating new website or even a blog from start to end.
Hey there I am so grateful I found your site, I really found you by error, while I
was looking on Askjeeve for something else,
Nonetheless I am here now and would just like to say thanks a lot
for a fantastic post and a all round interesting blog (I also love
the theme/design), I don’t have time to look over it all at the minute but I have
saved it and also added your RSS feeds, so when I
have time I will be back to read much more, Please do keep
up the excellent work.
I know this if off topic but I’m looking into
starting my own blog and was wondering what all is required to
get set up? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% positive. Any recommendations or
advice would be greatly appreciated. Kudos
Nice post. I was checking constantly this blog and I’m impressed!
Extremely useful information particularly the last part
:) I care for such info much. I was seeking this particular information for a very long time.
Thank you and good luck.
Hello would you mind sharing which blog platform you’re using?
I’m looking to start my own blog in the near future but
I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design seems different then most blogs
and I’m looking for something unique. P.S Sorry for getting off-topic but I had to ask!
This page definitely has all the info I needed about this subject and didn’t
know who to ask.
My spouse and I stumbled over here 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 at your web page repeatedly.
Link exchange is nothing else except it is only placing the other person’s web site link on your page at proper place and other person will also do similar for you.
I do not even know the way I stopped up right here, however I assumed this put
up was once great. I do not recognise who you might be but definitely you’re
going to a well-known blogger in case you are not already.
Cheers!
Thanks for ones marvelous posting! I truly enjoyed reading it,
you’re a great author.I will be sure to bookmark your blog and will come back someday.
I want to encourage you to definitely continue
your great job, have a nice afternoon!