【问题标题】:Spring Security configuration on URL and usersURL 和用户的 Spring Security 配置
【发布时间】:2015-06-05 05:36:45
【问题描述】:

我希望 REST 服务器中有两种请求: 路径为“/freerest/”的人任何人都可以请求,其他人则需要身份验证。

这是我的代码:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

  @Autowired
  UserAccountRepository userAccountRepository;

  @Override
  public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService());
  }

  @Bean
  UserDetailsService userDetailsService() {
    return new UserDetailsService() {

      @Override
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        UserAccount account = userAccountRepository.findByEmail(email);
        if(account != null) {
            return new User(account.getEmail(), account.getPassword(), true, true, true, true,
                AuthorityUtils.createAuthorityList("USER"));
        } else {
            throw new UsernameNotFoundException("could not find the user '"
                  + email + "'");
        }
      }

    };
  }
}

@EnableWebSecurity
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {    
    http.authorizeRequests().antMatchers("/freerest/**").permitAll().and().authorizeRequests().anyRequest().hasAnyAuthority("USER");
  }
}

在我看来,在 hasAnyAuthority("USER") 之后,应该有一个 .permitAll()。但不要。

因此,freerest 路径可以正常工作,但如果我尝试使用数据库中的某个用户或默认 Spring 用户,我会得到 403。

怎么了?

【问题讨论】:

    标签: java spring rest spring-security spring-boot


    【解决方案1】:

    试试这个。您在 antMatch 和任何请求之间添加了 and()。我认为这就是问题所在。

    并添加正确的身份验证领域,后跟and(),如下所示。 这里我使用 HTTP Basic Authentication for restful

        @Configuration
        @EnableWebSecurity
        @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
        public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
    
            ......
            ......
            ......
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().disable()
                        .authorizeRequests()
                            .antMatchers("/freerest/**").permitAll()
                            .anyRequest().hasAnyAuthority("USER")
                        .and()
                        .httpBasic();
            }
    
            ......
            ......
            ......
    
        }
    

    【讨论】:

    • 感谢您的回答!它在我向用户请求时起作用,不再返回 403。但如果我请求没有基本身份验证,它也会返回 200。比如,如果我请求没有基本身份验证的 localhost:8080/banana 应该返回 401 或 403...
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 2013-03-09
    • 2018-06-25
    • 2015-11-19
    • 2016-06-30
    • 1970-01-01
    • 2017-06-15
    • 2016-08-04
    相关资源
    最近更新 更多