问题描述
求restEasyoauth报错。WEB.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>WebApplication</display-name><!--RESTEasy监听器--><listener><listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class></listener><!--整合Spring--><listener><listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-*.xml</param-value></context-param><!--Spring懒加载--><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--OAuth认证--><servlet><servlet-name>OAuth</servlet-name><servlet-class>org.jboss.resteasy.auth.oauth.OAuthServlet</servlet-class></servlet><servlet-mapping><servlet-name>OAuth</servlet-name><url-pattern>/oauth/*</url-pattern></servlet-mapping><!--OAuth过滤器TheOAuthFilterhandlesauthenticationforprotectedresources--><filter><filter-name>OAuthFilter</filter-name><filter-class>org.jboss.resteasy.auth.oauth.OAuthFilter</filter-class></filter><filter-mapping><filter-name>OAuthFilter</filter-name><url-pattern>/api/*</url-pattern></filter-mapping><context-param><param-name>oauth.provider.provider-class</param-name><param-value>com.iovereye.webservice.oauth.OAuthDBProvider</param-value></context-param><!--增加URI访问前缀--><context-param><param-name>resteasy.servlet.mapping.prefix</param-name><param-value>/api</param-value></context-param><!--restEasy过滤规则--><servlet><servlet-name>Resteasy</servlet-name><servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class></servlet><servlet-mapping><servlet-name>Resteasy</servlet-name><url-pattern>/api/*</url-pattern></servlet-mapping></web-app>
解决方案
解决方案二:
OAuthDBPrivider.java[code=java]packagecom.iovereye.webservice.oauth;importjava.net.HttpURLConnection;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Date;importjava.util.Properties;importjava.util.Set;importjava.util.UUID;importorg.jboss.resteasy.auth.oauth.OAuthConsumer;importorg.jboss.resteasy.auth.oauth.OAuthException;importorg.jboss.resteasy.auth.oauth.OAuthProvider;importorg.jboss.resteasy.auth.oauth.OAuthRequestToken;importorg.jboss.resteasy.auth.oauth.OAuthToken;importcom.iovereye.webservice.util.Logs;publicclassOAuthDBProviderimplementsOAuthProvider{privatestaticfinalStringDEFAULT_CONSUMER_ROLE="user";privatestaticConnectionconn;publicOAuthDBProvider(){initConnection();}privatestaticsynchronizedvoidinitConnection(){if(conn!=null){return;}Propertiesprops=newProperties();try{props.load(OAuthDBProvider.class.getResourceAsStream("/jdbc.properties"));}catch(Exceptionex){thrownewRuntimeException("db.propertiesresourceisnotavailable");}Stringdriver=props.getProperty("jdbc.driverClassName");Stringurl=props.getProperty("jdbc.url");Stringuser=props.getProperty("jdbc.username");Stringpassword=props.getProperty("jdbc.password");initConnection(driver,url,user,password);}privatestaticvoidinitConnection(Stringdriver,Stringurl,Stringuser,Stringpassword){try{Class.forName(driver);conn=DriverManager.getConnection(url,user,password);}catch(Exceptionex){thrownewRuntimeException("InmemoryOAuthDBcannotbecreated"+ex.getMessage());}//try{//////consumers//update("CREATETABLEconsumers(idINTEGERAUTO_INCREMENTPRIMARYKEY,c_keyVARCHAR(150)"//+",secretVARCHAR(256),display_nameVARCHAR(256),connect_uriVARCHAR(256),"//+"scopesVARCHAR(256),permissionsVARCHAR(256),unique(c_key))");//////requesttokens//update("CREATETABLErequest_tokens(idINTEGERAUTO_INCREMENTPRIMARYKEY,consumer_keyVARCHAR(150)"//+",tokenVARCHAR(256),secretVARCHAR(256),callbackVARCHAR(256),scopesVARCHAR(256)"//+",permissionsVARCHAR(256),verifierVARCHAR(256),foreignkey(consumer_key)referencesconsumers(c_key))");//////accesstokens//update("CREATETABLEaccess_tokens(idINTEGERAUTO_INCREMENTPRIMARYKEY,consumer_keyVARCHAR(150)"//+",tokenVARCHAR(256),secretVARCHAR(256),scopesVARCHAR(256),"//+"permissionsVARCHAR(256),foreignkey(consumer_key)referencesconsumers(c_key))");//////custompermissionstorolesmap//update("CREATETABLEpermissions(idINTEGERAUTO_INCREMENTPRIMARYKEY,permissionVARCHAR(256)"//+",roleVARCHAR(256))");//////addpermissionstovaluesmap//registerCustomPermissionsAndRoles();////}catch(SQLExceptionex){////thrownewRuntimeException("OAuthDBtablescannotbecreated:"//+ex.getMessage());////}}privatestaticsynchronizedvoidupdate(Stringexpression)throwsSQLException{Statementst=conn.createStatement();//statementsinti=st.executeUpdate(expression);//runthequeryif(i==-1){System.out.println("dberror:"+expression);}st.close();}privatestaticvoidregisterCustomPermissionsAndRoles(){try{//for3-legOAuthdemosinvolvingtheenduseranda3rdparty//printerserviceupdate("INSERTINTOpermissions(permission,role)"+"VALUES('"+"printResources"+"','"+"PrinterService"+"'"+")");//forOAuthpushmessagingdemosupdate("INSERTINTOpermissions(permission,role)"+"VALUES('"+"sendMessages"+"','"+"MessagingService"+"'"+")");}catch(SQLExceptionex){thrownewRuntimeException("Permissionscannotbemappedtoroles");}}/***注册应用*/publicOAuthConsumerregisterConsumer(StringconsumerKey,StringdisplayName,StringconnectURI)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classregisterConsumer...");Stringsecret=makeRandomString();try{update("INSERTINTOconsumers(c_key,secret,display_name,connect_uri)"+"VALUES('"+consumerKey+"','"+secret+"'"+","+(displayName==null?null:"'"+displayName+"'")+","+(connectURI==null?null:"'"+connectURI+"'")+")");}catch(SQLExceptionex){thrownewOAuthException(HttpURLConnection.HTTP_UNAUTHORIZED,"Consumerwithkey"+consumerKey+"cannotbecreated");}returnnewOAuthConsumer(consumerKey,secret,displayName,connectURI);}privatestaticStringmakeRandomString(){returnUUID.randomUUID().toString();}/***注册应用范围*/publicvoidregisterConsumerScopes(StringconsumerKey,String[]scopes)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classregisterConsumerScopes...");}/***注册应用认可*/publicvoidregisterConsumerPermissions(StringconsumerKey,String[]permissions)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classregisterConsumerPermissions...");}/***获取范围*/publicStringgetRealm(){Logs.getLogger().debug("OAuthDBProvider.classgetRealm...");return"default";}/***获取应用信息*/publicOAuthConsumergetConsumer(StringconsumerKey)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classgetConsumer...");Logs.getLogger().debug("time:"+newDate().getTime());try{Statementst=conn.createStatement();ResultSetrs=st.executeQuery("SELECT*FROMconsumersWHEREc_key='"+consumerKey+"'");if(rs.next()){Stringkey=rs.getString("c_key");Stringsecret=rs.getString("secret");StringdisplayName=rs.getString("display_name");StringconnectURI=rs.getString("connect_uri");Stringscopes=rs.getString("scopes");Stringperms=rs.getString("permissions");OAuthConsumerconsumer=newOAuthConsumer(key,secret,displayName,connectURI,perms!=null?newString[]{perms}:null);consumer.setScopes(newString[]{scopes});returnconsumer;}else{thrownewOAuthException(HttpURLConnection.HTTP_UNAUTHORIZED,"Nosuchconsumerkey"+consumerKey);}}catch(SQLExceptionex){thrownewOAuthException(HttpURLConnection.HTTP_UNAUTHORIZED,"Nosuchconsumerkey"+consumerKey);}}/***获取用户Token*/publicOAuthRequestTokengetRequestToken(StringconsumerKey,StringrequestToken)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classgetRequestToken...");returnnull;}/***获取用户accessToken*/publicOAuthTokengetAccessToken(StringconsumerKey,StringaccessToken)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classgetAccessToken...");returnnull;}/***生成请求Token*/publicOAuthTokenmakeRequestToken(StringconsumerKey,Stringcallback,String[]scopes,String[]permissions)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classmakeRequestToken...");returnnull;}/***生成AccessToken*/publicOAuthTokenmakeAccessToken(StringconsumerKey,StringrequestToken,Stringverifier)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classmakeAccessToken...");returnnull;}/***授权请求Token*/publicStringauthoriseRequestToken(StringconsumerKey,StringrequestToken)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classauthoriseRequestToken...");returnnull;}/***检查时间戳*/publicvoidcheckTimestamp(OAuthTokentoken,longtimestamp)throwsOAuthException{Logs.getLogger().debug("OAuthDBProvider.classcheckTimestamp...");}/***转换角色的权限*/publicSet<String>convertPermissionsToRoles(String[]permissions){Logs.getLogger().debug("OAuthDBProvider.classconvertPermissionsToRoles...");returnnull;}}
解决方案三:
[/code]数据库:/*NavicatMySQLDataTransferSourceServer:localhostSourceServerVersion:50161SourceHost:localhost:3306SourceDatabase:iovereye_testTargetServerType:MYSQLTargetServerVersion:50161FileEncoding:65001Date:2014-04-1418:31:19*/SETFOREIGN_KEY_CHECKS=0;--------------------------------Tablestructurefor`access_tokens`------------------------------DROPTABLEIFEXISTS`access_tokens`;CREATETABLE`access_tokens`(`id`int(11)NOTNULLAUTO_INCREMENT,`consumer_key`varchar(150)DEFAULTNULL,`token`varchar(256)DEFAULTNULL,`secret`varchar(256)DEFAULTNULL,`scopes`varchar(256)DEFAULTNULL,`permissions`varchar(256)DEFAULTNULL,PRIMARYKEY(`id`),KEY`consumer_key`(`consumer_key`),CONSTRAINT`access_tokens_ibfk_1`FOREIGNKEY(`consumer_key`)REFERENCES`consumers`(`c_key`))ENGINE=InnoDBDEFAULTCHARSET=utf8;--------------------------------Recordsofaccess_tokens--------------------------------------------------------------Tablestructurefor`consumers`------------------------------DROPTABLEIFEXISTS`consumers`;CREATETABLE`consumers`(`id`int(11)NOTNULLAUTO_INCREMENT,`c_key`varchar(150)DEFAULTNULL,`secret`varchar(256)DEFAULTNULL,`display_name`varchar(256)DEFAULTNULL,`connect_uri`varchar(256)DEFAULTNULL,`scopes`varchar(256)DEFAULTNULL,`permissions`varchar(256)DEFAULTNULL,PRIMARYKEY(`id`),UNIQUEKEY`c_key`(`c_key`))ENGINE=InnoDBAUTO_INCREMENT=4DEFAULTCHARSET=utf8;--------------------------------Recordsofconsumers------------------------------INSERTINTO`consumers`VALUES('1','110','4c088c17-6461-40a3-ab77-6aa4dde1d0dc','JieMo','http://localhost:8080/iovereye-test-restEasy/',null,null);INSERTINTO`consumers`VALUES('3','jiemo','91b17bf9-b9e3-479d-9450-2099e624c93d','JieMo','http://localhost:8080/iovereye-test-restEasy/',null,null);--------------------------------Tablestructurefor`ie_install`------------------------------DROPTABLEIFEXISTS`ie_install`;CREATETABLE`ie_install`(`id`bigint(20)NOTNULLAUTO_INCREMENT,`name`varchar(255)DEFAULTNULL,`description`varchar(255)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8;--------------------------------Recordsofie_install--------------------------------------------------------------Tablestructurefor`permissions`------------------------------DROPTABLEIFEXISTS`permissions`;CREATETABLE`permissions`(`id`int(11)NOTNULLAUTO_INCREMENT,`permission`varchar(256)DEFAULTNULL,`role`varchar(256)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8;--------------------------------Recordsofpermissions--------------------------------------------------------------Tablestructurefor`request_tokens`------------------------------DROPTABLEIFEXISTS`request_tokens`;CREATETABLE`request_tokens`(`id`int(11)NOTNULLAUTO_INCREMENT,`consumer_key`varchar(150)DEFAULTNULL,`token`varchar(256)DEFAULTNULL,`secret`varchar(256)DEFAULTNULL,`callback`varchar(256)DEFAULTNULL,`scopes`varchar(256)DEFAULTNULL,`permissions`varchar(256)DEFAULTNULL,`verifier`varchar(256)DEFAULTNULL,PRIMARYKEY(`id`),KEY`consumer_key`(`consumer_key`),CONSTRAINT`request_tokens_ibfk_1`FOREIGNKEY(`consumer_key`)REFERENCES`consumers`(`c_key`))ENGINE=InnoDBDEFAULTCHARSET=utf8;--------------------------------Recordsofrequest_tokens------------------------------
创建consumershttp://localhost:8080/iovereye-test-restEasy/oauth/consumer/registration?oauth_consumer_key=jiemo&xoauth_consumer_display_name=JieMo&xoauth_consumer_connect_uri=http://localhost:8080/iovereye-test-restEasy/requestTokenhttp://localhost:8080/iovereye-test-restEasy/oauth/requestToken?oauth_consumer_key=110&oauth_signature=4c088c17-6461-40a3-ab77-6aa4dde1d0dc&oauth_timestamp=1325632323&oauth_nonce=123&oauth_signature_method=HMAC-SHA1&oauth_callback=localho==============================请求失败返回状态码:401错误输出:signature_invalid==============================求解,请多多指教
解决方案四:
不好意思,晚上加班,解决了,是因为没有加密请求参数。
解决方案五:
我看你,学了不会少,可以留个联系方式吗?
解决方案六:
大侠能把那个加密参数补上吗?