[英]Complex authentication in Spring Boot
我正在尝试通过Spring Boot应用程序与外部提供程序进行身份验证,我将需要为第三方软件设备进行编码。 该应用程序在该外部软件上发布命令,因此需要用户凭证才能进行连接和操作。
需要使用针对Active Directory数据库的表单中提供的用户名和密码来执行身份验证(检查用户是否存在于公司中),然后使用内部数据库来告知应用程序是否允许用户使用该应用程序以及是否允许使用该应用程序他是不是管理员(稍后用于自定义菜单栏)。 然后,通过服务器上存在的二进制可执行文件(使用ProcessBuilder),使用此外部软件对用户进行身份验证。 这有点复杂,但这是必须的,因为有外部约束。
此外,一旦用户在此第三方软件中进行了身份验证,他必须从包含该用户可用所有角色的列表中选择一个角色。 只有在此之后,连接才最终建立,我们必须将用户重定向到他可以使用该应用程序的主页。
登录页面显示一个带有用户名和密码字段的表单,以及一个将触发身份验证过程并向用户显示角色列表的按钮,选择一个按钮并单击另一个按钮后,将选择角色并重定向用户到主页。
问题是我没有任何线索可以在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模块中有工作代码,该代码想重用但不知道如何使用。
就像这里的类似答案一样,您需要创建自己的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类中,该类扩展了此configure方法中的WebSecurityConfigurerAdapter覆盖:
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(this.authenticationProvider);
}
在哪里可以自动装配之前创建的customAuthenticationProvider :
@Autowired
private CustomAuthenticationProvider authenticationProvider;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.