【问题标题】:Enable CORS in access token url - SPRING在访问令牌 url 中启用 CORS - SPRING
【发布时间】:2018-08-04 22:49:30
【问题描述】:

我正在做一个简单的弹簧安全项目。我使用 Angular 作为前端。对于身份验证,我从后端调用访问令牌,但春季没有任何 CORS 启用。如何启用 CORS。这是获取访问令牌的代码

<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>

【问题讨论】:

    标签: spring-mvc spring-security cors spring-security-oauth2


    【解决方案1】:

    抱歉,我不知道如何通过 XML 来实现,但在我的基于注释的配置中,CORS 是这样启用的:

    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;    
    
    @Configuration
    public class WebApiConfiguration extends WebMvcConfigurationSupport {
        @Override
        protected void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/api/**")
                    .allowedMethods(
                            HttpMethod.GET.name(),
                            HttpMethod.POST.name(),
                            HttpMethod.PUT.name(),
                            HttpMethod.PATCH.name(),
                            HttpMethod.HEAD.name(),
                            HttpMethod.DELETE.name()
                   );
        }
    }
    

    【讨论】:

      【解决方案2】:

      启用 CORS 的 XML 配置是,

       <?xml version="1.0" encoding="UTF-8"?>
       <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans       
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/mvc  
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
        <mvc:annotation-driven />
      
        <mvc:cors>
      
          <mvc:mapping path="/api/**" allowed-origins="http://domain1.com, http://domain2.com"
                       allowed-methods="GET, PUT" allowed-headers="header1, header2, header3"
                       exposed-headers="header1, header2" allow-credentials="false" max-age="123" />
      
          <mvc:mapping path="/resources/**" allowed-origins="http://domain1.com" />
      
        </mvc:cors>
      
       </beans> 
      

      【讨论】:

      • allowed-header里面加入header1,header2,header3的目的是什么
      • 在这种情况下这将是可选的,只需添加 allowed-origins 和 allowed-methods 参数
      • 这对我有用,但我从中获取访问令牌的一个 url 除外。我做了一个 xml 配置来获取访问令牌,获取访问令牌的 url 是http://localhost:8080/life-style/oauth/token?grant_type=password&amp;client_id=restapp&amp;client_secret=restapp&amp;username=nishandhungana41sdfgh&amp;password=nishandhungana 我如何在该 URL 上启用 CORS 我尝试了所有可能的解决方案但不能..请帮帮我
      • 尝试使用 allowed-headers="Access-Control-Request-Headers"
      • 或者您可以切换到使用 Spring 的 CorsConfigurationSource。有关详细信息,请参阅 docs.spring.io/spring-security/site/docs/4.2.x/reference/…
      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 2020-04-09
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2018-04-26
      • 1970-01-01
      相关资源
      最近更新 更多