java - JSR 250 方法级别安全性在 Spring MVC 中不起作用

标签 java spring spring-mvc spring-security jsr250

我尝试从基于 @Configuration 的安全性转向 JSR 250 方法级安全性。下面的代码的工作原理如下:

对我的页面的访问是在SecurityConfiguration.class内的configure(HttpSecurity http)中配置的。每个人都可以访问“所有”页面,如果有人尝试“ protected ”,则会显示默认登录页面,如果角色错误,则会显示“访问被拒绝”消息。很好。

现在,我想做完全相同的事情,但使用 JSR 250 注释。所以:

我已删除 configure(HttpSecurity http) 方法,并将其添加到调度程序 servlet 上下文配置中

@EnableGlobalMethodSecurity(jsr250Enabled = true, proxyTargetClass = true, mode = AdviceMode.ASPECTJ, prePostEnabled=true)

显然 Controller 内部有@PermitAll@RolesAllowed

这些更改无法正常工作。如果我尝试访问任何页面,系统会询问我有关凭据的信息(默认登录页面),如果我填写了这些凭据,那么我就可以以任何角色访问任何页面:(

我是不是忘记了什么?

预先感谢您提供的任何帮助, 马立克

应用程序上下文:

@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();
}

Web应用程序上下文:

@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 :

@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>

最佳答案

我注意到您的@EnableGlobalMethodSecurity注释使用代理模式AdviceMode.ASPECTJ,但您的依赖项未列出AspectJ。

如果您尝试使用 AspectJ 代理,则需要提供依赖项并添加配置以使用 AspectJ 编译器进行编译。

如果您不打算使用 AspectJ 代理,请尝试不使用“mode = AdviceMode.ASPECTJ”参数。

编辑 - 这可能并不明显。要使用 AspectJ 代理,您需要:

  1. 指定依赖项
  2. 提供aspectj插件配置以使用AspectJ编译器进行编译

这是一个 Maven 配置示例:Running JDK8 for aspectj

这是 gradle 的一个:https://github.com/jigishpa/spring-samples/blob/master/aop/hello/build.gradle

关于java - JSR 250 方法级别安全性在 Spring MVC 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235428/

相关文章:

java - Java 格式验证数字?

java - 为什么Spring MVC会以404响应并报告“在DispatcherServlet中未找到带有URI […]的HTTP请求的映射”?

java - Spring MVC 图像不显示

java - 如何为 Spring Integration SFTP 入站适配器动态定义文件过滤器模式?

java - Spring mvc 向其他包隐藏一个包

java - Java : Path does not chain with any of the trust anchors 上的 SSL 异常

java - 黑莓持久对象

hibernate - 尝试设置 Tomcat 和 JDBC 连接池 : Communications Link Failure

java - 如何创建不同数字之间的映射?

java - 如何将 spring aop 与来自外部 jar 的类一起使用