我刚刚从 spring-security 5.0.x 升级到 5.1.3,在容器内部署时,我发现我的服务没有从 spring-security 获取经过身份验证的用户主体。
相反,看起来 spring-webmvc 被实例化为我的用户主体的另一个实例,但没有 spring-security 提供的所有用户和 LDAP 详细信息。
我发现了一条来自 Spring https://github.com/spring-projects/spring-security/issues/3771 的消息,它似乎相关,但它说我需要从旧AuthenticationPrincipalArgumentResolver
迁移到新,但我没有在任何地方明确使用它。
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
@Controller
public class MyService {
@RequestMapping(value = "endpoint1",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String myEndpoint(
@AuthenticationPrincipal(errorOnInvalidType = true) CustomUser user) {
我什至把errorOnInvalidType
放在那里,但无济于事。
我还尝试根据文档创建自定义AuthenticationPrincipal
注释 https://docs.spring.io/spring-security/site/docs/5.1.3.RELEASE/api/
看起来AuthenticationPrincipalArgumentResolver
确实没有完成它的工作,但从调试中我看到升级版本和旧的弃用版本都没有被调用(两者都在spring-security-web
)相反,在创建不需要的空主体时,我在堆栈中看到了很多spring-webmvc
类,例如HandlerMethodArgumentResolverComposite
,所以在我看来,我好像不小心删除了配置的关键部分——注释或 jar 或实现。
谁能指出我的错误?
好吧,我发现了如何通过手动创建AuthenticationPrincipalArgumentResolver
bean 来强制 Spring 解决AuthenticationPrincipal
。
使用启用网络安全时,身份验证主体的信用为空