当前位置: 首页 > 面试题库 >

@Secured批注在具有Autoproxy的AspectJ模式下不起作用

彭硕
2023-03-14
问题内容

我正在尝试让我的Spring MVC应用程序与Spring
@Secured批注和AspectJ自动代理一起很好地使用,但是它似乎并没有代理或识别我的@Secured批注。我有一个像这样的控制器:

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

}

春季安全性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>

上面的内容是由无XML的Spring @Configuration组件加载的,如下所示:

@Configuration
@ComponentScan(basePackages = {"com.example"})
@EnableWebMvc
@ImportResource("classpath:security.xml")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

}

依次使用Servlet 3.0 WebApplicationInitializer进行加载:

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("/*");
    }

}

但是,Spring Security不会检测到注释,并且我仍然能够在未经授权的情况下使用上面的安全端点。根据Spring Security
FAQ
,这可能是因为该<global-method- security>元素是在错误的应用程序上下文中加载的,但是我不知道如何使用上述no-xml Spring配置来确保这一点。

我想念什么吗?我尝试将@EnableAspectJAutoProxy(proxyTargetClass =
true)添加到我的应用程序配置中,但这也无济于事。无论如何,是否需要进行运行时编织,还是必须使用编译时编织为我的应用程序启用基于注释的安全性?


问题答案:

将AOP与Spring结合使用时,可以在两种AOP实现之间进行选择:

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

  • AspectJ AOP实现可以适用于所有对象,并且不受Spring AOP的限制,但是需要编译时或加载时编织

mode="aspectj" 告诉Spring使用AspectJ进行AOP实现,因此,如果不进行案例编织,安全性方面将无法工作。

术语“ AspectJ自动代理”与将AspectJ用作AOP实现无关—它是一项功能,允许您在Spring AOP中使用AspectJ API(而不是实现)。

因此,在您的情况下,您可以使用Spring AOP实现,因为控制器是Spring
bean,因此应删除mode="aspectj"。还要注意,您的控制器应具有无参数的构造函数-这是Spring AOP的局限之一。



 类似资料:
  • 我试图让我的Spring MVC应用程序更好地使用Spring@Securited注释和AspectJ自动代理,但它似乎没有代理或识别我的@Securited注释。我有一个这样的控制器: 和一个看起来如下所示的spring security XML: 代码: 我是不是漏掉了什么?我尝试将@EnableAspectJAutoProxy(proxyTargetClass=true)添加到应用程序配置中

  • 问题内容: 我试图使用@Secured(“ ADMIN”)(没有任何XML,只有Java配置,Spring Boot)来设置方法安全注释。但是无法通过角色进行访问。 安全配置: 我想限制对控制器方法的访问: 通过url限制访问的工作方式是: 也许我忘记指定要限制角色了? UPD: 按照规则,在控制器层或服务层必须在哪一层? 问题答案: 这个问题解决了。 我加 在控制器中,我更改为

  • 问题内容: 我发现了许多类似的问题,但都没有解决我的问题。我的问题是可以访问的功能 我的spring-security.xml代码如下。 当我添加 我的代码时显示找不到资源错误,并且当我删除我的代码时成功执行但可以访问函数 我的控制器功能是。 问题答案: 你应该有 如果您希望注释起作用。 回答评论: 看来您缺少依赖性。 如果您正在使用Maven,则需要: 如果没有,你可以从这里拿到罐子。

  • 我似乎对JFreeChart XYPlot中的测试注释有问题。我可以如下设置文本注释 我还可以通过添加 但是如果我试图改变字体类型/大小或颜色,它似乎不起作用 字体保持黑色,并为默认类型/大小。奇怪的是,在烛台图表上,功能似乎很好,但不是简单的绘图 其他人有没有经历过这种情况,或者我做错了什么? ======== 我似乎已经解决了这个问题。在我的烛台图表代码中,数据集在构建时添加到图表中。添加文本

  • 问题内容: 由生成的解析器表现出以下有趣的行为,这阻止了我编写模式来解析字符串,例如: 我调试了代码,似乎问题是由年份字段解析超出了字符串的末尾引起的(三个y的最大宽度始终为19)。但是,我不明白如果没有最后的文字,它如何对模式起作用。 有什么办法可以不必使用格式化程序生成器来解决此问题? 编辑: 由于下面的Jarrod确认这是越野车,因此我进行了更多的谷歌搜索,最后找到了错误报告: http:/

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