代码之家  ›  专栏  ›  技术社区  ›  Adam

spring安全认证并创建主体,但控制器获得不同的无效用户实例

  •  0
  • Adam  · 技术社区  · 6 年前

    我刚从SpringSecurity5.0.x升级到5.1.3,在容器中部署时,我发现我的服务没有从SpringSecurity获得经过身份验证的用户主体。

    相反,spring webmvc看起来是实例化了我的用户主体的另一个实例,但是没有spring security提供的所有用户和LDAP详细信息。

    我发现了一条来自春天的信息 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/

    看起来确实像 没有执行其任务,但从调试中我看不到调用升级版本或旧的不推荐版本(两者都在 spring-security-web )

    相反,我看到很多 spring-webmvc 创建不需要的空主体时堆栈中的类,如 HandlerMethodArgumentResolverComposite ,所以在我看来,我无意中删除了配置的一个关键部分—要么是注释,要么是jar,要么是实现。

    有人能指出我的错误吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Adam    6 年前

    好吧,我找到了如何强迫弹簧来解决 AuthenticationPrincipal 通过手动创建 AuthenticationPrincipalArgumentResolver 豆子。

    AuthenticationPrincipal is empty when using EnableWebSecurity