我正在尝试设置与spring webflux安全自定义字符串消息,如果登录/注销成功或失败。除“AuthenticationFailureHandler”外,处理程序都在工作。
文档中提到了一些关于“logoutsuccessURL()”的内容,但它并不存在,并且这些处理程序返回的是mono
所以我有两个问题:
>
如何返回一些字符串作为响应,例如,如果authenticaion失败,那么“无效用户名或密码”或一些json字符串。我试着重定向到一个动作,但是我不能用虚空单声道来做。
为什么authenticationFailureHandler不起作用,而其他处理程序都起作用?那是窃听器吗?
-我尝试用mono.just(“redirect:/some-url”).then()重定向,但它什么也没做。对于答复
您可以在这里找到我的整个代码:https://github.com/iron2414/webfluxauth
这是本文代码的修改版本:https://medium.com/@mgray_94552/reactive-authorisation-in-spring-security-943e6534aaeb
安全配置如下所示:
return http
.exceptionHandling()
.accessDeniedHandler((swe, e) -> {
System.out.println("ACCESS DENIED");
return Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
});
})
.authenticationEntryPoint((swe, e) -> {
System.out.println("AUTHENTICATION ENTRTY POINT");
ServerHttpResponse response = swe.getResponse();
return Mono.fromRunnable(() -> {
swe.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
});
})
.and()
.authorizeExchange()
.pathMatchers("/**").authenticated()
.and()
.formLogin()
.authenticationFailureHandler(MyAuthenticationFailureHandler())
.authenticationSuccessHandler(MyAuthenticationSuccessHandler()).and()
.logout().logoutSuccessHandler(MyLogoutHandler())
.and()
.csrf()
.disable()
.build();
loginFailure处理程序:
@Bean
public MyAuthenticationFailureHandler MyAuthenticationFailureHandler() {
return new MyAuthenticationFailureHandler();
}
@Component
public class MyAuthenticationFailureHandler implements ServerAuthenticationFailureHandler {
@Override
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException e) {
//TODO redirect
System.out.println("AUTHENTICATION FAILURE");
return Mono.empty();
}
}
我遇到了同样的问题,最后使用了一个自定义的DefaultErrorWebExceptionHandler来处理它:
@Component
public class MyGlobalWebExceptionHandler extends DefaultErrorWebExceptionHandler {
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
return route(all(), this::renderErrorResponse);
}
@Override
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
ErrorRepresentation representation = createErrorRepresentation(request);
return ServerResponse
.status(representation.getError().getStatus())
.contentType(APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(representation));
}
private ErrorRepresentation createErrorRepresentation(ServerRequest request) {
boolean includeStackTrace = isIncludeStackTrace(request, MediaType.ALL);
Map<String, Object> error = getErrorAttributes(request, includeStackTrace);
Throwable throwable = getError(request);
if (throwable instanceof AuthenticationException) {
AuthenticationFailureException failureException = new AuthenticationFailureException(throwable.getMessage());
return new ErrorRepresentation(failureException, error.get("path").toString());
}
}
}
对于响应,我必须使用“RedirectServerAuthenticationSuccessHandler”来验证成功。我创建了实现失败的完全相同的类。
身份验证失败处理程序不工作是一个已知问题。为了使其工作,您必须设置登录页。所以我只是将其更新为默认的“/login”
我想在用户发送请求时验证他或她的身份,所以我在SecurityConfig类中添加了以下内容: 我希望当我在地址字段中输入localhost:8080/submit时,页面可以重定向到localhost:8080/login。我的工作者实体具有“工作者”角色: 当我输入“localhost:8080/submit”时,我可以注册一个帐户并重定向到登录页面。但是当我输入正确的用户名和密码时,它会响应
这是我的配置: 显然,我有一个路由/后台/登录的控制器,它使用正确的输入名(_username,_password)呈现登录表单。 当我编译表单时,如果我输入了错误的用户名和密码数据,我就会正确地出现错误的凭据,但是如果我输入了正确的数据,我就会以匿名用户的身份被重定向回登录页面。 一些应用程序/日志。。。 如何在第2行看到登录成功,但当控制器重定向到安全的我获得访问拒绝(第8行) 我错在哪里?非
有没有一种方法可以添加一个登录成功处理程序使用spall-oaut2? 我尝试使用基本身份验证过滤器,但它只过滤客户端凭据,而不是用户凭据。 还是需要创建自定义用户身份验证管理器? 蒂亚
编辑1我知道这个问题已经问过了,但是没有人回答,stacktrace也没有添加到里面,所以我又问了一遍。 如果我安装了twitter应用程序,然后尝试登录,那么应用程序将成功地用twitter登录,但如果我从移动端卸载twitter应用程序,然后尝试登录应用程序,那么我收到以下错误,无法用twitter登录 未能获取请求令牌 我没有看到任何WebView。之后点击登录与twitter按钮。 E/T
问题内容: 我无法摆脱这个错误。我已经通过SSMS添加了“ NT AUTHORITY \ NETWORK”用户以及使用此线程作为参考的相关角色:用户“ NT AUTHORITY \ NETWORKSERVICE”的登录失败 我试图通过Windows服务建立数据库连接。在调试模式下,数据库连接可以正常工作。当我实际尝试运行已安装的服务时,便会出现此错误。 这是我来自app.config的连接字符串: