Spring Security在使用RequestParam时不断要求登录

huangapple go评论130阅读模式
英文:

Spring Security keeps asking for login when using RequestParam

问题

这是配置代码部分:

  1. @Override
  2. protected void configure(HttpSecurity http) throws Exception {
  3. http.cors().configurationSource(corsConfigurationSource()).and().csrf()
  4. .disable()
  5. .authorizeRequests()
  6. .antMatchers(HttpMethod.OPTIONS, "/**")
  7. .permitAll()
  8. .anyRequest()
  9. .authenticated()
  10. .and()
  11. .httpBasic();
  12. }

这是第一个控制器:

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

这是第二个控制器:

  1. @GetMapping(path = "/getUserInfo")
  2. public User getInfo(@RequestParam(value = "username") String user) {
  3. return dao.getByUserName(user);
  4. }

这是完整的配置类:

  1. @Autowired
  2. UserDetailsService userDetailsService;
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http.cors().configurationSource(corsConfigurationSource()).and().csrf()
  6. .disable()
  7. .authorizeRequests()
  8. .antMatchers(HttpMethod.OPTIONS, "/**")
  9. .permitAll()
  10. .anyRequest()
  11. .authenticated()
  12. .and()
  13. .httpBasic();
  14. }
  15. CorsConfigurationSource corsConfigurationSource() {
  16. CorsConfiguration configuration = new CorsConfiguration();
  17. List<String> allowOrigins = Arrays.asList("*");
  18. configuration.setAllowedOrigins(allowOrigins);
  19. configuration.setAllowedMethods(singletonList("*"));
  20. configuration.setAllowedHeaders(singletonList("*"));
  21. //in case authentication is enabled this flag MUST be set, otherwise CORS requests will fail
  22. configuration.setAllowCredentials(true);
  23. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  24. source.registerCorsConfiguration("/**", configuration);
  25. return source;
  26. }
  27. @Autowired
  28. public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  29. auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
  30. }

如果还有其他需要的翻译,请告诉我。

英文:

I'm trying to build a backend part of my app and I want to use rest controllers and secure them with spring security.
This is the config:

  1. @Override
  2. protected void configure(HttpSecurity http) throws Exception {
  3. http.cors().configurationSource(corsConfigurationSource()).and().csrf().
  4. disable()
  5. .authorizeRequests()
  6. .antMatchers(HttpMethod.OPTIONS, &quot;/**&quot;)
  7. .permitAll()
  8. .anyRequest()
  9. .authenticated()
  10. .and()
  11. .httpBasic();
  12. }

I also have two controllers:

  1. @GetMapping(path = &quot;/testOFWork&quot;)
  2. public AuthBean test() {
  3. System.out.println(&quot;worked&quot;);
  4. return new AuthBean(&quot;You are authenticated already&quot;);
  5. }
  6. @GetMapping(path = &quot;/getUserInfo&quot;)
  7. public User getInfo(@RequestParam(value = &quot;username&quot;) String user) {
  8. return dao.getByUserName(user);
  9. }

The first one is working perfectly. When I try to reach it: spring asks me to log in. I do it and it shows me a message.
But when I'm trying to get to the second one it just keeps asking me for a login. And if I try to go to the first one, which worked perfectly fine it asks me to login again.
Experimentally I've figured out that RequestParam causes this problem.
I understand that the problem may be in the configuration itself, but I can't get It.

Thanks for the response in advance!"

EDIT
Here is full config class:

  1. @Autowired
  2. UserDetailsService userDetailsService;
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http.cors().configurationSource(corsConfigurationSource()).and().csrf().
  6. disable()
  7. .authorizeRequests()
  8. .antMatchers(HttpMethod.OPTIONS, &quot;/**&quot;)
  9. .permitAll()
  10. .anyRequest()
  11. .authenticated()
  12. .and()
  13. .httpBasic();
  14. }
  15. CorsConfigurationSource corsConfigurationSource() {
  16. CorsConfiguration configuration = new CorsConfiguration();
  17. List&lt;String&gt; allowOrigins = Arrays.asList(&quot;*&quot;);
  18. configuration.setAllowedOrigins(allowOrigins);
  19. configuration.setAllowedMethods(singletonList(&quot;*&quot;));
  20. configuration.setAllowedHeaders(singletonList(&quot;*&quot;));
  21. //in case authentication is enabled this flag MUST be set, otherwise CORS requests will fail
  22. configuration.setAllowCredentials(true);
  23. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  24. source.registerCorsConfiguration(&quot;/**&quot;, configuration);
  25. return source;
  26. }
  27. @Autowired
  28. public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  29. auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
  30. }

答案1

得分: 1

你正在告诉你的 Spring Security,在任何请求中检查用户是否已通过身份验证,使用以下代码段:

EDIT

你的代码应该如下所示:

  1. http.authorizeRequests()
  2. .antMatchers("/securityNone").permitAll()
  3. .anyRequest().authenticated()
  4. .and()
  5. .httpBasic()
  6. .authenticationEntryPoint(authenticationEntryPoint);
  7. http.addFilterAfter(new CustomFilter(),
  8. BasicAuthenticationFilter.class);

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

英文:

You are telling to your spring security to check if user is authenticated for any request in .antMatchers(HttpMethod.OPTIONS, "/**").

EDIT

Your code should look like this:

  1. http.authorizeRequests()
  2. .antMatchers(&quot;/securityNone&quot;).permitAll()
  3. .anyRequest().authenticated()
  4. .and()
  5. .httpBasic()
  6. .authenticationEntryPoint(authenticationEntryPoint);
  7. http.addFilterAfter(new CustomFilter(),
  8. BasicAuthenticationFilter.class);

You need to separate the not secured access from the secured one

huangapple
  • 本文由 发表于 2020年4月9日 00:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61105814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定