java - 为什么总是需要完全身份验证才能访问此资源而不是 Spring Boot 中的登录弹出窗口
问题描述
在我的代码中Full authentication is required to access this resource
实现后加载 url 时总是得到。authenticationEntryPoint
在此之前按预期工作,但是当给出错误的登录详细信息时,页面会再次加载但没有给出正确的消息,以防止authenticationEntryPoint
在我的代码中实现。
代码如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic()
.authenticationEntryPoint(new MyBasicAuthenticationEntryPoint());
http.addFilterAt(getCustomAuthenticationFilter(), BasicAuthenticationFilter.class);
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
org.springframework.security.core.AuthenticationException authException)
throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getOutputStream().println(authException.getMessage());
response.getOutputStream().flush();
response.getOutputStream().close();
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("localhost");
super.afterPropertiesSet();
}
}
自定义过滤器代码:
public class CustomAuthenticationFilter extends BasicAuthenticationFilter {
@Autowired
AuthUsersService authUsersService;
@Autowired
public CustomAuthenticationFilter(AuthenticationManager authenticationManager) {
super(authenticationManager);
}
@Override
protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
Authentication authResult) throws IOException {
System.out.println("Successful");
return;
}
@Override
protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException {
System.out.println("UN-Successful");
return;
}
}
我不明白为什么这条Full authentication is required to access this resource
消息总是出现而不是 login popup 。
任何人都可以请帮忙。
解决方案
推荐阅读
- python - client.py 中无法登录 fbchat 错误
- python - 自动化无聊的东西(不能让 BeautifulSoup 解析 HTML)
- javascript - 使用 html 或客户端脚本从另一个文本文件导入元素
- c# - 如何在 Azure 表存储中使用 partitionkey 加快查询速度
- sql - 在执行其他类型的聚合时获得最高类别
- robotframework - 任何关键字(在 selenium2library 中)的可用性以提取网页的源代码
- php - 简单的表单,但输入按钮正在执行相同的操作
- python - 即使在屏幕边缘也能获得鼠标相对运动
- python - 如何将具有相似名称的列的 pandas 数据框转换为行?
- swift - SwiftUI 在表单选择器中传递键和值