我使用Spring Boot和Spring Security以及Cors支持.
如果我执行以下代码
url = 'http://localhost:5000/api/token'
xmlhttp = new XMLHttpRequest
xmlhttp.onreadystatechange = ->
if xmlhttp.readyState is 4
console.log xmlhttp.status
xmlhttp.open "GET", url, true
# xmlhttp.setRequestHeader "X-Requested-With", "XMLHttpRequest"
xmlhttp.setRequestHeader 'Authorization', 'Basic ' + btoa 'a:a'
do xmlhttp.send
Run Code Online (Sandbox Code Playgroud)
我得到了结果
200
Run Code Online (Sandbox Code Playgroud)
如果我测试错误的凭据,如
url = 'http://localhost:5000/api/token'
xmlhttp = new XMLHttpRequest
xmlhttp.onreadystatechange = ->
if xmlhttp.readyState is 4
console.log xmlhttp.status
xmlhttp.open "GET", url, true
# xmlhttp.setRequestHeader "X-Requested-With", "XMLHttpRequest"
xmlhttp.setRequestHeader 'Authorization', 'Basic ' + btoa 'a:aa'
do xmlhttp.send
Run Code Online (Sandbox Code Playgroud)
而不是获得401(这是春季安全中错误身份验证的标准代码),我得到了
0
Run Code Online (Sandbox Code Playgroud)
以下浏览器通知:
获取http:// localhost:5000/api/token
XMLHttpRequest无法加载 …
我正在尝试从我的Webstorm应用程序向我的后端应用程序发送请求,这两个应用程序都位于不同的端口,我正在使用前端的angularJS和后端的java.我已经阅读了一些关于CORS过滤器的内容,并且我了解到为了执行跨源请求,我需要实现这些.然而,在做完这个我的错误后,正在
Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63343' is therefore not allowed access. http://localhost:8080/register?password=&username=
XMLHttpRequest cannot load http://localhost:8080/register?password=&username=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63343' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
没有改变导致我相信我做错了什么,这是我发送请求的代码:
var charmanderServices = angular.module('charmanderServices', ['ngResource']);
var hostAdress = "http://localhost:8080";
charmanderServices.factory("register", ["$resource",
function($resource){
console.log('in service');
return $resource(hostAdress + "/register", {}, {
'registerUser' : { method: 'POST', isArray: false,
params: {
username: '@username',
password: '@password'
}
},
headers : …Run Code Online (Sandbox Code Playgroud) 所以我从Dave Syer的这个例子中得到了以下授权服务器
@SpringBootApplication
public class AuthserverApplication {
public static void main(String[] args) {
SpringApplication.run(AuthserverApplication.class, args);
}
/* added later
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
protected static class MyWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http //.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
}*/
@Configuration
@EnableAuthorizationServer
protected static class OAuth2AuthorizationConfig extends
AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
KeyPair keyPair = new KeyStoreKeyFactory(
new ClassPathResource("keystore.jks"), "foobar".toCharArray())
.getKeyPair("test");
converter.setKeyPair(keyPair);
return converter; …Run Code Online (Sandbox Code Playgroud) 我正在使用keycloak来确保我的休息服务.我正在参考这里给出的教程.我创造了休息和前端.现在当我在后端添加keycloak时,当我的前端进行api调用时,我收到CORS错误.
Spring启动时的Application.java文件看起来像
@SpringBootApplication
public class Application
{
public static void main( String[] args )
{
SpringApplication.run(Application.class, args);
}
@Bean
public WebMvcConfigurer corsConfiguration() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/*")
.allowedMethods(HttpMethod.GET.toString(), HttpMethod.POST.toString(),
HttpMethod.PUT.toString(), HttpMethod.DELETE.toString(), HttpMethod.OPTIONS.toString())
.allowedOrigins("*");
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
application.properties文件中的keycloak属性如下所示
keycloak.realm = demo
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.ssl-required = external
keycloak.resource = tutorial-backend
keycloak.bearer-only = true
keycloak.credentials.secret = 123123-1231231-123123-1231
keycloak.cors = true
keycloak.securityConstraints[0].securityCollections[0].name = spring secured api
keycloak.securityConstraints[0].securityCollections[0].authRoles[0] = admin
keycloak.securityConstraints[0].securityCollections[0].authRoles[1] = user
keycloak.securityConstraints[0].securityCollections[0].patterns[0] …Run Code Online (Sandbox Code Playgroud) 我想允许一个域的跨域请求.我的项目使用Spring,所以我想利用新的CORS支持.
我正在使用4.2.0所有springframework依赖项的版本.
我按照这里的示例https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#disqus_thread并尝试了第一个版本.我的控制器注释看起来像:
@CrossOrigin(origins = "http://fiddle.jshell.net/", maxAge = 3600)
@Controller
@RequestMapping("/rest")
public class MyController
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,mvc-config是另一种方法.我也试过了:
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="http://fiddle.jshell.net/, http://domain2.com"
allowed-methods="GET, PUT"
allowed-headers="header1, header2, header3"
exposed-headers="header1, header2" allow-credentials="false"
max-age="123" />
</mvc:cors>
Run Code Online (Sandbox Code Playgroud)
使用任何一种方法,Response似乎都不包含任何类似的东西Access-Control-Allow-Origin,也不能通过jsfiddle的简单查询得到结果.
Chrome开发人员工具的标题信息在运行时从localhost访问,如下所示.在这种情况下,请求来自同一个域,而不是通过javascript,但我认为CORS注释会添加访问控制参数吗?
响应标题:
Content-Length:174869
Content-Type:text/html;charset=UTF-8
Date:Fri, 21 Aug 2015 12:21:09 GMT
Server:Apache-Coyote/1.1
Run Code Online (Sandbox Code Playgroud)
请求标题:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*\/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ro;q=0.6,de;q=0.4,fr;q=0.2
Cache-Control:no-cache
Connection:keep-alive
Cookie:JSESSIONID=831EBC138D2B7E176DF4945ADA05CAC1;_ga=GA1.1.1046500342.1404228238; undefined=0
Host:localhost:8080
Pragma:no-cache
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0(Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36
Run Code Online (Sandbox Code Playgroud)
我不使用弹簧靴,我认为我错过了一个配置步骤.
最近我在Spring 4中尝试了新的内置CORS-Support.这个功能很棒,我想在Spring Boot/AngularJS应用程序中实现它.
所有请求都正常但我无法注销我的用户,因为
OPTIONS-Request to/logout由Spring Security处理.
是否有可能处理OPTIONS之前-请求的Spring Security或者我应该附上CORS报头中LogoutSuccessHandler?
我有一个使用Angular 2和Spring Boot开发的Web应用程序.我使用spring-boot-data-rest依赖项将我的存储库公开为HTTP端点.
在开发过程中,我在端口8080上运行的本地tomcat上运行我的后端spring启动项目.为了开发前端,我使用angular-cli在端口4200上运行我的Angular 2应用程序.我在4200上运行的前端需要能够点击8080上暴露的端点,但这不起作用,因为:
请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:4200 '访问.
如果这些是我手动输入的自定义端点@RestController,我可以简单地添加@CrossOrigin注释:
@RestController
public class MyController {
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping(value = "/whatever")
public void whatever() {
//whatever
}
}
Run Code Online (Sandbox Code Playgroud)
但我显然不能为我的端点暴露出来spring-boot-data-rest.那么,我如何才能从http://localhost:4200原点访问这些端点?
我正在运行一个 jHipster 实例,在服务器上启用了 oAuth 身份验证和 CORS。我添加了以下bean:
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.setAllowedMethods(Arrays.asList(new String[]{"GET", "PUT", "POST", "DELETE", "OPTIONS"}));
source.registerCorsConfiguration("/api/**", config);
source.registerCorsConfiguration("/v2/api-docs", config);
source.registerCorsConfiguration("/oauth/**", config);
return new CorsFilter(source);
}
Run Code Online (Sandbox Code Playgroud)
并将 .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll() 添加到 ResourceServerConfiguration 配置。
当我尝试从在浏览器本地运行的应用程序对用户(使用在服务器上运行的 jHipster)进行身份验证时,我得到:请求方法:选项 - 状态代码:401 未经授权
似乎没有正确配置 CORS 来处理飞行前身份验证 POST 请求。
我尝试实施Spring Data Rest and Cors和Spring Data Rest and Cors提出的一些解决方案,但无济于事。
这是否可以在 jHipster 中完成以启用身份验证以从浏览器或应用程序(不在 jhipster 服务器上运行)工作?
cors ×6
spring ×6
java ×4
angularjs ×2
spring-boot ×2
spring-mvc ×2
firebase ×1
javascript ×1
jhipster ×1
jwt ×1
keycloak ×1
oauth ×1
rest ×1
spring-web ×1
tomcat ×1