Fra*_*sso 10 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!";
}
}
Run Code Online (Sandbox Code Playgroud)
@Secure注释在服务上工作正常,但在控制器中没有,所以我在这里阅读(http://docs.spring.io/spring-security/site/faq/faq.html#faq-method-security-in -web-context)我认为是因为方法安全性只在根应用程序上下文中配置而不是在servlet中配置.但是,我找不到通过Java配置设置此方法的方法,而不是使用web.xml文件.有任何想法吗?
更新:
正如评论中所指出的,方法应该是公开的代理方式.
在 XML 中,您必须global-method-security在 servlet-context.xml 文件中定义第二个。这是因为有两个上下文,即根上下文和 Web 上下文,并且需要分别配置安全性。
在Java配置中,尝试创建一个单独的Web配置类,并将其标记为@EnableWebMvc:
@Configuration
@EnableWebMvc
@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true)
public class WebConfig {
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9146 次 |
| 最近记录: |