- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
@Scheduled
注释方法是否在任何 SecurityContext
之外?当我尝试使用以下内容时,它始终将 securitycontext/auth
设为 null。如果这不可能,那么运行用户特定身份验证感知计划任务的正确方法是什么?
@Scheduled(fixedDelay = 10000)
// This method will send notifications to the current user
public void sendUserNotifications() {
SecurityContext sc = SecurityContextHolder.getContext();
Authentication auth = sc.getAuthentication();
if(auth == null) {
log.info(">> SecurityContext=[{}], Authentication[auth] is {}, please login to receive notifications", sc, auth);
return;
}
最佳答案
给定的摘录永远行不通。
解释:
native session 管理由底层 servlet API 和容器提供。 HttpSession
对象仅在用户使用 request.getSession()
登录时创建,稍后此 HttpSession
将由 SecurityContextHolder.getContext().getAuthentication();
内部。这意味着它与用户的请求有关,这是一个容器管理的线程,而不是您的调度程序将在其上运行的普通旧 JVM 线程。
这里你的调度器对用户没有任何依赖,它将在 JVM 管理的线程上独立运行。因此根本不会创建 HttpRequest/HttpSession
对象。
想象一下,当您第一次启动应用程序时,还没有用户登录,那么这里会是什么情况。
因此,您将始终只将 securitycontext/auth
设为 null。
回答你的问题
If this is not possible then what could be the right way to run userspecific authentication aware scheduled task?
我现在能想到的一种方法是,使用spring提供的SessionRegistry
。它跟踪所有当前登录的用户。
因此,您可以通过 Autowiring 到此调度程序来传递此 SessionRegistry
对象,并获取所有主体/登录用户的列表并向他们发送通知。
类似下面的内容-
@EnableScheduling
@Component
public class MyScheduler {
@Autowired
@Qualifier("sessionRegistry")
private SessionRegistry sessionRegistry;
@Scheduled(fixedDelay = 10000)
// This method will send notifications to the current user
public void sendUserNotifications() {
List<UserDetails> principals = sessionRegistry.getAllPrincipals()
.stream()
.filter(principal -> principal instanceof UserDetails)
.map(UserDetails.class::cast)
.collect(Collectors.toList());
// send notification to all users.
}
此外,您必须首先在您的安全配置中启用 sessionRegistry,您必须监听 HTTP session ,然后在您的安全配置中配置注册表。
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
...
servletContext.addListener(HttpSessionEventPublisher.class);
}
}
和安全配置-
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
// ...
http.sessionManagement().maxSession(1).sessionRegistry(sessionRegistry());
}
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
}
有关如何获取当前登录用户的更多详细信息 see here .
关于java - Spring @Scheduled 方法 SecurityContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62069695/
端点 Jersey . 我想用 ContainerRequestFilter 保护端点 @Provider @Secured public class AuthorizationRequestFilt
我看过很多关于这个的帖子,但我不明白我做错了什么。 服务.yml: parameters: services: bar.menu_builder: class: Foo\Bar
我对 Spring Integration 中的 SecurityContext 传播有一些困惑。 以下是文档的要点: http://docs.spring.io/spring-integration
我创建了一个自定义的 AuthenticationProvider,因此我必须返回一个包含用户的用户名和密码的 Authentication 实现(例如 UsernamePasswordAuthent
我正在尝试为 403(拒绝访问)和 500(内部服务器错误)等错误编写自定义错误页面。它们将从 Velocity 模板呈现,并使用用户的语言环境翻译所有消息。身份验证和语言环境解析在应用程序中运行良好
在我的 spring 应用程序中,我希望 SecurityContext 始终包含一个 Authentication。如果它不是常规的 UsernamePasswordAuthenticationTo
官方 Angular Security Guide谈到 4 个安全上下文:HTML、Url、Style 和 Resource Url。 每个人负责清理相应类型的资源。 此外,DomSanitizer
我在我的项目中使用 Spring Security。 我有更改登录的功能。为了实现这个目标,我使用以下代码 Authentication authentication = ... SecurityCo
我有一个 Quarkus 应用程序,我在其中实现了 ContainerRequestFilter 接口(interface)来保存来自传入请求的 header : @PreMatching publi
@Scheduled 注释方法是否在任何 SecurityContext 之外?当我尝试使用以下内容时,它始终将 securitycontext/auth 设为 null。如果这不可能,那么运行用户特
我正在使用 Spring Security 设置开发 Spring MVC 应用程序,该应用程序通过 LDAP 对 Active Directory 用户进行身份验证。我正在尝试设置 AOP 以记录调
我正在使用 securityContext 进行测试,但是当我将 runAsNonRoot 设置为 true 时我无法启动 pod。我使用 vagrant 以用户 abdelghani 的身份将一个
当我尝试在 symfony 演示中访问应用程序/示例时,出现以下错误 Error: The Symfony\Component\Security\Core\SecurityContext class
在我的k8s部署的securityContext.runAsUser部分中选择要运行的用户与在Dockerfile中使用USER myuser指定用户之间有什么区别? 我特别感兴趣是否存在与USER
我正在创建泽西网络服务。 Jersey 文档的一部分: Security information of a request is available by injecting a JAX-RS Sec
我正在为我的 Dropwizard + Liquibase + Angular 应用程序编写集成测试来测试 REST 服务。我的应用程序使用 cookie 进行基本身份验证。 所以我创建了 Class
我正在研究 Spring Security 的不同类实现。我知道我们将 Authentication 对象设置为 SecurityContext ThreadLocal 对象: UsernamePas
我是 Spring Boot 和 Spring Security 的新手,继承了一个使用它们的 webapp 项目。我们将把 webapp 迁移到新的部署环境。我们将要改变的事情之一是身份验证机制,以
我要问的问题有点棘手,我还没有找到任何答案。也许是因为我在寻找错误的东西。但我希望你能在这方面帮助我。 我用了following tutorial实现使用 token 而不是基本用户/密码身份验证的自
我想检查集群中的 Pod 是否作为特权 Pod 运行,这可能表明我们可能存在安全问题,因此我检查是否特权:true 但是根据securityContext:规范还有其他字段,例如 allowPrivi
我是一名优秀的程序员,十分优秀!