我尝试从@Configuration
基于安全性转移到 JSR 250 方法级安全性。下面的代码工作如下:
对我的页面的访问是在configure(HttpSecurity http)
里面配置的SecurityConfiguration.class
。每个人都可以访问“全部”页面,如果有人尝试“受保护”,则显示默认登录页面,如果角色错误,则显示“访问被拒绝”消息。美好的。
现在,我想做完全相同的事情,但使用 JSR 250 Annotations。所以:
我已删除configure(HttpSecurity http)
方法,添加到调度程序 servlet 上下文配置
@EnableGlobalMethodSecurity(jsr250Enabled = true, proxyTargetClass = true, mode = AdviceMode.ASPECTJ, prePostEnabled=true)
显然在控制器内部@PermitAll
。@RolesAllowed
这些更改无法正常工作。如果我尝试访问被询问有关凭据的任何页面(默认登录页面),如果我填写它们,那么我可以以任何角色访问任何页面:(
我是不是忘记了什么?
提前感谢您提供的任何帮助,Marek
应用环境:
@Import(SecurityConfiguration.class)
public class AppConfiguration {
// entityManagerFactory, transactionManager, localValidatorFactoryBean, methodValidationPostProcessor
}
@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("marek").password("123456").roles("USER");
auth.inMemoryAuthentication().withUser("bill").password("123456").roles("ADMIN");
auth.inMemoryAuthentication().withUser("james").password("123456").roles("SUPERADMIN");
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/all**").permitAll();
http.authorizeRequests().antMatchers("/protected/**").access("hasRole('ROLE_ADMIN')");
http.authorizeRequests().antMatchers("/confidential/**").access("hasRole('ROLE_SUPERADMIN')");
http.authorizeRequests().and().formLogin();
}
网络应用上下文:
@Configuration
@EnableWebMvc
@EnableGlobalMethodSecurity(jsr250Enabled = true, proxyTargetClass = true, mode = AdviceMode.ASPECTJ, prePostEnabled=true)
@ComponentScan(basePackages = "xxx.xxx.controllers")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
// addInterceptors, addViewControllers, templateResolver, templateEngine, thymeleafViewResolver
}
控制器:
@Controller
public class HomeController {
@PermitAll
@RequestMapping(value = "/all**", method = RequestMethod.GET)
public String allPage(Model model) {
return "all";
}
@RolesAllowed("ADMIN")
@RequestMapping(value = "/protected**", method = RequestMethod.GET)
public String protectedPage(Model model) {
return "protected";
}
@RolesAllowed("SUPERADMIN")
@RequestMapping(value = "/confidential**", method = RequestMethod.GET)
public String superAdminPage(Model model) {
return "confidential";
}
}
依赖项:
<appengine.target.version>1.9.18</appengine.target.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<javax.jsr250-api.version>1.0</javax.jsr250-api.version>
<spring.version>4.1.5.RELEASE</spring.version>
<spring.security.version>3.2.6.RELEASE</spring.security.version>
<spring.thymeleaf.version>2.1.4.RELEASE</spring.thymeleaf.version>
<aspectj.version>1.8.5</aspectj.version>