gpt4 book ai didi

java - Spring Boot 中的复杂身份验证

转载 作者:行者123 更新时间:2023-11-30 03:06:25 24 4
gpt4 key购买 nike

我正在尝试通过外部提供商从 Spring Boot 应用程序完成身份验证,我需要为第三方软件设备编写代码。该应用程序对该外部软件发出命令,因此需要用户凭据才能连接和操作。

需要使用表单中提供的用户名和密码针对 Active Directory 数据库(检查用户是否存在于公司中)执行身份验证,然后使用内部数据库告诉应用程序是否允许用户使用应用程序以及他是否是管理员(用于稍后自定义菜单栏)。然后,通过服务器上存在的二进制可执行文件(使用 ProcessBuilder)使用此外部软件对用户进行身份验证。这有点复杂,但由于外部约束,它必须如此。

此外,一旦用户在此第 3 方软件中通过身份验证,他必须从包含该用户可用的所有角色的列表中选择一个角色。只有在此之后,连接才最终建立,我们必须将用户重定向到可以使用该应用程序的主页。

登录页面显示一个带有用户名和密码字段的表单,以及一个按钮,该按钮将触发身份验证过程并向用户显示角色列表,选择一个角色并单击另一个按钮后,将选择角色并选择用户将被重定向到主页。

问题是我没有任何线索在 Spring Boot 中实现这一点。

我的 LoginController 包含:

@Inject
public LoginController(final LoginService loginService) {
this.loginService = loginService;
}

@RequestMapping("/login.html")
public ModelAndView getLoginView() {
LOGGER.debug("Received request to get login view");
ModelMap model = new ModelMap();
model.addAttribute("authenticationTypes",loginService.getAuthenticationTypes());
model.addAttribute(loginService);
return new ModelAndView("login", model);
}

我在一个旧的 JSF 应用程序中使用的 LoginServiceImpl 模块中有工作代码,该应用程序想要重用但不知道如何重用。

最佳答案

喜欢类似的答案here ,您需要创建自己的 CustomAuthenticationProvider,它必须实现 AuthenticationProvider

例如:

@Component
public class CustomAuthenticationProvider
implements AuthenticationProvider {

@Autowired
private ThirdPartyClient thirdPartyClient;

public void setAtpClient(ThirdPartyClient atpClient) {
this.thirdPartyClient = atpClient;
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();


Request3rd requestTO = new AtpAuthenticateRequestDTO();
requestTO.setPassword(password);
requestTO.setUsername(username);
Response3rd authenticate = this.thirdPartyClient.authenticate(requestTO);

if (authenticate != null) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
Authentication auth = new UsernamePasswordAuthenticationToken(authenticate.getUsername(), password, grantedAuths);
return auth;
} else {
return null;
}
}

@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

}

然后在 SecurityConfig 类中,该类扩展了此配置方法中的 WebSecurityConfigurerAdapter 重写:

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(this.authenticationProvider);
}

您可以在其中 Autowiring 之前创建的customAuthenticationProvider:

@Autowired
private CustomAuthenticationProvider authenticationProvider;

关于java - Spring Boot 中的复杂身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34658372/

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