我在尝试在 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 注释在服务中工作正常,但在 Controller 中没有,所以我在这里读到 (http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in-web-context) 我认为是因为方法安全性仅在根应用程序上下文中配置,而不是在 servlet 上下文中配置. 但是,我找不到通过 Java 配置而不是使用 web.xml 文件来设置它的方法。 有什么想法吗?
更新:
正如评论中所指出的,方法应该是公开的才能被代理。
最佳答案
Controller 方法需要公开才能被 @Secured
代理。这样做应该可以解决它。
关于spring - Spring Boot 中的全局方法安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21674739/