我試圖讓Spring MVC應用與Spring @Secured註解和AspectJ自動代理一起玩,但它似乎並沒有代理或識別我的@Secured註釋。我有一個這樣的控制器:@Secured註解不能在AspectJ模式下使用Autoproxy
@Controller
@RequestMapping("/")
public class ApplicationController {
private ApplicationFactory applicationFactory;
@Inject
public ApplicationController(ApplicationFactory applicationFactory) {
super();
this.applicationFactory = applicationFactory;
}
@Secured("ROLE_USER")
@ResponseBody
@RequestMapping(method = GET)
public Application getApplicationInfo() {
return applicationFactory.buildApplication(this);
}
}
和彈簧安全XML,看起來是這樣的:
代碼:
<security:global-method-security secured-annotations="enabled" mode="aspectj" proxy-target-class="true" />
<security:http auto-config="true" use-expressions="true">
<security:http-basic/>
</security:http>
以上是由無XML彈簧加載@像這樣的配置組件:
@Configuration
@ComponentScan(basePackages = {"com.example"})
@EnableWebMvc
@ImportResource("classpath:security.xml")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
}
然後使用Servlet 3.0 WebApplicationI nitializer:
public class SpringMvcInitializer implements WebApplicationInitializer {
private final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
public void onStartup(ServletContext servletContext) throws ServletException {
context.register(ApplicationConfiguration.class);
servletContext.addListener(new ContextLoaderListener(context));
servletContext.addListener(new Log4jConfigListener());
final DelegatingFilterProxy proxy = new DelegatingFilterProxy("springSecurityFilterChain", context);
FilterRegistration.Dynamic filter = servletContext.addFilter("securityFilter", proxy);
filter.addMappingForUrlPatterns(EnumSet.of(REQUEST), false, "/*");
final DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", servlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");
}
}
然而,Spring Security中沒有檢測到的註釋,我仍然能夠上方端點安全不被認可。根據Spring Security FAQ,這可能是因爲<global-method-security>
元素正在加載錯誤的應用程序上下文中,但我不知道如何確保使用上述no-xml Spring配置。
我錯過了什麼嗎?我嘗試將@EnableAspectJAutoProxy(proxyTargetClass = true)添加到我的應用程序配置中,但這也沒有幫助。是否有運行時編織或我將不得不使用編譯時編織來爲我的應用程序啓用基於註釋的安全性?
你究竟想達到什麼目的?你爲什麼需要'mode =「aspectj」'? – axtavt 2012-07-09 18:13:50
我試圖使用@Secured註釋與自動代理,而無需加載/編譯時織入。 – 2012-07-09 18:30:25