gpt4 book ai didi

java - AuthenticationEntryPoint 仅有时被称为

转载 作者:搜寻专家 更新时间:2023-11-01 01:50:16 24 4
gpt4 key购买 nike

我有一个简单的 AuthenticationEntryPoint,它应该为未经授权的请求设置 WWW-Authenticate header 。

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
response.setHeader("WWW-Authenticate", "FormBased");
response.sendError(401, authException.getMessage());
}
}

我在 AuthorizationServerConfigurer 的配置方法之一中使用它

@Override
public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception {
authorizationServerSecurityConfigurer.authenticationEntryPoint(authenticationEntryPoint);
}

不过,这个开始方法并不总是被调用。当请求中没有 Authorize header 或 Authorize header 值不以“Basic”开头时,它会被调用。但是,如果 Authorize header 以“Basic”开头,则不会调用开始方法(并且响应的值为 Basic realm="oauth2/client")。如何确保调用此方法?

最佳答案

正如 AliDehghani 所指出的,这是因为 BasicAuthenticationFilter 使用 BasicApplicationEntryPoint 而不管 AuthorizationServerSecurityConfigurer 中声明的 ApplicationEntryPoint >。为了让 BasicAuthenticationFilter 使用我的 CustomApplicationEntryPoint 我需要创建一个新的 CustomBasicAuthenticationFilter 并添加 @Autowire 注释给构造函数:

@Component
public class CustomBasicAuthenticationFilter extends BasicAuthenticationFilter {

@Autowired
public CustomBasicAuthenticationFilter(AuthenticationManager authenticationManager,
AuthenticationEntryPoint authenticationEntryPoint) {
super(authenticationManager, authenticationEntryPoint);
}
}

然后将其添加到 AuthorizationServerConfigurer 的配置方法之一

@Override
public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception {
authorizationServerSecurityConfigurer
.authenticationEntryPoint(authenticationEntryPoint)
.addTokenEndpointAuthenticationFilter(customBasicAuthenticationFilter);
}

现在应用程序使用我的 CustomBasicAuthenticationFilter - 它在功能上等同于 BasicAuthenticationFilter。但是,它现在包含在构造期间声明的 AuthenticationEntryPoint bean - 这是我的 CustomAuthenticationEntryPoint

关于java - AuthenticationEntryPoint 仅有时被称为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123835/

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