毕设随笔

 1.静态调用session的拦截器

package com.stylefeng.guns.core.intercept;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.HttpSessionHolder;
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.springframework.stereotype.Component;

/**
 * 静态调用session的拦截器
 */
@Aspect
@Component
public class SessionHolderInterceptor extends BaseController {

    @Pointcut("execution(* com.stylefeng.guns.*..controller.*.*(..))")
    public void cutService() {
    }

    @Around("cutService()")
    public Object sessionKit(ProceedingJoinPoint point) throws Throwable {
        HttpSessionHolder.put(super.getHttpServletRequest().getSession());
        try {
            return point.proceed();
        } finally {
            HttpSessionHolder.remove();
        }
    }
}

1.Spring AOP面向切面编程,可以用来配置事务、做日志、权限验证、在用户请求时做一些处理等等。用@Aspect做一个切面,就可以直接实现。如图定义一个了SessionHolderInterceptor 切面类,加上@Component  @Aspect这两个注解   

2.定义切点---@Pointcut("execution(* com.stylefeng.guns.*..controller.*.*(..))")
    切点表达式中,..两个点表明多个,*代表一个,  上面表达式代表切入com.stylefeng.guns包下的带有controller类的所有方法,方法参数不限,返回类型不限。  其中访问修饰符可以不写,不能用*,,第一个*代表返回类型不限,第二个*表示所有类,第三个*表示所有方法,..两个点表示方法里的参数不限。  然后用@Pointcut切点注解,想在一个空方法上面,一会儿在Advice通知中,直接调用这个空方法就行了,也可以把切点表达式卸载Advice通知中的,单独定义出来主要是为了好管理。

3.Advice,通知增强,主要包括五个注解Before,After,AfterReturning,AfterThrowing,Around,下面代码中关键地方都有注释,我都列出来了。

   @Before  在切点方法之前执行

   @After  在切点方法之后执行

   @AfterReturning 切点方法返回后执行

   @AfterThrowing 切点方法抛异常执行

   @Around 属于环绕增强,能控制切点执行前,执行后,用这个注解后,程序抛异常,会影响@AfterThrowing这个注解。

环绕通知:
注意:Spring AOP的环绕通知会影响到AfterThrowing通知的运行,不要同时使用
环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。
环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型

 

4.model.addattribute()的作用

1.往前台传数据,可以传对象,可以传List,通过el表达式 ${}可以获取到,类似于request.setAttribute("sts",sts)效果一样。

2.Shiro

 

   /**
     * 获取封装的 ShiroUser
     *
     * @return ShiroUser
     */
    public static ShiroUser getUser() {
        if (isGuest()) {
            return null;
        } else {
            return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal();
        }
    }

1.subject总结:

subject见(SpringBoot+Shiro)

1) //获取当前用户对象Subject

Subject currentUser = SecurityUtils.getSubject();

2) //通过当前用户拿到Session

Session session = currentUser.getSession();

3) / /判断当前用户是否被认证
if(!currentUser.isAuthenticated()){ }

4)//获得当前用户是否被认证

currentUser.getPrincipal()

5) //判断当前用户是否拥有角色

currentUser.hasRole(" ")

6) //判断当前用户是否拥有权限

currentUser.isPermitted

7) //注销

currentUser.logout();

2.ShiroConfig


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值