首页 > 解决方案 > 为什么总是需要完全身份验证才能访问此资源而不是 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 。

任何人都可以请帮忙。

标签: javaspring-bootspring-security

解决方案


推荐阅读