如何让Spring oauth2接受内容类型的application / json

时间:2015-07-29 11:12:17

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

我正在开发一个相当简单的spring boot API,它将由oauth2保护。所有端点都能正常接受applicaiton / json请求,但默认的spring / oauth / *端点除外,它们似乎需要请求内容类型的application / x-www-form-urlencoded。

有没有办法覆盖这种行为,还是我需要在我的Spring启动应用程序中包含一个库来处理映射?我的pom.xml包含以下内容

<artifactId>oauth2-authserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>OAuth2 Auth Server</name>
<description>Authorization server for an OAuth2 controlled application</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>${spring-security-oauth2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-jwt</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>0.9.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <wro4j.version>1.7.6</wro4j.version>
    <java.version>1.7</java.version>
    <spring-security-oauth2.version>2.0.7.RELEASE</spring-security-oauth2.version>
</properties>

1 个答案:

答案 0 :(得分:2)

来自OAuth2规范

  

授权请求

     

客户端通过添加以下内容来构造请求URI      授权端点URI的查询组件的参数      使用“application / x-www-form-urlencoded”

  

访问令牌请求

     

客户端通过添加
向令牌端点发出请求   以下参数使用“application / x-www-form-urlencoded”格式

每个请求都应使用application / x-www-form-urlencoded