Spring Security登录仍然出现,即使我已经使用了permit all。

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

Spring security login is still coming even after I used permit all

问题

我对Spring Security非常陌生。我正在构建一个具有登录注销功能的项目。我按照教程中的说明进行了配置,但仍然不允许在未登录的情况下直接访问。

大多数我使用的方法都被标记为过时或即将移除。

如果有人能指导我,我将不胜感激。

我还注意到它没有生成默认的用户名和密码供登录使用。

英文:

I am very new to spring security. I am building a project which has login logout feature. I did the configuration as told in the tutorials but is still not allowing to access directly without login.
And most of the methods which i use says that it is marked for
deprecated or marked for removal.
I will be grateful if anyone can guide me

I also noticed that it is not generating the default username and password for login

  1. package com.example.covidsurvey.config;
  2. import org.apache.catalina.security.SecurityConfig;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  7. import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  8. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  10. import org.springframework.security.config.http.SessionCreationPolicy;
  11. import org.springframework.security.core.userdetails.UserDetailsService;
  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  13. import org.springframework.security.web.SecurityFilterChain;
  14. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  15. @Configuration
  16. @EnableWebSecurity
  17. public class MyConfig {
  18. @Bean
  19. public UserDetailsService getUserDetailServices() {
  20. return new UserDetailServiceImpl();
  21. }
  22. @Bean
  23. public BCryptPasswordEncoder passwordEncoder() {
  24. return new BCryptPasswordEncoder();
  25. }
  26. @Bean
  27. public DaoAuthenticationProvider authenticationProvider() {
  28. DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
  29. daoAuthenticationProvider.setUserDetailsService(this.getUserDetailServices());
  30. daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
  31. return daoAuthenticationProvider;
  32. }
  33. @Bean
  34. public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
  35. return authenticationConfiguration.getAuthenticationManager();
  36. }
  37. @Bean
  38. public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
  39. httpSecurity.csrf().disable()
  40. .authorizeHttpRequests()
  41. .requestMatchers("/admin/**").permitAll()
  42. .and()
  43. .authorizeHttpRequests().requestMatchers("/user/**").permitAll()
  44. .anyRequest()
  45. .authenticated()
  46. .and()
  47. .formLogin();
  48. return httpSecurity.build();
  49. }
  50. }

答案1

得分: 0

我不确定您要访问的URL是什么,但我相信.authenticated()正在导致Spring想要对您进行身份验证。

我建议查看此回答,他们很好地解释了如何启用/禁用特定端点的安全性。

希望这有所帮助。

英文:

im not sure what url you want to hit but i believe .authenticated() is causing spring to want to authenticate you

i would say to check out this response, they do a good job explaining how to enable/disable security for specific endpoints

hope this helps

huangapple
  • 本文由 发表于 2023年6月16日 11:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486788.html
匿名

发表评论

匿名网友

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

确定