【发布时间】:2018-09-07 03:36:33
【问题描述】:
我无法从另一个 Origin 访问受保护的资源。找了几天没找到,所以在这里发帖提问。
故事是这样的:
我创建了第一个在默认端口 8080 上运行的 Spring Boot 应用程序。
它依赖于 spring-boot-starter-data-rest 和其他依赖,它有一个 GreetingRepository:
public interface GreetingRepository extends JpaRepository<Greeting, Long>, JpaSpecificationExecutor<Greeting> {}
使用 RepositoryRestConfigurerAdapter 全局启用 CORS:
@Configuration
public class GlobalRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.getCorsRegistry()
.addMapping("/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowedMethods("*");
}
}
我创建了在端口 9000 上运行的第二个 Spring Boot 应用程序,它将访问此 Greetings 资源。
而且它有效。第二个应用程序使用 GET 方法向http://localhost:8080/api/greetings 发送 HTTP 请求,并获得带有 Json 数据的响应,HEADER Access-Control-Allow-Origin: *.一切都很好。
但是。
然后我想在第一个应用程序中保护我的资源。在那里我包含了 spring-boot-starter-security 依赖项并在 WebSecurityConfigurerAdapter 中进行了配置:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService myUserDetailsService;
@Autowired
PasswordEncoder myPasswordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.httpBasic()
.and()
.csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailsService).passwordEncoder(myPasswordEncoder);
}
@Bean
public UserDetailsService createBeanUserDetailService() {
return new MyUserDetailsService();
}
@Bean
public PasswordEncoder createBeanPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
并制作了 UserDetailsService 等等。 (重要提示:我在添加 CORS 之前测试了安全性,因此此安全配置有效,这不是问题。
然后,在第一个应用程序中添加安全性后,第二个应用程序使用 GET 方法向http://localhost:8080/api/greetings 发送与第一次相同的 HTTP 请求。
我找不到这个问题的解决方案。因此 CORS 适用于 Spring Repository 资源,而 Spring Security 适用,但由于 /login 页面,我无法从另一个 Origin 访问安全资源。如何解决?
【问题讨论】:
-
你没有登录,所以你被重定向到你的登录页面。
-
如果您的 REST 调用未通过身份验证,您真的想查看登录页面吗?
-
@dur 我知道我为什么会被重定向。我想构建可以从另一个 Origin(其他网站和移动应用程序)访问的 API。例如,如何使用 Ajax 验证来自另一个 Origin 的请求?我使用基本身份验证。
-
您的第一个问题是,您的请求未通过身份验证。您确定您的请求包含基本身份验证吗?用户名和密码是否正确?显示带有标头的请求。在解决了第一个问题之后,您还必须解决 CORS 问题。
-
现在您解决了 CORS 问题。见stackoverflow.com/a/42021652/5277820
标签: rest spring-boot spring-security cors spring-data-rest