【发布时间】:2015-04-04 07:47:59
【问题描述】:
我正在使用 Spring 安全性和 Oauth2。但是我是 Spring Oauth2 的新手,当前端访问资源时出现 CORS 错误。
我正在使用以下过滤器来允许其他域访问该资源:
@Component
@Order(Integer.MAX_VALUE)
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Credentials", "True");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
我编写了以下代码以允许在我的 SecurityConfiguration.java 中使用公共资源。
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers("/social/facebook/**","/register/**","/public/**").permitAll().and()
.authorizeRequests().antMatchers("/user/**").hasRole("USER").and()
.exceptionHandling()
.accessDeniedPage("/login.jsp?authorization_error=true")
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize")).disable();
}
对于 Oauth2,以下代码用于保护我的 OAuth2ServerConfig.java 中的用户资源。
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatchers().antMatchers("/user/**")
.and()
.authorizeRequests()
.antMatchers("/user/**").access("#oauth2.hasScope('read')")
.regexMatchers(HttpMethod.DELETE, "/oauth/users/([^/].*?)/tokens/.*")
.access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('write')")
.regexMatchers(HttpMethod.GET, "/oauth/clients/([^/].*?)/users/.*")
.access("#oauth2.clientHasRole('ROLE_CLIENT') and (hasRole('ROLE_USER') or #oauth2.isClient()) and #oauth2.hasScope('read')")
.regexMatchers(HttpMethod.GET, "/oauth/clients/.*")
.access("#oauth2.clientHasRole('ROLE_CLIENT') and #oauth2.isClient() and #oauth2.hasScope('read')");
}
当我在浏览器中打开 index.html 文件时,如下所示:(对不起,我没有至少 10 个声望来发布图片,所以我在这里粘贴链接)
http://i.stack.imgur.com/yQKJM.png
成功获取公共数据,即允许其他域访问“/public/**”数据。
但它无法获取“/user/**”数据(受 Oauth2 保护)。它给了我下面的错误说“跨源请求被阻止”。
http://i.stack.imgur.com/XIVx1.png
当我将前端文件移动到 Spring 服务器的同一个域时。它可以很好地获取“公共”和“用户”数据,如下所示:
http://i.stack.imgur.com/Q2n7F.png
前端和后端应该分开。但是 CORS 被阻止访问预计数据。谁能给我一些建议?非常感谢。我猜过滤器在 Oauth2 上不起作用?仍然花费大量时间寻找解决方案。
【问题讨论】:
-
你可以在这里参考我的答案,stackoverflow.com/questions/40418441/…
标签: spring spring-mvc oauth spring-security cors