Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot中的复杂身份验证_Java_Spring_Authentication_Spring Boot - Fatal编程技术网

Java Spring Boot中的复杂身份验证

Java Spring Boot中的复杂身份验证,java,spring,authentication,spring-boot,Java,Spring,Authentication,Spring Boot,我正在尝试从Spring Boot应用程序与外部提供商进行身份验证,我需要为第三方软件设备编写代码。该应用程序在该外部软件上发出命令,因此连接和操作需要用户凭据 需要使用表单中提供的用户名和密码对Active Directory数据库(检查用户是否存在于公司中)执行身份验证,然后使用内部数据库告知应用程序是否允许用户使用应用程序以及他是否是管理员(用于稍后自定义菜单栏)。 然后,通过服务器上的二进制可执行文件(使用ProcessBuilder),使用此外部软件对用户进行身份验证。这有点复杂,但这

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

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

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

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

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

我的登录控制器包含:

@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模块中有一个工作代码,它希望重用,但不知道如何重用。

与类似的答案一样,您需要创建自己的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);
}

}
在这里,您可以自动连接在以下时间之前创建的customAuthenticationProvider

@Autowired
private CustomAuthenticationProvider authenticationProvider;

这样做了,尽管我仍在编写身份验证部分,并且我必须扩展UsernamePasswordAuthenticationFilter,以便使用user、password和一个附加字段进行身份验证,这是身份验证配置文件(显然,必须在实际身份验证之前知道)。然后,我需要用户从成功登录后第三方软件将报告的角色列表中选择他的角色,这是我不确定如何以用户体验合理的方式为最终用户完成的
@Autowired
private CustomAuthenticationProvider authenticationProvider;