当前位置: 首页 > 知识库问答 >
问题:

春靴中的Spring安全困惑

郁宾鸿
2023-03-14
UserDetails usd=this.myCustomUserDetailService.loadByUserName(userNameFromToke);//userNameFromToke is a String that got extracted from the token that client provided.

UsernamePasswordAuthenticationToken upat=new UsernamePasswordAuthenticationToken(usd,null,usd.getAuthorities()); //Here in the password I am passing null.

upat.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));

SecurityContextHolder.getContext().setAuthentication(upat);
`ups.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));`

setAuthentication方法和UsernamePasswordAuthenticationToken是如何工作的?我正在传递从令牌中提取的信息,即securityContextHolder.getContext().setAuthentication(upat);以及UsernamePasswordAuthenticationToken构造函数中的userdetails、passowords和权限,但它验证的是“upat”?UsernamePasswordAuthenticationToken是否自动使用“MyCustomUserDetailService”进行内部检查?

我知道这个问题有点冗长,但作为一个初学者,我正在努力理解它背后的机制,我已经对它进行了研究,但这种困惑并没有消失。请帮忙。另外,如果你觉得我的问题有问题,建议我如何简明我的问题。

共有1个答案

淳于思淼
2023-03-14

>

  • UsernamePasswordAuthenticationToken中的详细信息完全由您决定,如果您稍后在应用程序中需要通过身份验证的用户提供更多详细信息,这将非常有用。与调用SecurityContextHolder.getContext().getAuthentication()类似,您可以将此Authentication强制转换为UsernamePasswordAuthenticationToken并出于任何业务目的访问其详细信息;

    您的UsernamePasswordAuthenticationToken没有进行任何验证,您需要通过调用UserDetailsService并检查密码,或者插入并调用AuthenticationManager#Authenticate方法手动进行验证。Spring Security并不关心如何填充SecurityContextHolder。在基本方案中,在将经过身份验证的SecurityContext设置到SecurityContextThold后,SecurityContext将接受此SecurityContext并将其作为属性保存在HttpSession中。在接下来的请求中,此属性将出现在HttpSession中,这样Spring Security将加载SecurityContext并将其设置到SecurityContextHolder中。

    您可以在SecurityContextPersistenceFilter实现中获得更多细节,但我将在这里指出这个问题的具体部分:

    HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
    // Here Spring Security loads the SecurityContext from the HttpSession, this is done before reaching your controllers, before calling the FilterChain.
    SecurityContext contextBeforeChainExecution = this.repo.loadContext(holder); 
    try {
        SecurityContextHolder.setContext(contextBeforeChainExecution);
        if (contextBeforeChainExecution.getAuthentication() == null) {
            logger.debug("Set SecurityContextHolder to empty SecurityContext");
        }
        else {
            if (this.logger.isDebugEnabled()) {
                this.logger
                            .debug(LogMessage.format("Set SecurityContextHolder to %s", contextBeforeChainExecution));
            }
        }
        chain.doFilter(holder.getRequest(), holder.getResponse());
    }
    finally {
        // You've populated the SecurityContextHolder and it'll be available here after you application returns. So it'll get the SecurityContext and persist it in the HttpSession
        SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();
        SecurityContextHolder.clearContext();
        // Persisting the SecurityContext in the HttpSession
        this.repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse());
        request.removeAttribute(FILTER_APPLIED);
        this.logger.debug("Cleared SecurityContextHolder to complete request");
    }
    

  •  类似资料:
    • 客户端需要LDAP和本地JDBC身份验证机制。 它们需要一种对两组用户都通用的授权机制。 应该根据用户的角色限制用户进入某些页面。以及需要应用于每个用户的单独权限(创建、更新、删除)集 那么,我如何实现按页授权,这将由管理员决定谁(哪个角色)可以访问哪个页面? 是否应该在配置中指定每个角色页组合?是否有任何方法可以动态地改变页面和角色,因为角色可能会在以后添加。

    • 我有我的spring boot应用程序,我正在尝试添加Spring Security性,但当我通过postman发出请求时,我不断收到一个403 Forbbiden,联机时我发现我应该在我的配置中添加:“.csrf().disable()”,但它不起作用(如果我在permitAll()中放置路径为:“person/**”的方法,则所有操作都有效) 这是我的代码: 我的用户控制器: My perso

    • 我想在我的Junit5单元测试中将值注入到带有@Value注释的私有字段。 我引用了这个,并使用了ReflectionTestUtils。setField通过注入值解决了我的问题,但在验证方法被调用的次数时失败。 MyClass(我的类别): 测试类: 运行上述测试时出错 我想kafkaTemplate.sendMessage();被调用一次,但被调用两次后添加反射TestUtils。 需要关于如

    • 我有一个spring-boot 1.1.0.build-snapshot项目,它使用Spring-Security spring-security-web:4.0.0.m1。我想在我的H2表中添加数据库表,但在启动时会出现异常(生产和集成测试代码都有)。 以下是我的相关schema.sql文件内容: 我的application.properties文件中有以下内容: 当我启动或运行测试时,我会得到

    • 我想在我的spring boot项目中使用Kafka Streams实时处理。所以我需要Kafka Streams配置,或者我想使用KStreams或KTable,但我在互联网上找不到示例。 我做了制作人和消费者现在我想流实时。

    • 我正在Lynda.com https://www.lynda.com/spring-tutorials/spring-boot-actuator/653258/709599-4.html?autoplay=true上学习spring-boot教程,演示者简单地说关闭管理安全性这就是application.yml的样子 是否有一种方法可以将application.yml设置为默认属性文件,我在这里遗