当前位置: 首页 > 知识库问答 >
问题:

@安全注释在AspectJ模式下无法使用Autoproxy

傅正阳
2023-03-14

我试图让我的Spring MVC应用程序更好地使用Spring@Securited注释和AspectJ自动代理,但它似乎没有代理或识别我的@Securited注释。我有一个这样的控制器:

@Controller
@RequestMapping("/")
public class ApplicationController {

    private ApplicationFactory applicationFactory;

    @Inject
    public ApplicationController(ApplicationFactory applicationFactory) {
        super();
        this.applicationFactory = applicationFactory;
    }

    @Secured("ROLE_USER")
    @ResponseBody
    @RequestMapping(method = GET)
    public Application getApplicationInfo() {
        return applicationFactory.buildApplication(this);
    }

}

和一个看起来如下所示的spring security XML:

代码:

  <security:global-method-security secured-annotations="enabled" mode="aspectj" proxy-target-class="true" />

  <security:http auto-config="true" use-expressions="true">
    <security:http-basic/>
  </security:http>
@Configuration
@ComponentScan(basePackages = {"com.example"})
@EnableWebMvc
@ImportResource("classpath:security.xml")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

}
public class SpringMvcInitializer implements WebApplicationInitializer {

    private final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

    public void onStartup(ServletContext servletContext) throws ServletException {
        context.register(ApplicationConfiguration.class);

        servletContext.addListener(new ContextLoaderListener(context));
        servletContext.addListener(new Log4jConfigListener());

        final DelegatingFilterProxy proxy = new DelegatingFilterProxy("springSecurityFilterChain", context);
        FilterRegistration.Dynamic filter = servletContext.addFilter("securityFilter", proxy);
        filter.addMappingForUrlPatterns(EnumSet.of(REQUEST), false, "/*");

        final DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", servlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

}

我是不是漏掉了什么?我尝试将@EnableAspectJAutoProxy(proxyTargetClass=true)添加到应用程序配置中,但这也没有帮助。是要运行时编织还是必须使用编译时编织来为应用程序启用基于注释的安全性?

共有1个答案

施永贞
2023-03-14

在Spring中使用AOP时,您可以在AOP的两种实现中进行选择:

>

  • Spring AOP实现不需要编织,但它只适用于Spring管理的bean并且有一些限制

    AspectJ AOP实现可以对所有对象工作,没有Spring AOP的限制,但需要编译时或加载时编织

  •  类似资料:
    • 考虑以下基本的Foo、Bar和Main类: 运行Main打印“20个随机AspectJ字节”。我想取代执行吧。从Foo调用时的GenerateRadom。随机你好。这可以通过以下方面实现: 这可以工作,Main现在打印“7个随机AspectJ字节”。 如何使用注释表达相同的方面?这是我的尝试: 这不起作用,我收到以下编译器警告: 我已经验证了p\u randomHello和p\u Generato

    • 问题内容: 人们经常会问诸如此类的AspectJ问题,因此我想在一个我以后可以轻松链接的地方回答它。 我有这个标记注释: 现在,我注释这样的接口和/或方法: 这是一个小的驱动程序应用程序,它也实现了该接口: 现在,当我定义此方面时,我希望它会被触发 为每个构造函数执行带注释的类,并 每次执行带注释的方法。 不幸的是,方面没有打印任何内容,就像类Application和方法two()没有任何@Mar

    • 问题内容: 我有使用java config方法配置的具有Spring安全性的Spring Web应用程序。我想从身份验证中排除某些URL模式(例如:静态资源等)。我之前用spring security xml config完成了此操作,但是用java config无法弄清,因为添加antmatchers并没有帮助。 以下是我在扩展WebSecurityConfigurerAdapter的安全性配置

    • 我使用java配置方法配置了Spring Security性spring web应用程序。我想从身份验证中排除一些URL模式(例如:静态资源等)。我以前用spring security xml config做过这件事,但用java config做过这件事,因为添加antmatchers没有帮助。 下面是我在扩展WebSecurityConfigurerAdapter的安全配置类中添加的代码 我使用

    • 问题内容: 我正在尝试让我的Spring MVC应用程序与Spring @Secured批注和AspectJ自动代理一起很好地使用,但是它似乎并没有代理或识别我的@Secured批注。我有一个像这样的控制器: 春季安全性XML如下所示: 码: 上面的内容是由无XML的Spring @Configuration组件加载的,如下所示: 依次使用Servlet 3.0 WebApplicationIni

    • 在执行通知时,如何获取的和?我正在使用Spring 4.0.6、AspectJ 1.7.4