1
我希望在我的REST服务器中有两种请求: 那些拥有“/ freerest /”路径的人可以请求,其他人则需要验证。URL和用户的春季安全配置
这是我的代码:
@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
有什么不对?
感谢您的回答!它适用于我向我的用户请求时,不再返回403.但是,如果我没有基本认证请求,它也返回200。 像,如果我请localhost:8080 /香蕉没有基本认证应该返回401或403 ... – 2015-03-31 19:39:33