您的位置:首页 > 游戏 > 游戏 > springSecurity学习之springSecurity注解使用

springSecurity学习之springSecurity注解使用

2024/10/6 6:51:18 来源:https://blog.csdn.net/Lxn2zh/article/details/140602823  浏览:    关键词:springSecurity学习之springSecurity注解使用

springSecurity注解使用

在使用springboot的时候,大家更习惯于使用注解来进行配置,那么springSecurity注解怎么使用呢

首先开启注解

@EnableGlobalMethodSecurity(// Spring Security 开启注解securedEnabled=true, // 开启@Secured注解,会创建切点,代理@Secured注解的方法prePostEnabled = true // 开启@PreAuthorize和@PostAuthorize注解
)

@Secured

用户具有哪些角色可以访问,注意要有ROLE_前缀

@Secured({"ROLE_student","ROLE_admin"})  // 用户具有哪些角色可以访问,注意要有ROLE_前缀
public String test(){System.out.println("secured");return "test";
}

@PreAuthorize

方法进入前进行校验

@PreAuthorize("hasAuthority('admin')")  // 方法进入前进行校验
public String test(){System.out.println("preAuthorize");return "test";
}

@PostAuthorize

方法结束后进行校验,可以正常执行,但是返回值需要进行权限校验

@PostAuthorize("hasAuthority('admin')")  // 方法结束后进行校验,可以正常执行,但是返回值需要进行权限校验public String test(){System.out.println("preAuthorize");return "test";
}

@PreFilter

允许方法调用,但是在进去方法前先过滤输入值

@PostFilter

允许方法调用,但是会过滤方法的结果

https://zhhll.icu/2021/框架/springSecurity/4.springSecurity注解使用/

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com