pdf secured_使您的Spring Security @Secured注释更干燥
pdf secured
最近,Grails用户邮件列表中的一个用户想知道在定义@Secured注释时如何减少重复 。 在Java批注中指定属性的规则非常严格,因此我看不到直接执行他所要求的方法的方法。
使用Groovy并没有真正的帮助,因为Groovy类中的注释大部分与Java中的注释几乎相同(数组值的语法除外)。 当然,Groovy现在支持注释中的闭包,但这需要在插件中进行代码更改。 但是后来我想到了Jeff Brown最近在缓存插件中所做的一些工作。
Spring的缓存抽象API包括三个注释。 @Cacheable
, @CacheEvict
和@CachePut
。 我们正在考虑支持比这些注释所允许的更多的配置选项,但是由于您无法对注释进行子类化,因此我们决定使用AST转换来查找这些注释的版本(当前具有与Spring注释相同的属性)并进行转换有效的Spring注释。 因此,我查看了Jeff的代码 ,它最终成为解决此问题的基础。
无法使用代码外部化权限列表,因为您无法控制编译顺序。 因此,我最终得到了一个不完美但可以使用的解决方案–我在项目根目录中寻找一个属性文件( roles.properties
)。 格式很简单–密钥是每个权限列表的名称,值是权限名称的列表,以逗号分隔。 这是一个例子:
admins=ROLE_ADMIN, ROLE_SUPERADMIN switchUser=ROLE_SWITCH_USER editors=ROLE_EDITOR, ROLE_ADMIN
这些键是用于新@Authorities
批注的值:
package grails.plugins.springsecurity.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.codehaus.groovy.transform.GroovyASTTransformationClass; /** * @author Burt Beckwith */ @Target ({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) @Inherited @Documented @GroovyASTTransformationClass ( "grails.plugins.springsecurity.annotation.AuthoritiesTransformation" ) public @interface Authorities { /** * The property file key; the property value will be a * comma-delimited list of role names. * @return the key */ String value(); }
例如,这是一个使用新注释的控制器:
@Authorities ( 'admins' ) class SecureController { @Authorities ( 'editors' ) def someAction() { ... } }
这等效于此控制器(如果使用@Authorities
反编译,则会看到两个注释):
@Secured ([ 'ROLE_ADMIN' , 'ROLE_SUPERADMIN' ]) class SecureController { @Secured ([ 'ROLE_EDITOR' , 'ROLE_ADMIN' ]) def someAction() { ... } }
AST转换类使用属性文件中指定的角色名称查找@Authorities
批注,加载属性文件并添加新的@Secured
批注(不会删除@Authorities
批注):
package grails.plugins.springsecurity.annotation; import grails.plugins.springsecurity.Secured; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.codehaus.groovy.ast.ASTNode; import org.codehaus.groovy.ast.AnnotatedNode; import org.codehaus.groovy.ast.AnnotationNode; import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.expr.ConstantExpression; import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.ast.expr.ListExpression; import org.codehaus.groovy.control.CompilePhase; import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.transform.ASTTransformation; import org.codehaus.groovy.transform.GroovyASTTransformation; import org.springframework.util.StringUtils; /** * @author Burt Beckwith */ @GroovyASTTransformation (phase=CompilePhase.CANONICALIZATION) public class AuthoritiesTransformation implements ASTTransformation { protected static final ClassNode SECURED = new ClassNode(Secured. class ); public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) { try { ASTNode firstNode = astNodes[ 0 ]; ASTNode secondNode = astNodes[ 1 ]; if (!(firstNode instanceof AnnotationNode) || !(secondNode instanceof AnnotatedNode)) { throw new RuntimeException( "Internal error: wrong types: " + firstNode.getClass().getName() + " / " + secondNode.getClass().getName()); } AnnotationNode rolesAnnotationNode = (AnnotationNode) firstNode; AnnotatedNode annotatedNode = (AnnotatedNode) secondNode; AnnotationNode secured = createAnnotation(rolesAnnotationNode); if (secured != null ) { annotatedNode.addAnnotation(secured); } } catch (Exception e) { // TODO e.printStackTrace(); } } protected AnnotationNode createAnnotation(AnnotationNode rolesNode) throws IOException { Expression value = rolesNode.getMembers().get( "value" ); if (!(value ConstantExpression)) { (!(value instanceof ConstantExpression)) { // TODO System.out.println( "annotation @Authorities value isn't a ConstantExpression: " + value); return null ; } String fieldName = value.getText(); String[] authorityNames = getAuthorityNames(fieldName); if (authorityNames == null ) { return null ; } return buildAnnotationNode(authorityNames); } protected AnnotationNode buildAnnotationNode(String[] names) { AnnotationNode securedAnnotationNode = new AnnotationNode(SECURED); List<Expression> nameExpressions = new ArrayList<Expression>(); for (String authorityName : names) { nameExpressions.add( new ConstantExpression(authorityName)); } securedAnnotationNode.addMember( "value" , new ListExpression(nameExpressions)); return securedAnnotationNode; } protected String[] getAuthorityNames(String fieldName) throws IOException { Properties properties = new Properties(); File propertyFile = new File( "roles.properties" ); if (!propertyFile.exists()) { // TODO System.out.println( "Property file roles.properties not found" ); return null ; } properties.load( new FileReader(propertyFile)); Object value = properties.getProperty(fieldName); if (value == null ) { // TODO System.out.println( "No value for property '" + fieldName + "No value for property '" + fieldName + "'" ); return null ; } List<String> names = new ArrayList<String>(); String[] nameArray = StringUtils.commaDelimitedListToStringArray( value.toString()) for (String auth : nameArray) { auth = auth.trim(); if (auth.length() > 0 ) { names.add(auth); } } return names.toArray( new String[names.size()]); } }
我可能会在某个时候将其包含在插件中-我已提醒您创建了JIRA问题 -但现在您可以将这两个类复制到应用程序的src / java文件夹中,并在项目根目录中创建一个roles.properties
文件。 每当您要添加或删除条目或从条目添加或删除角色名称时,请更新属性文件,运行grails clean
和grails compile
以确保使用最新值。
参考:在An Solipsists博客上,由我们的JCG合作伙伴 Burt Beckwith 使您的Spring Security @Secured注释更加干燥 。
翻译自: https://www.javacodegeeks.com/2012/06/make-your-spring-security-secured.html
pdf secured
pdf secured_使您的Spring Security @Secured注释更干燥相关推荐
- 使您的Spring Security @Secured注释更干燥
最近,Grails用户邮件列表中的一个用户想知道在定义@Secured批注时如何减少重复 . 在Java批注中指定属性的规则非常严格,因此我看不到直接执行他所要求的方法的方法. 使用Groovy并没有 ...
- Spring Security中文文档
Spring Security中文文档 来源:https://www.springcloud.cc/spring-security.html#overall-architecture 作者 Ben A ...
- Spring boot使用Spring Security和OAuth2保护REST接口
在本文中,我将会演示SpringSecurity+OAuth2如何去保护SpringBoot上的RESTAPI端点,以及客户端和用户凭证如何存储在关系数据库中.因此,我们需要做到以下几点: 配置Spr ...
- 使用Spring Security进行简单身份验证
朋友不允许朋友写用户身份验证. 厌倦了管理自己的用户? 立即尝试Okta的API和Java SDK. 在几分钟之内即可对任何应用程序中的用户进行身份验证,管理和保护. 身份验证对于除了最基本的Web应 ...
- Spring Security摘抄
最近一个项目的重构需要用到权限管理,想着用Spring Security较为方便.摘抄如下. 转自:http://www.cnblogs.com/zhangliang0115/archive/2012 ...
- Spring Security 和 Shiro 该如何选择?
来源 | https://blog.csdn.net/weixin_38405253/article/details/115301113 要知道Shiro和Spring Security该如何选择,首 ...
- 在 SpringBoot 项目中,Spring Security 和 Shiro 该如何选择?
点击上方 好好学java ,选择 星标 公众号重磅资讯,干货,第一时间送达 今日推荐:推荐19个github超牛逼项目!个人原创100W +访问量博客:点击前往,查看更多 要知道Shiro和Sprin ...
- Spring Security:基于MySQL数据库的身份认证
本文来说下Spring Security:基于MySQL数据库的身份认证和角色授权 .本文为了上手简单,只用了一张user表. 文章目录 概述 创建项目 基于数据库的身份认证 本文小结 概述 需求缘起 ...
- security框架工作笔记001--- Shiro和Spring Security对比_智慧城市项目中选型用Spring Security
JAVA技术交流QQ群:170933152 Shiro简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Secu ...
最新文章
- OpenCV-Python:实现人脸、人眼、嘴巴识别
- aesecbpkcs5 php_php AES/ECB/PKCS5Padding加密
- Unix/Linux常用命令及配置
- python range函数怎么表示无限_Python for循环与range函数的使用详解
- knn 机器学习_机器学习:通过预测意大利葡萄酒的品种来观察KNN的工作方式
- C语言定义外部变量或函数使得另一个C文件可以调用
- java 虚基类_重拾C++之虚函数和虚基类以及抽象类
- COCO数据集提取自己需要的类,转VOC
- Java的反射(二)
- 原生javascript淡入淡出焦点图 + Jquery实现方法
- vant 上传附件后回显_Vue + VantUI Uploader 上传组件, 实现上传功能, 但 手机实时上传照片只回显, 上传不上去 。...
- JAVA遇见HTML——JSP篇(JavaBeans)
- python 表白程序代码_python抖音表白程序源代码
- 计算机网络物理防护,计算机网络的物理安全
- 高质量解读《高性能mysql》——第1章 MySQL架构与历史
- FFMPEG音视频解码流程MP4音视频文件流读取(转)
- VS2013安装SVN插件
- 解决spring-data-jpa 级联添加时,主表放弃对外键维护时外键字段为null
- r语言nonzerocoef函数_文献汇报||Lasso方法在肿瘤基因位点筛选中的应用
- 两款开关电源的纹波对比
热门文章
- 02-MyBatis配置SQL打印
- 用指针完成函数参数的调用
- ssh(Spring+Spring mvc+hibernate)——EmpDaoImpl.java
- 马踏棋盘算法(骑士周游)
- Mysql字符串截取 mysql将字符串字段转为数字排序或比大小
- oracle 10741 trace,RedHat5.3上安装Oracle 10.2.0.1
- java面试常见面试问题_Java面试准备:15个Java面试问题
- java中两任务并行运行_Java并行编程中的“可调用”与“可运行”任务
- spring mvc 提示_Spring BootHibernate提示
- drools dmn_使用Drools的DMN运行时示例