gpt4 book ai didi

java - 使用AOP拦截Spring社交控制

转载 作者:行者123 更新时间:2023-12-01 11:08:24 26 4
gpt4 key购买 nike

我的目标是拦截 ConnectController 类中的 ConnectionStatus 方法,该类是 Spring Social Web 的一部分:

完整方法植入:

/**
* Render the status of the connections to the service provider to the user as HTML in their web browser.
*/
@RequestMapping(value="/{providerId}", method=RequestMethod.GET)
public String connectionStatus(@PathVariable String providerId, NativeWebRequest request, Model model) {
setNoCache(request);
processFlash(request, model);
List<Connection<?>> connections = connectionRepository.findConnections(providerId);
setNoCache(request);
if (connections.isEmpty()) {
return connectView(providerId);
} else {
model.addAttribute("connections", connections);
return connectedView(providerId);
}
}

我有以下方面类来切入上述方法:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.context.request.NativeWebRequest;

@Aspect
public class SocialConnectionAspect {

private static final Logger log = LoggerFactory.getLogger(SocialConnectionAspect.class);

@Around("execution(* org.springframework.social.connect.web.ConnectController.connectionStatus(..))")
public void processSocialConnesction(final ProceedingJoinPoint pjp) throws Throwable {
log.debug(pjp.getSignature().toLongString());
Object[] args = pjp.getArgs();
for (Object arg : args) {
log.debug(arg.getClass().getSimpleName());
boolean b = arg instanceof String;
log.debug("is String: " + b);
b = arg instanceof Model;
log.debug("is Model: " + b);
b = arg instanceof NativeWebRequest;
log.debug("is NativeWebRequest: " + b);
}
pjp.proceed(args);
}

}

但是,每次我尝试建立社交联系时,页面都会抛出以下异常:

type Exception report

message Request processing failed; nested exception is java.lang.IllegalStateException: The mapped controller method class 'org.springframework.social.connect.web.ConnectController' is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy89'. If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: The mapped controller method class 'org.springframework.social.connect.web.ConnectController' is not an instance of the actual controller bean instance 'com.sun.proxy.$Proxy89'. If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying.
HandlerMethod details:
Controller [com.sun.proxy.$Proxy89]
Method [public org.springframework.web.servlet.view.RedirectView org.springframework.social.connect.web.ConnectController.connect(java.lang.String,org.springframework.web.context.request.NativeWebRequest)]
Resolved arguments:
[0] [type=java.lang.String] [value=twitter]
[1] [type=org.springframework.web.context.request.ServletWebRequest] [value=ServletWebRequest: uri=/myproject/connect/twitter;client=0:0:0:0:0:0:0:1;session=85AF73CB172D0682D4FDA52E66DFE149;user=test.username1]

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:105)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:119)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  • 我不确定我在这里做错了什么。甚至可以切入这个 Spring Controller 吗?

最佳答案

您必须启用 Spring 使用 CGLIB 来代理您使用方面的类:

@EnableAspectJAutoProxy(proxyTargetClass=true)

在默认设置下,Spring 仅使用 Java 代理。如果您的类没有实现任何接口(interface)(在您的例子中为 ConnectController),则 Java 代理将不起作用,因此需要 CGLIB 代理。

但在您的情况下,ConnectController 没有默认构造函数,因此 CGLIB 也会失败。您使用的是哪个版本的 Spring?从 4.0 开始,您可以使用 Objenesis 实例化此类。看看https://jira.spring.io/browse/SPR-10594

关于java - 使用AOP拦截Spring社交控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com