【发布时间】:2022-01-06 06:34:29
【问题描述】:
我们正在将遗留应用程序迁移到 Spring Boot。为了在我们为用户分配角色之前继续测试,我想覆盖以下内容:
类:SecurityContextHolderAwareRequestWrapper
方法:public boolean isUserInRole(String role)
我创建了一个扩展SecurityContextHolderAwareRequestWrapper 并覆盖isUserInRole() 的新类,如下所示:
@Component
public class MySecurityContextHolderAwareRequestWrapper extends org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper {
public MySecurityContextHolderAwareRequestWrapper(HttpServletRequest request,
AuthenticationTrustResolver trustResolver, String rolePrefix) {
super(request, trustResolver, rolePrefix);
}
@Override
public boolean isUserInRole(String role) {
return true;
}
当应用程序运行时,新 bean 不会取代现有的 SecurityContextHolderAwareRequestWrapper 类。这一点很清楚,因为当新类被实例化时,构造函数不会被注入到SecurityContextHolderAwareRequestWrapper 中的 bean 中。应用程序无法启动,因为找不到新类 MySecurityContextHolderAwareRequestWrappercould 的 AuthenticationTrustResolver 和 String 类型的参数
什么是覆盖 SecurityContextHolderAwareRequestWrapper 或 Spring Boot 框架中的任何类的正确方法?
谢谢
【问题讨论】:
标签: spring-boot spring-security