春季安全配置未生效

huangapple 未分类评论53阅读模式
标题翻译

Spring Security Configuration is not applied

问题

我有以下配置,需要为 /api/v1/** 端点配置 HTTPBasic 认证,并且我想为 /users/ 的 URL 模式配置 form 认证。当我按照以下配置运行时,Web 请求的配置正常工作,但 API 的配置却没有生效。没有应用任何安全性。我哪里做错了?

  1. @Configuration
  2. @EnableWebSecurity
  3. public class WebSecurityConfig {
  4. @Order(1)
  5. @Configuration
  6. public static class MVCSecurityConfiguration extends WebSecurityConfigurerAdapter {
  7. @Bean
  8. public BCryptPasswordEncoder getBCryptPasswordEncoder() {
  9. return new BCryptPasswordEncoder();
  10. }
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. http
  14. .antMatcher("/users/**")
  15. .csrf()
  16. .and()
  17. .authorizeRequests()
  18. .antMatchers(
  19. "/resources/**", "/users/register", "/users/signup", "/users/confirm",
  20. "/users/user-action", "/users/reset-password", "/confirm", "/webjars/**"
  21. )
  22. .permitAll()
  23. .antMatchers("/users/**")
  24. .hasRole("USER")
  25. .anyRequest()
  26. .authenticated()
  27. .and()
  28. .formLogin()
  29. .loginPage("/login")
  30. .usernameParameter("username")
  31. .passwordParameter("password");
  32. http
  33. .authorizeRequests()
  34. .antMatchers("/api/v1/users/**")
  35. .hasRole("USER")
  36. .anyRequest()
  37. .authenticated()
  38. .and()
  39. .httpBasic();
  40. }
  41. }
  42. }
英文翻译

I have the below configuration where i need to configure HTTPBasic authentication for /api/v1/** endpoints and i want to configure form authentication for /users/ url pattern. When i run with the below configuration, the configuration for web requests is working correctly but the configuration for API is not working. No security is being applied. Where am I going wrong?

  1. @Configuration
  2. @EnableWebSecurity
  3. public class WebSecurityConfig {
  4. @Order(1)
  5. @Configuration
  6. public static class MVCSecurityConfiguration extends WebSecurityConfigurerAdapter {
  7. @Bean
  8. public BCryptPasswordEncoder getBCryptPasswordEncoder() {
  9. return new BCryptPasswordEncoder();
  10. }
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. http.
  14. antMatcher("/users/**")
  15. .csrf()
  16. .and()
  17. .authorizeRequests()
  18. .antMatchers(
  19. "/resources/**", "/users/register", "/users/signup", "/users/confirm", "/users/user-action", "/users/reset-password", "/confirm", "/webjars/**")
  20. .permitAll()
  21. .antMatchers("/users/**")
  22. .hasRole("USER")
  23. .anyRequest()
  24. .authenticated()
  25. .and()
  26. .formLogin().loginPage("/login").usernameParameter("username").passwordParameter("password");
  27. http
  28. .authorizeRequests()
  29. .antMatchers("/api/v1/users/**")
  30. .hasRole("USER")
  31. .anyRequest()
  32. .authenticated()
  33. .and()
  34. .httpBasic();
  35. }
  36. }

答案1

得分: 3

我已经将您的代码与下面的配置一起使用:

  1. @EnableWebSecurity
  2. public class SecurityConfiguration {
  3. public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Override
  5. protected void configure(HttpSecurity http) throws Exception {
  6. http.antMatcher("/api/v1/users/**")
  7. .authorizeRequests().anyRequest()
  8. .hasRole("USER").and().httpBasic();
  9. }
  10. }
  11. @Configuration
  12. @Order(2)
  13. public class MVCSecurityConfiguration extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http.csrf().and().authorizeRequests()
  17. .antMatchers("/resources/**", "/users/register", "/users/signup", "/users/confirm",
  18. "/users/user-action", "/users/reset-password", "/confirm", "/webjars/**").permitAll()
  19. .antMatchers("/users/**").hasRole("USER")
  20. .and()
  21. .formLogin().usernameParameter("username").passwordParameter("password");
  22. }
  23. }
  24. }

查看关于Spring Security的文档以及示例代码此处

英文翻译

I have put your code to work with this configuration bellow:

  1. @EnableWebSecurity
  2. public class SecurityConfiguration {
  3. public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Override
  5. protected void configure(HttpSecurity http) throws Exception {
  6. http.antMatcher("/api/v1/users/**")
  7. .authorizeRequests().anyRequest()
  8. .hasRole("USER").and().httpBasic();
  9. }
  10. }
  11. @Configuration
  12. @Order(2)
  13. public class MVCSecurityConfiguration extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http.csrf().and().authorizeRequests()
  17. .antMatchers("/resources/**", "/users/register", "/users/signup", "/users/confirm",
  18. "/users/user-action", "/users/reset-password", "/confirm", "/webjars/**").permitAll()
  19. .antMatchers("/users/**").hasRole("USER")
  20. .and()
  21. .formLogin().usernameParameter("username").passwordParameter("password");
  22. }
  23. }
  24. }

View docs for Spring Security and sample code here.

huangapple
  • 本文由 发表于 2020年5月30日 20:37:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/62102534.html
匿名

发表评论

匿名网友

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

确定