gpt4 book ai didi

spring - 将 Spring Security 与 Spring Batch 结合使用

转载 作者:行者123 更新时间:2023-12-01 10:59:13 25 4
gpt4 key购买 nike

我有一个使用 @PreAuthorize 注释保护的 Spring 服务层。到目前为止,这已通过 Web 应用程序使用。现在我需要启用我们的 SpringBatch 作业来使用这些服务。

在调用这些服务方法之前启用作业授权它们的最直接方法是什么?

最佳答案

我遇到了同样的问题,这是我解决它的方法:

在我的 tasklet 中,我调用了这个方法:

securityUtility.authenticateAs("login", "password");

这是在我的 SecurityUtility 类中编写的 authenticateAs 方法的定义,该类注入(inject)到我的 tasklet 中,实现了 ApplicationContextAware :

/**
* Authenticate a user
* @param username
* @param password
* @return
*/
public Authentication authenticateAs(String username, String password) {
ProviderManager providerManager = (ProviderManager)applicationContext.getBean("authenticationManager");
Authentication authentication = providerManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
setAuthentication(authentication);
return authentication;
}

效果很好,如果您需要更多详细信息,请告诉我;)

关于spring - 将 Spring Security 与 Spring Batch 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com