主要讲如何动态获取,因为之前是spring容器启动时获取数据库中的权限。
在配置过程中,出现了不生效的问题。
之前的代码:这种方式不生效
ApplicationContext ctx = new ClassPathXmlApplicationContext("application*.xml"); FactoryBean factoryBean = (FactoryBean) ctx.getBean("&filterInvocationSecurityMetadataSource"); DefaultFilterInvocationSecurityMetadataSource fids = (DefaultFilterInvocationSecurityMetadataSource) factoryBean.getObject(); FilterSecurityInterceptor filter = (FilterSecurityInterceptor) ctx.getBean("filterSecurityInterceptor"); filter.setSecurityMetadataSource(fids);
改为:
HttpServletRequest request = ServletActionContext.getRequest(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); FactoryBean factoryBean = (FactoryBean) ctx.getBean("&filterInvocationSecurityMetadataSource"); FilterInvocationSecurityMetadataSource fids = (FilterInvocationSecurityMetadataSource) factoryBean.getObject(); FilterSecurityInterceptor filter = (FilterSecurityInterceptor) ctx.getBean("filterSecurityInterceptor"); filter.setSecurityMetadataSource(fids);
改成下面的代码后就可以了,自己思考了一下,当时感觉spring是单例的,获得到的bean也是同一个bean即可。
之所以不生效,我感觉就是bean并不是重新再spring中获取,而是找到web中已经得到的,然后再改他的授权MAP.
自己的一些理解,不知道对不对。求拍砖
时间: 2024-10-09 13:59:19