Java Spring Security在使用RequestParam时一直要求登录

Java Spring Security在使用RequestParam时一直要求登录,java,spring,spring-boot,spring-mvc,spring-security,Java,Spring,Spring Boot,Spring Mvc,Spring Security,我正在尝试构建我的应用程序的后端部分,我想使用rest控制器,并使用spring security保护它们。 这是配置文件: @Override protected void configure(HttpSecurity http) throws Exception { http.cors().configurationSource(corsConfigurationSource()).and().csrf(). disable()

我正在尝试构建我的应用程序的后端部分,我想使用rest控制器,并使用spring security保护它们。 这是配置文件:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().configurationSource(corsConfigurationSource()).and().csrf().
                disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }
我还有两个控制器:

@GetMapping(path = "/testOFWork")
    public AuthBean test() {
        System.out.println("worked");
        return new AuthBean("You are authenticated already");
    }

@GetMapping(path = "/getUserInfo")
    public User getInfo(@RequestParam(value = "username") String user) {
        return dao.getByUserName(user);
    }
第一个是完美的工作。当我尝试访问它时:spring要求我登录。我这样做了,它会给我一个信息。 但是,当我试图进入第二个,它只是不断要求我登录。如果我尝试去第一个,它工作得非常好,它会要求我再次登录。 通过实验,我发现RequestParam导致了这个问题。 我知道问题可能出在配置本身,但我无法理解

感谢您提前回复!"

编辑 下面是完整的配置类:

@Autowired
    UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().configurationSource(corsConfigurationSource()).and().csrf().
                disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();
    }


    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        List<String> allowOrigins = Arrays.asList("*");
        configuration.setAllowedOrigins(allowOrigins);
        configuration.setAllowedMethods(singletonList("*"));
        configuration.setAllowedHeaders(singletonList("*"));
        //in case authentication is enabled this flag MUST be set, otherwise CORS requests will fail
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }
@Autowired
用户详细信息服务用户详细信息服务;
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http.cors().configurationSource(corsConfigurationSource())和().csrf()。
禁用()
.授权请求()
.antMatchers(HttpMethod.OPTIONS,“/**”)
.permitAll()
.anyRequest()
.authenticated()
.及()
.httpBasic();
}
CorsConfiguration源CorsConfiguration源(){
CorsConfiguration配置=新的CorsConfiguration();
List allowOrigins=Arrays.asList(“*”);
配置.setAllowedOrigins(allowOrigins);
configuration.setAllowedMethods(singletonList(“*”);
setAllowedHeaders(singletonList(“*”);
//如果启用了身份验证,则必须设置此标志,否则CORS请求将失败
配置.setAllowCredentials(true);
UrlBasedCorsConfigurationSource=新的UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration(“/**”,配置);
返回源;
}
@自动连线
public void configAuthentication(AuthenticationManagerBuilder auth)引发异常{
auth.userDetailsService(userDetailsService).passwordEncoder(新的BCryptPasswordEncoder());
}

您告诉您的spring安全性检查用户是否在.antMatchers(HttpMethod.OPTIONS,“/**”)中对任何请求进行了身份验证

编辑

您的代码应该如下所示:

 http.authorizeRequests()
          .antMatchers("/securityNone").permitAll()
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .authenticationEntryPoint(authenticationEntryPoint);

        http.addFilterAfter(new CustomFilter(),
          BasicAuthenticationFilter.class);

您需要将未受保护的访问与受保护的访问分开

您是如何测试这一点的?使用浏览器?谢谢您的回答。我想我可以这样做。但最好是阅读spring io的优秀指南并实现自定义过滤器。我希望您过得愉快。