Spring Boot 中的全局方法安全性

2024-03-14

我在尝试在 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 http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in-web-context)我认为是因为方法安全性仅在根应用程序上下文中配置,而不是在 servlet 的上下文中配置。 但是,我找不到通过 Java 配置而不是使用 web.xml 文件来设置它的方法。 有任何想法吗?

Update:

正如评论中指出的,方法应该公开才能被代理。


控制器方法需要公开才能被代理@Secured。只要这样做就可以解决问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Boot 中的全局方法安全性 的相关文章

随机推荐