多用户登录对普通用户和管理员用户都不起作用。

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

Multi login for normal user and admin user not working

问题

这是您的代码的翻译:

  1. 我使用两种配置一种用于普通用户登录页面另一种用于管理员用户登录页面
  2. 这是我的普通用户配置
  3. @Configuration
  4. @EnableWebSecurity
  5. @Order(2)
  6. public class Config {
  7. @Bean
  8. public UserDetailsService getUserDetailsService() {
  9. return new UserDetailsServiceImpl();
  10. }
  11. @Bean
  12. public BCryptPasswordEncoder passwordEncoder() {
  13. return new BCryptPasswordEncoder();
  14. }
  15. @Bean
  16. public DaoAuthenticationProvider authenticationProvider() {
  17. DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
  18. daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
  19. daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
  20. return daoAuthenticationProvider;
  21. }
  22. // 配置方法
  23. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  24. auth.authenticationProvider(authenticationProvider());
  25. }
  26. @Bean
  27. protected SecurityFilterChain filterChain1(HttpSecurity http) throws Exception {
  28. http
  29. .authorizeHttpRequests()
  30. .requestMatchers("/registration/**", "/signup", "/user/plandetails").permitAll()
  31. .requestMatchers("/registration", "/user/plandetails").permitAll()
  32. .requestMatchers("/registration", "/user/checkout").permitAll()
  33. .requestMatchers("/registration/", "/signup", "/user/plandetails").permitAll()
  34. .requestMatchers("/signup/**", "/signup", "/user/plandetails").permitAll()
  35. .requestMatchers("/signup", "/signup", "/user/plandetails").permitAll()
  36. .requestMatchers("/signup", "/registration", "/user/plandetails").permitAll()
  37. .requestMatchers("/user/**").hasRole("USER")
  38. .requestMatchers("/**").permitAll()
  39. .and()
  40. .formLogin()
  41. .loginPage("/signin")
  42. .loginProcessingUrl("/dologin")
  43. .defaultSuccessUrl("/user/dashboard")
  44. .and()
  45. .csrf().disable()
  46. .authorizeHttpRequests()
  47. .anyRequest().fullyAuthenticated()
  48. .and()
  49. .rememberMe();
  50. return http.build();
  51. }
  52. }
  53. 这是我的管理员用户配置
  54. @Configuration
  55. @Order(1)
  56. public class AdminConfig {
  57. @Bean
  58. protected SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
  59. http
  60. .authorizeHttpRequests()
  61. .requestMatchers("/**").permitAll();
  62. http
  63. .authorizeHttpRequests()
  64. .requestMatchers("/admin/**").authenticated()
  65. .anyRequest().hasRole("ADMIN")
  66. .and()
  67. .formLogin()
  68. .loginPage("/admin/login")
  69. .loginProcessingUrl("/admin/doAdminLogin")
  70. .defaultSuccessUrl("/admin/adminDashboard")
  71. .permitAll()
  72. .and()
  73. .logout()
  74. .logoutUrl("/admin/logout")
  75. .logoutSuccessUrl("/");
  76. http
  77. .csrf().disable();
  78. return http.build();
  79. }
  80. }
  81. 我无法使用普通用户或管理员用户登录当我输入密码时它将重定向到`http://localhost:8080/dologin`,用户无法登录。这是错误截图https://i.stack.imgur.com/tD7oE.png,但是当我注释掉管理员配置页面的代码时,它开始工作。
  82. 根据我读到的某些地方我需要为Spring Security分别保留普通用户和管理员用户登录的配置文件
  83. 所以请帮助解决这个问题
  84. <details>
  85. <summary>英文:</summary>
  86. I am using two configuration, one for normal user login page and another fpr admin user login page.
  87. This configuration is for my normal user:
  88. &lt;!-- language: java --&gt;
  89. @Configuration
  90. @EnableWebSecurity
  91. @Order(2)
  92. public class Config {
  93. @Bean
  94. public UserDetailsService getUserDetailsService() {
  95. return new UserDetailsServiceImpl();
  96. }
  97. @Bean
  98. public BCryptPasswordEncoder passwordEncoder() {
  99. return new BCryptPasswordEncoder();
  100. }
  101. @Bean
  102. public DaoAuthenticationProvider authenticationProvider() {
  103. DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
  104. daoAuthenticationProvider.setUserDetailsService(this.getUserDetailsService());
  105. daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
  106. return daoAuthenticationProvider;
  107. }
  108. // configure method
  109. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  110. auth.authenticationProvider(authenticationProvider());
  111. }
  112. @Bean
  113. protected SecurityFilterChain filterChain1(HttpSecurity http) throws Exception {
  114. http
  115. .authorizeHttpRequests()
  116. .requestMatchers(&quot;/registration/**&quot;, &quot;/signup&quot;, &quot;/user/plandetails&quot;).permitAll()
  117. .requestMatchers(&quot;/registration&quot;, &quot;/user/plandetails&quot;).permitAll()
  118. .requestMatchers(&quot;/registration&quot;, &quot;/user/checkout&quot;).permitAll()
  119. .requestMatchers(&quot;/registration/&quot;, &quot;/signup&quot;, &quot;/user/plandetails&quot;).permitAll()
  120. .requestMatchers(&quot;/signup/**&quot;, &quot;/signup&quot;, &quot;/user/plandetails&quot;).permitAll()
  121. .requestMatchers(&quot;/signup&quot;, &quot;/signup&quot;, &quot;/user/plandetails&quot;).permitAll()
  122. .requestMatchers(&quot;/signup&quot;, &quot;/registration&quot;, &quot;/user/plandetails&quot;).permitAll()
  123. .requestMatchers(&quot;/user/**&quot;).hasRole(&quot;USER&quot;)
  124. .requestMatchers(&quot;/**&quot;).permitAll()
  125. .and()
  126. .formLogin()
  127. .loginPage(&quot;/signin&quot;)
  128. .loginProcessingUrl(&quot;/dologin&quot;)
  129. .defaultSuccessUrl(&quot;/user/dashboard&quot;)
  130. .and()
  131. .csrf().disable()
  132. .authorizeHttpRequests()
  133. .anyRequest().fullyAuthenticated()
  134. .and()
  135. .rememberMe();
  136. return http.build();
  137. }
  138. And this is my admin user configuration:
  139. &lt;!-- language: java --&gt;
  140. @Configuration
  141. @Order(1)
  142. public class AdminConfig{
  143. @Bean
  144. protected SecurityFilterChain filterChain2(HttpSecurity http) throws Exception {
  145. http
  146. .authorizeHttpRequests()
  147. .requestMatchers(&quot;/**&quot;).permitAll();
  148. http
  149. .authorizeHttpRequests()
  150. .requestMatchers(&quot;/admin/**&quot;).authenticated()
  151. .anyRequest().hasRole(&quot;ADMIN&quot;)
  152. .and()
  153. .formLogin()
  154. .loginPage(&quot;/admin/login&quot;)
  155. .loginProcessingUrl(&quot;/admin/doAdminLogin&quot;)
  156. .defaultSuccessUrl(&quot;/admin/adminDashboard&quot;)
  157. .permitAll()
  158. .and()
  159. .logout()
  160. .logoutUrl(&quot;/admin/logout&quot;)
  161. .logoutSuccessUrl(&quot;/&quot;);
  162. http
  163. .csrf().disable();
  164. return http.build();
  165. }
  166. }
  167. I am not able to login with normal as well as with admin user. When I am entering password, it is redirecting me to `http://localhost:8080/dologin` and user is not getting logged in. This is the error screenshot https://i.stack.imgur.com/tD7oE.png but when I am commenting the admin configuration page code, it start to work.
  168. As I have read somewhere and I need to keep configuration file for Spring Security separately for normal user and admin user login.
  169. So, please help in solving this issue.
  170. </details>
  171. # 答案1
  172. **得分**: 0
  173. - 您的控制器没有映射到 `/` API。`404` 表示该URL没有映射到任何API请检查您的控制器然后转到相应的URL登录应该会出现相应的页面
  174. - 此外,`HttpSecurity` 类似乎已实现了建造者模式
  175. - 原始代码
  176. ```
  177. ...
  178. http.authorizeHttpRequests()
  179. .requestMatchers(&quot;/**&quot;).permitAll();
  180. http.authorizeHttpRequests()
  181. .requestMatchers(&quot;/admin/**&quot;).authenticated().anyRequest()
  182. ...
  183. ```
  184. - 您应该这样做
  185. ```
  186. ...
  187. http.authorizeHttpRequests()
  188. .requestMatchers(&quot;/**&quot;).permitAll()
  189. .and().authorizeHttpRequests()
  190. .requestMatchers(&quot;/admin/**&quot;)
  191. .authenticated().anyRequest()
  192. ...
  193. ```
  194. 您可以参考此[答案](https://stackoverflow.com/a/75516761/9462050)。
  195. <details>
  196. <summary>英文:</summary>
  197. * Your controller does not have a mapping for `/` API. `404` means that there is no API mapped for that URL. Check your controller and then navigate to the respective URL, then log in, and the respective page should appear.
  198. * Also, `HttpSecurity` class seems to have implemented a builder pattern.
  199. Instead of:

...
http.authorizeHttpRequests()
.requestMatchers("/**").permitAll();

  1. http.authorizeHttpRequests()
  2. .requestMatchers(&quot;/admin/**&quot;).authenticated().anyRequest()

...

  1. You should do something like :

...
http.authorizeHttpRequests()
.requestMatchers("/").permitAll()
.and().authorizeHttpRequests()
.requestMatchers("/admin/
")
.authenticated().anyRequest()
...

  1. You may refer to this [answer](https://stackoverflow.com/a/75516761/9462050)
  2. </details>

huangapple
  • 本文由 发表于 2023年2月24日 01:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548238.html
匿名

发表评论

匿名网友

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

确定