在我们的项目中,我们混合了基于 xml 和注释的 spring mvc 安全配置。我们有很多角色,很多控制者,以及关于谁应该能够做什么的经常变化的要求。我认为拥有 util 类会很好地生成这样的权限矩阵。
到目前为止,我所做的是编写简单的类路径扫描器,它查找所有标有@Controller的类并提取@PreAuthorize的值。控制器的命名约定很简单:{Action}{Type}Controller(例如 NewOrderController)因此很容易生成人类(我指的是非程序员)可读的 csv 文件。
问题是对注释的类路径扫描并没有涵盖我们在 xml 配置中得到的内容。
我想知道是否有任何其他方法可以查询 spring mvc 本身用于访问解析的元数据。
编辑:
示例 xml 配置(大大简化):
<security:http pattern="/static/**" security="none" />
<security:http auto-config="false"
use-expressions="true"
entry-point-ref="entryPoint"
security-context-repository-ref="securityContextRepository">
<security:intercept-url pattern="/product/**" access="hasAnyRole('PRODUCT_USER')" />
<security:intercept-url pattern="/admin/**" access="hasAnyRole('ADMIN_USER')" />
<!-- No security configuration for /order/** -->
</security:http>
注释驱动配置示例:
@Controller
@RequestMapping("/order/new/{id}")
public class NewOrderController {
@Autowired
public NewOrderController() {
}
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("hasRole('ORDER_USER')")
public ModelAndView display() {
return new ModelAndView(/*...*/);
}
@RequestMapping(method = RequestMethod.POST)
@PreAuthorize("hasRole('ORDER_USER')")
public RedirectView process() {
return new RedirectView(/*...*/);
}
}