【发布时间】:2014-03-07 15:19:37
【问题描述】:
我在尝试在 Spring Boot 应用程序中启用全局方法安全性时遇到了一些问题。 或多或少我有这个配置:
@ComponentScan
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
public class Main extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Main.class);
app.setShowBanner(false);
ApplicationContext context = app.run(args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
}
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
...
}
@Override
protected void configure(HttpSecurity http) throws Exception {
...
}
}
@Controller
public class SampleController {
@RequestMapping("/api/hello")
@ResponseBody
String hello() {
return "Hello!";
}
@Secured(SecurityGrant.WRITE_PROJECT)
@RequestMapping("/api/bye")
@ResponseBody
String bye() {
return "Bye!";
}
}
@Secure 注释在服务中可以正常工作,但在控制器中不能正常工作,所以正如我在此处阅读的 (http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in-web-context) 我认为是因为方法安全性仅在根应用程序上下文中配置,而不是在 servlet 上下文中配置. 但是,我找不到通过 Java 配置而不是使用 web.xml 文件来设置它的方法。 有什么想法吗?
更新:
正如 cmets 中所指出的,方法应该是公开的才能被代理。
【问题讨论】:
-
控制器方法是否需要公开才能被
@Secured代理? -
就是这样!你拯救了我的一天。
标签: spring spring-mvc spring-security spring-boot