【发布时间】:2018-11-08 09:55:14
【问题描述】:
如何在 Spring Security 标签中启用 CORS,即在令牌 url 中。我正在使用 spring security 3.1 和 spring mvc 4.3.12。我已成功生成一个令牌以保护 API,但无法在令牌 url 中启用 CORS。
我已使用以下代码生成访问令牌
spring-security.xml
<http pattern="/oauth/token" create-session="stateless"
authentication-manager-ref="clientAuthenticationManager"
xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
<anonymous enabled="false" />
<http-basic entry-point-ref="clientAuthenticationEntryPoint" />
<!-- include this only if you need to authenticate clients via request
parameters -->
<custom-filter ref="clientCredentialsTokenEndpointFilter"
after="BASIC_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
这就是我启用 CORS 但不起作用的方式
dispatcher-servlet.xml
<mvc:cors>
<mvc:mapping path="/oauth/token" allowed-origins="http://localhost:4200"
allowed-methods="GET" allowed-headers="Access-Control-Allow-Origin" />
</mvc:cors>
这是我得到的错误
login:1 加载失败 http://localhost:8080/M.S.-Handloom-Fabrics/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=nishan&password=nishan: 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 使用权。响应的 HTTP 状态代码为 400。
【问题讨论】:
标签: java spring spring-mvc spring-security token