如何在ServerSecurityContextRepository上定义一个切入点?

huangapple go评论148阅读模式
英文:

How to define a pointcut on ServerSecurityContextRepository?

问题

我正在尝试创建一个在调用 `ServerSecurityContextRepository.save(ServerWebExchange exchange, SecurityContext context)` 方法之前执行代码的切面但我无法使我的切入点起作用

以下是我迄今为止尝试过的内容
```java
@Aspect
@Component
@Slf4j
public static class ServerSecurityContextRepositoryAspect {

    @Pointcut("within(org.springframework.security.web.server.context.ServerSecurityContextRepository+) && execution(* *.save(..))")
    public void securityContextSaved() {
    }

    @Before("securityContextSaved()")
    public void beforeSecurityContextSaved(JoinPoint jp) {
        log.error("%d".formatted(jp.getArgs().length));
        if (jp.getArgs().length != 2) {
            log.warn("oups");
        }
        if (jp.getArgs()[1] == null) {
            log.warn("SecurityContext destroyed");
        } else {
            log.warn("SecurityContext saved");
        }
    }
}

有任何建议吗?


<details>
<summary>英文:</summary>

I&#39;m trying to create an aspect to execute code before `ServerSecurityContextRepository.save(ServerWebExchange exchange, SecurityContext context)` is called, but I couldn&#39;t get my pointcut working.

Here is what I tried so far:
```java
@Aspect
@Component
@Slf4j
public static class ServerSecurityContextRepositoryAspect {

	@Pointcut(&quot;within(org.springframework.security.web.server.context.ServerSecurityContextRepository+) &amp;&amp; execution(* *.save(..))&quot;)
	public void securityContextSaved() {
	}

	@Before(&quot;securityContextSaved()&quot;)
	public void beforeSecurityContextSaved(JoinPoint jp) {
		log.error(&quot;%d&quot;.formatted(jp.getArgs().length));
		if (jp.getArgs().length != 2) {
			log.warn(&quot;oups&quot;);
		}
		if (jp.getArgs()[1] == null) {
			log.warn(&quot;SecurityContext destroyed&quot;);
		} else {
			log.warn(&quot;SecurityContext saved&quot;);
		}
	}
}

Any suggestion?

答案1

得分: 0

ServerSecurityContextRepository没有作为一个bean在我的应用程序中暴露(在我的代码中使用new实例化)=> Spring不可能使用AOP来对其进行处理...

英文:

I just figured it out: ServerSecurityContextRepository is not exposed as a bean in my application (instantiated with a new in my code) => no chance that Spring can instrument it with AOP...

huangapple
  • 本文由 发表于 2023年8月11日 03:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定