spring - Spring Boot 中的全局方法安全性

标签 spring spring-mvc spring-security spring-boot

我在尝试在 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/

相关文章:

java - 如何使用 xml 在 spring security 中禁用注销确认?

java - 集群中的用户( session )数

Spring在单元测试中嵌入了ldap服务器

java - 定义整个maven项目的连接范围-spring mvc

java - 如何在 Spring MVC 中为 API 调用生成指标/统计信息?

spring-mvc - mockMvc - 测试错误消息

java - 使用 spring mvc 集成测试时 Spring security taglib 不可见

java - 使用 spring 和 bean 列表创建表单

java - 在 ItemReader/Processor/Writer 中访问 StepExecution/JobExecution 的最佳方式

java - 如果我们使用@embedded注解,HQL(hibernate查询语言)需要如何改变?