使用Spring启动实现oauth2客户端凭据流时出现异常

时间:2016-11-05 06:43:32

标签: spring spring-mvc spring-security spring-boot oauth-2.0

我尝试使用spring security oauth2和spring boot实现client_credentials流程。

但是当我尝试使用令牌访问资源方法时,我得到了这个异常。

ERROR [org.springframework.boot.web.support.ErrorPageFilter] (default task-3) Forwarding to error page from request [/error] due to exception [com.fasterxml.jackson.databind.ObjectWriter.forType(Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectWriter;]: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectWriter.forType(Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectWriter;

这是我的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>auth-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

       <!-- Spring Security dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>

        <!-- Java EE dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <finalName>auth-server</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

以下是我的spring安全配置文件。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception{
        clients.inMemory()
                .withClient("123456")
                .secret("123456789")
                .authorizedGrantTypes("client_credentials")
                .scopes("read","write")
                .accessTokenValiditySeconds(3600);
    }


}

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter{

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(HttpMethod.GET,"/api/users/**").access("#oauth2.hasScope('read')")
                .antMatchers("/").access("#oauth2.hasScope('read')");
    }


}

这是我的休息控制器。

@RestController 公共类UserController {

@RequestMapping(value = "/api/users/greeting", method = RequestMethod.GET)
public String greeting(){
    return "Hello Greetings";
}

@RequestMapping("/")
public String helloWorld(){
    return "Hello World";
}

}

我在这里错了什么。我最初的猜测是杰克逊存在依赖冲突。所以我在oauth2依赖中添加了一个包含。

但似乎不会解决这个问题。我仍然得到错误。

有人可以帮我解决这个问题。

最诚挚的问候,

0 个答案:

没有答案