Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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 AuthenticationException.getAuthentication()的替换_Java_Spring_Login_Spring Security_Openid - Fatal编程技术网

Java AuthenticationException.getAuthentication()的替换

Java AuthenticationException.getAuthentication()的替换,java,spring,login,spring-security,openid,Java,Spring,Login,Spring Security,Openid,以下场景 我们有一个带有openid按钮的登录表单 所有用户都可以使用user/passwd或openid登录eigther 当用户尝试使用openid登录但没有帐户时,他将被重定向到注册表 这在SpringSecurity3.x中非常有效,但在即将发布的4.x版本中不起作用,因为AuthenticationException.getAuthentication()方法已被删除 我们allready的尝试: 将failurehandler替换为successhandler,但这会导致Ope

以下场景

  • 我们有一个带有openid按钮的登录表单
  • 所有用户都可以使用user/passwd或openid登录eigther
  • 当用户尝试使用openid登录但没有帐户时,他将被重定向到注册表
  • 这在SpringSecurity3.x中非常有效,但在即将发布的4.x版本中不起作用,因为AuthenticationException.getAuthentication()方法已被删除
我们allready的尝试:

  • 将failurehandler替换为successhandler,但这会导致OpenId UserDetailService中出现UserNameNotFoundException,因此不会调用该处理程序
  • 在OpenId UserDetailService中实现重定向,但在这里我们无法访问HttpServletRequest和HttpServletResponse
下面是实际的3.x工作代码

@Slf4j
@组成部分
公共类OpenIDAuthenticationFailureHandler Impl扩展了SimpleRuthenticationFailureHandler实现OpenIDAuthenticationFailureHandler{
@非空
私有最终规范化开放属性生成器规范化开放属性生成器;
@自动连线
公共OpenIDAuthenticationFailureHandlerImpl(@Nullable final NormalizedOpenIdAttributesBuilder NormalizedOpenIdAttributesBuilder){
Assert.notNull(normalizedOpenIdAttributesBuilder);
this.normalizedOpenIdAttributesBuilder=normalizedOpenIdAttributesBuilder;
}
私有静态void sessionShouldBePresent(@Nullable final HttpSession session)引发ServletException{
if(会话==null){
日志错误(“未找到会话”);
抛出新的ServletException(“未找到会话”);
}
}
私有静态布尔openIdAuthenticationSuccessfulButUserIsNotRegistered(@Nullable final AuthenticationException){
返回UsernameNotFoundException的异常实例&&
OpenIDAuthenticationToken的异常.getAuthentication()实例&&
OpenIDAuthenticationStatus.SUCCESS==getOpenIdAuthenticationToken(异常).getStatus();
}
私有静态OpenIDAuthenticationToken getOpenIdAuthenticationToken(@Nonnull final AuthenticationException){
返回(OpenIDAuthenticationToken)异常。getAuthentication();
}
@凌驾
身份验证失败(最终HttpServletRequest请求、最终HttpServletResponse响应、最终AuthenticationException异常)引发IOException、ServletException{
if(openIdAuthenticationSuccessfulButUserIsNotRegistered(例外)){
重定向到OpenRegistrationUrl(请求、响应、异常);
}否则{
super.onAuthenticationFailure(请求、响应、异常);
}
}
private void RedirectToOpenedRegistrationUrl(@Nonnull final HttpServletRequest请求,@Nonnull final HttpServletResponse响应,@Nonnull final AuthenticationException异常)引发IOException,ServletException{
final RedirectStrategy RedirectStrategy=新的DefaultRedirectStrategy();
AddOpenIDAttributesTokenSession(请求,getOpenIdAuthenticationToken(异常));
redirectStrategy.sendRedirect(请求、响应,“/signup”);
}
私有void addOpenIDAttributesToken(@Nonnull final HttpServletRequest请求,@Nonnull final OpenIDAuthenticationToken OpenIDAuthenticationToken)抛出ServletException{
最终HttpSession会话=request.getSession(false);
届会应出席(届会);
最终NormalizedOpenIdAttributes NormalizedOpenIdAttributes=normalizedOpenIdAttributesBuilder.build(openIdAuthenticationToken);
session.setAttribute(OPENID\u session\u ATTRIBUTE\u NAME,normalizedOpenIdAttributes);
}
}
Web安全配置类

@Order(securityproperty.ACCESS\u OVERRIDE\u Order)
公共类应用程序安全性扩展了WebSecurity配置适配器{
@自动连线
私有OpenIDAuthenticationFailureHandler OpenIDAuthenticationFailureHandler;
@自动连线
私有OpenIDAuthenticationUserDetails服务OpenIDAuthenticationUserDetails服务;
@凌驾
受保护的void configure(最终HttpSecurity http)引发异常{
http.formLogin()
.login页面(“/login”)
.loginProcessingUrl(“/login/check”)
.permitAll();
http.logout()
.permitAll();
http.exceptionHandling()
.accessDeniedPage(“/拒绝”);
http.openidLogin()
.login页面(“/login”)
.loginProcessingUrl(“/login/openid”)
.failureUrl(“/login/failure”)
.failureHandler(openIDAuthenticationFailureHandler)
.defaultSuccessUrl(“/”,true)
.authenticationUserDetailsService(openIDAuthenticationUserDetailsService)
.permitAll();
}
}
此实现基于此应用程序中的代码


有什么建议我们可以尝试与SpringSecurity4.x兼容吗。我们也搜索了互联网,但我们发现的所有东西都不适用于spring security 4.x,这并不是说这会有多大帮助,而是有人在页面底部发表了一条评论,解释了问题的潜在解决方案:我也有同样的问题。替换内容是什么???是否有任何更新,您是否能够找到解决方案@BigMichi1Didn尚未找到解决方案