方面未在 Spring 中执行

2024-03-20

我正在编写一个几乎完全受登录保护的网站(我正在使用 Spring Security)。不过,有些页面不受保护(主页、登录页面、注册页面、忘记密码页面……),我想要实现的是:

  • 如果用户在访问这些非安全页面时未登录, 正常显示它们
  • 如果用户已经登录,则重定向到 主页(或指定的页面)redirectTo注释元素)

当然,我想避免将其放入每个控制器方法中:

if(loggedIn())
{
    // Redirect
}
else
{
    // Return the view
}

因此我想使用 AOP。

我创建了注释@NonSecured我编码了以下方面:

@Aspect
public class LoggedInRedirectAspect
{
    @Autowired
    private UserService userService;

    @Around("execution(@my.package.annotation.NonSecured * *(..))")
    public void redirect(ProceedingJoinPoint point) throws Throwable
    {
        System.out.println("Test");
        point.proceed();
    }
}

带注释的方法示例:

@Controller
@RequestMapping("/")
public class HomeController
{
    @NonSecured(redirectTo = "my-profile")
    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model,
                        HttpServletRequest request) throws Exception
    {
        // Show home page
    }
}

applicationContext.xml 重要位:

<context:annotation-config />
<context:component-scan base-package="my.package" />

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

<bean id="loggedInRedirectAspect" class="my.package.aspect.LoggedInRedirectAspect" />
<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="loggedInRedirectAspect" />
</aop:aspectj-autoproxy>

The problem is that the method redirect(...) in the aspect never gets called. Aspects in general are working fine, in fact the following method in the aspect will get called: The following advice gets called but doesn't get called for the controller methods.

@Around("execution(* *(..))")
public void redirect(ProceedingJoinPoint point) throws Throwable
{
    point.proceed();
}

我在切入点中做错了什么吗?

谢谢。

Update:这个问题中的最后一个片段被调用,但仍然没有被控制器方法调用。


@satoshi,我认为你遇到的问题是因为你正在使用Spring-AOP,它只能为具有接口的bean创建AOP代理 - 在你的情况下,控制器没有接口。

修复方法可能是使用 AspectJ 进行编译时/加载时编织,而不使用 Spring AOP 或在类路径中包含 cglib jar 并强制创建基于 cglib 的代理:

<aop:aspectj-autoproxy proxy-target-class="true"/>

更新: 编译时编织可以使用 maven 插件完成,showWeaveInfo 配置将准确显示哪些类已被编织:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.10</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.6.10</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outxml>true</outxml>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
        </aspectLibraries>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

方面未在 Spring 中执行 的相关文章

随机推荐

  • Julia 作用域:为什么这个函数会修改全局变量?

    我是 Julia 的新手 但到目前为止我还是它的粉丝 但根据多年的 R 编程经验 一些范围规则让我感到困惑 我们来看看这个函数 这与我的预期完全一致 function foo1 x y x t 1 while t lt 1000 t 1 y
  • 如何在 java、MySQL 和 Tomcat 6 中使用连接池

    如何在Java MySQL Tomcat 6中使用连接池 我读过这篇文章http dev mysql com tech resources articles connection pooling with connectorj html h
  • IHttpActionResult 与异步任务

    我见过的大多数 Web API 2 0 方法都会返回IHttpActionResult 它被定义为 定义异步创建 System Net Http HttpResponseMessage 的命令 的接口 我对方法返回时发生的情况有点困惑asy
  • Jenkins 管道、bash 和管道

    我有一个输出字符串 我想对其运行 tr 和 jq 命令 管道是这样有意义的 IP sh script echo spawnServer jq 0 tr d returnStdout true 不幸的是 詹金斯管道讨厌管道 所以我得到的是 t
  • 我如何检查移动数据或 wifi 是否打开或关闭。 ios 快速

    在我的应用程序中 我正在检查移动数据是否关闭 是否会显示弹出窗口 例如检查您的数据连接 为此我写了这段代码 import Foundation import SystemConfiguration public class Reachabi
  • 在 Cypress 测试中尝试使用 Auth0 登录时出错

    我们的应用程序只能由经过身份验证的用户访问 并且我们使用Auth0 https auth0 com 用于身份验证 我们已经开始编写 Cypress 测试 并且在每次测试之前尝试使用 Auth0 JavaScript 客户端登录 第一个测试总
  • F#:带有类型定义的引用?

    我正在使用引号 但看不到类型定义的表达式模式 真的没有吗 还是我错过了什么 lt type MyType name string member x Name name gt 给出 引号文字中出现意外的关键字 type 你不能 你只能引用代码
  • Python Spark Dataframe 到 Elasticsearch

    我不知道如何使用 Spark 中的 python 将数据帧写入 Elasticsearch 我按照以下步骤操作here https db blog web cern ch blog prasanth kothuri 2016 05 inte
  • 如何在eclipse软件中获得Redo

    我需要找回我的程序 如何获取Redoeclipse pls 的键盘快捷键任何人都可以帮助我 You can use below code to implement Redo public class TextAreaDemoB extend
  • 如何对 numpy 数组进行舍入?

    我有一个 numpy 数组 如下所示 data np array 1 60130719e 01 9 93827160e 01 3 63108206e 04 我想将每个元素四舍五入到小数点后两位 我怎样才能这样做呢 Numpy 提供了两种相同
  • 计算一年内信用卡每月最低还款额的代码

    请我尝试找出我的推理有什么问题 从而找出我的结果 我正在学习一门在线课程 我需要计算 12 个月内消除信用卡债务所需的最低金额 我得到了年利率 债务金额 余额 的值以及每月还款额应增加的值 10 的倍数 根据我的推理 我生成的代码应该在几个
  • 如何查找Active Directory的全局编录?

    我想在 Active Directory 环境中搜索用户GC DC xxx DC yyy DC zzz格式 但是 如何以编程方式在任意 Active Directory 环境中查找全局编录 每个域名总是对应一个全局目录吗 我可以尝试其他替代
  • 将图像旋转 X 度 C# wpf [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 这已经困扰我很多年了 我只想要一个简单的方法来将图像旋转 X 度 这是针对炮塔防御游戏 其中炮塔需要向某个方向射击 我想要这样的东西 pub
  • Linux 上 C 语言中字符数组和指针的分段错误

    所以我有以下程序 int main char one computer char two another two 1 b one 1 b return 0 它在 one 1 b 行上出现段错误 这是有道理的 因为指针 one 指向的内存必须
  • 如果在图表渲染完成之前调用 FSharpChart.SaveAs (),则保存空白图像

    在 F Interactive 中运行时 我希望以下代码创建一个简单的饼图并将其保存到磁盘 let pie FSharpChart Pie Apples 1 Oranges 2 Bananas 3 FSharpChart SaveAs te
  • 使用Powershell将Excel中的文本转换为数字

    我有一个 Excel 文件 其中三列设置为 数字 但是 当我打开该文件时 我会看到以下内容 我在这里找到了一个有用的链接 堆栈溢出链接 https stackoverflow com questions 68941811 attemptin
  • 对于 AMD 模块,什么时候(或为什么)可以在 Define() 中使用 require() ?

    我对AMD模块 例如使用RequireJs或curl js 的理解是 require 用于异步加载不同的模块 加载后执行回调 fn 要定义模块 您将有单独的脚本使用define 但我见过一些模块使用require 在它们的函数定义中 例如
  • 计算 pandas 系列的*滚动*最大回撤

    编写一个计算时间序列最大回撤的函数非常容易 写下来需要一点点思考O n 时间而不是O n 2 时间 但情况并没有那么糟糕 这将起作用 import numpy as np import pandas as pd import matplot
  • 如何构建一个同时暴露rest和soap服务的Spring Boot jar

    我一直在考虑为开发人员社区构建一个测试 jar 以便公开 API 的下一个版本的预览 让存根返回具有精确格式的预期响应等 我们确实有 REST 和 SOAP API 我想构建 REST 服务不会有任何问题 因为网络上充斥着示例 令人惊讶的是
  • 方面未在 Spring 中执行

    我正在编写一个几乎完全受登录保护的网站 我正在使用 Spring Security 不过 有些页面不受保护 主页 登录页面 注册页面 忘记密码页面 我想要实现的是 如果用户在访问这些非安全页面时未登录 正常显示它们 如果用户已经登录 则重定