有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java UnsupportedSignatureMethodException:tomcat和jerseyoauth中的HMACSHA1异常

这是我的pom

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mkyong.common</groupId>
        <artifactId>RESTfulExample</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>RESTfulExample Maven Webapp</name>
        <url>http://maven.apache.org</url>

        <repositories>
            <repository>
                <id>maven2-repository.java.net</id>
                <name>Java.net Repository for Maven</name>
                <url>http://download.java.net/maven/2/</url>
                <layout>default</layout>
            </repository>

        </repositories>
        <properties>
            <project.build.java.target>1.6</project.build.java.target>
        </properties>

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.2</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-server</artifactId>
                <version>1.8</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>4.3.0.Final</version>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.28</version>
            </dependency>




            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.6.5</version>
            </dependency>

            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>1.9.5</version>
            </dependency>



     <dependency>
        <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
        <artifactId>oauth-server</artifactId>
        <version>1.18</version>
    </dependency>
                <dependency>
        <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
        <artifactId>oauth-signature</artifactId>
        <version>1.18</version>
    </dependency> 

          <dependency>
        <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
        <artifactId>oauth-client</artifactId>
        <version>1.18</version>
    </dependency>


        </dependencies>

        <build>
            <finalName>RESTfulExample</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.5</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.3.1</version>
                    <configuration>
                        <complianceLevel>1.6</complianceLevel>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <!--<goal>test-compile</goal> -->
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
  </project>

这是我的誓言过滤类

public class OAuthAuthenticationFilter1 implements ContainerRequestFilter {
    public ContainerRequest filter(ContainerRequest containerRequest) {
        OAuthServerRequest request = new OAuthServerRequest(containerRequest);
        OAuthParameters params = new OAuthParameters();
        params.readRequest(request);
        OAuthSecrets secrets = new OAuthSecrets();
        OAuthParameters param=new OAuthParameters();
        secrets.consumerSecret("OwnAccount");
        param.consumerKey("OwnAccount");
        try {
            if(!OAuthSignature.verify(request, params, secrets)) {
                throw new WebApplicationException(401);
            }
        } catch (OAuthSignatureException e) {
            throw new WebApplicationException(e, 401);
        }
        return containerRequest;
    }
}

这是我的客户

public class Sample1 {
    public static final String HOSTNAME = "http://localhost:8080/RESTfulExample/rest/hello/planetext";
    public static final String CONSUMER_KEY = "OwnAccount"; 
    public static final String CONSUMER_SECRET = "OwnAccount";


    /**
     * @param args
     */
    public static void main(String[] args) {
        Client client = Client.create();
        OAuthParameters params = new OAuthParameters().signatureMethod("HMAC-SHA1").consumerKey(CONSUMER_KEY);
        OAuthSecrets secrets = new OAuthSecrets().consumerSecret(CONSUMER_SECRET);
        OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
        WebResource res = client.resource(HOSTNAME );
        res.addFilter(filter);
          String responseString = res.get(String.class);
        System.out.println(responseString);
    }

}

我使用以下誓言和泽西pom指的是该网站 http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.sun.jersey.contribs.jersey-oauth%22

<dependency>
    <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
    <artifactId>oauth-server</artifactId>
    <version>1.18</version>
</dependency>
            <dependency>
    <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
    <artifactId>oauth-signature</artifactId>
    <version>1.18</version>
</dependency> 

      <dependency>
    <groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
    <artifactId>oauth-client</artifactId>
    <version>1.18</version>
</dependency>

当调用过滤器类上的以下代码时

try {
      // exception is thrown from here 

            if(!OAuthSignature.verify(request, params, secrets)) {
                throw new WebApplicationException(401);
            }
        } catch (OAuthSignatureException e) {
            throw new WebApplicationException(e, 401);
        }

我得到了这个例外

com。太阳运动衫非统组织。签名不受支持的SignatureMethodException:HMAC-SHA1 在com。太阳运动衫非统组织。签名OAuthSignature。getSignatureMethod(OAuthSignature.java:257)

我正在使用Tomcat7

我在网上搜索,但找不到tomcat的原因。我错过了什么?有人为tomcat面对过吗


共 (1) 个答案

  1. # 1 楼答案

    当我将jersey server jar更新为1.18时,我解决了这个问题。我以前使用的是jersey server 1.8 jar