implementing authentication using oauth 2.0 and wicket - wicket

I try to bring up secure communication with our customer using oauth 2.0.
first of all I have to confess that I am totally new to oauth.
Used technologies are as follow: wicket, spring,
I took the following steps.
Add dependency in pom.xml
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth</artifactId>
<version>1.0.0.M3</version>
</dependency>
Then I added the following in WEB-INF/web.xml
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Finally I added these lines to META-INF/spring-context/application.xml
<beans:bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.InMemoryOAuth2ProviderTokenServices">
<beans:property name="supportRefreshToken" value="true" />
</beans:bean>
<oauth:provider client-details-service-ref="clientDetails" token-services-ref="tokenServices">
<oauth:verification-code user-approval-page="/oauth/confirm_access" />
</oauth:provider>
<oauth:client-details-service id="clientDetails">
<oauth:client clientId="foo" authorizedGrantTypes="authorization_code" />
</oauth:client-details-service>
But I got this error:
25 09 12 14:48:11:921:ERROR: [ContextLoader] Context initialization failed
java.lang.NoClassDefFoundError: org/springframework/core/env/ConfigurableEnvironment
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
at java.lang.Class.getConstructor0(Class.java:2714)
at java.lang.Class.getDeclaredConstructor(Class.java:2002)
...

I think you need the spring-core in your maven dependencies. Specifically version 3.1 or upwards.

It is usually like this that when we add a dependency, It contains necessary dependencies itself. Isn't it?
Cause I got so Error and I add till now many dependency but still face some new ones?
Is that logical ... to add dependency till I can run the project.
I have still no code inside ... just try to add OAUTH dependency!

Related

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

I am trying to build a simple hello world application for two days using Jersey + Google app engine. For simple AppEngine project I followed these tutorials and both works just fine
https://developers.google.com/appengine/docs/java/gettingstarted/creating
https://developers.google.com/appengine/docs/java/webtoolsplatform
But now I am trying to add Jersey and following this tutorial http://www.vogella.com/articles/REST/article.html.
But server keeps giving me
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
when I add these lines in web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestServer</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I have downloaded Jersey JAX-RS 2.1 RI bundle from here and have added all jar files in WEB-INF/lib folder as described in tutorial. And even after two days nothing is working. I have searched several times on Google and apparently people who are using Maven have solved it somehow but I am not using Maven neither did the guy who wrote that tutorial.
Just to check if even com.sun.jersey.spi.container.servlet.ServletContainer exists in imported Jersey jars I tried to just write this fully qualified name in Java and let the intellisense finish names but I couldn't get any intellisense after com.sun.je so my last guess is that there have been some package rearrangement in latest Jersey build and jersey is no longer inside com.sun. I am exhausted and I would appreciate any kind of help.
You have downloaded Jersey 2 (which RI of JAX-RS 2). The tutorial you're referring to uses Jersey 1. Download Jersey 1.17.1 from (here), should be sufficient for you.
Jersey 1 uses com.sun.jersey, and Jersey 2 uses org.glassfish.jersey hence the exception.
Also note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
Edit
Registering Resources and Providers in Jersey 2 contains additional info on how to register classes/instances in Jersey 2.
If you are using jersey 2.x then you need different configuration in web.xml as servlet class is change in it. you can update your web.xml with following configuration.
<servlet>
<servlet-name>myrest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>your.package.path</param-value>
</init-param>
<init-param>
<param-name>unit:WidgetPU</param-name>
<param-value>persistence/widget</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myrest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Add this in pom
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
It's an eclipse setup issue, not a Jersey issue.
From this thread ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish.
So Eclipse wasn't using the Gradle dependencies when Apache was starting .
I also faced a similar issue. Resolved the problem by going through the step step tutorial from the below link.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/
The main thing to notice is that the jersey libraries should be placed correctly in TOMCAT WEB-INF/lib folder. It is done automatically by the Eclipse settings mentioned in the above link. It will create a WAR file with the dependent JAR Files. Else, you will run into problems with ClassNotFound Exception.
apache-tomcat-7.0.56-windows-x64\apache
-tomcat-7.0.56\webapps\JerseyJSONExample\WEB-INF\lib
"11/23/2014 12:06 AM 130,458 jersey-client-1.9.jar
11/23/2014 12:06 AM 458,739 jersey-core-1.9.jar
11/23/2014 12:06 AM 147,952 jersey-json-1.9.jar
11/23/2014 12:06 AM 713,089 jersey-server-1.9.jar"
4 File(s) 1,450,238 bytes
The second tutorial explains about how to create a Webservice which produces and consumes JSON output.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/
Both the links gave a good picture on how things work and save a lot of time.
try this :
org.glassfish.jersey.servlet.ServletContainer
on servlet-class
I had the same problem as you though I have followed a different guide: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/
The strange part is that, in this guide I have used, I should not have any problem with compatibility between versions (1.x against 2.x) because following the guide you use the jersey 1.8.x on pom.xmland in the web.xmlyou refer to a class (com.sun.jersey.spi.container.servlet.ServletContainer) as said before of 1.x version. So as I can infer this should be working.
My guess is because I'm using JDK 1.7 this class does not exist anymore.
After, I tried to resolve with the answers before mine, did not helped, I have made changes on the pom.xmland on the web.xml the error changed to: java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
Which supposedly should be exist!
As result of this error, I found a "new" solution: http://marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/
With Maven (archetypes), generate a jersey project, likes this:
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.0
And it worked for me! :)
We get this error because of build path issue. You should add "Server Runtime" libraries in Build Path.
"java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer"
Please follow below steps to resolve class not found exception.
Right click on project --> Build Path --> Java Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0
I encountered the same error today although I was using Jersey 1.x, and had the right jars in my classpath. For those who'd like to follow the vogella tutorial to the letter, and use the 1.x jars, you'd need to add the jersey libraries to WEB-INF/lib folder. This will certainly resolve the problem.
you need to add jersey-bundle-1.17.1.jar to lib of project
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!-- <param-name>jersey.config.server.provider.packages</param-name> -->
<param-value>package.package.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
You must replace in your web.xml:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
for this:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
this is Jersey 2.x uses org.glassfish.jersey packages instead of com.sun.jersey (which is used by Jersey 1.x) and hence the exception. Note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.x once you migrate to JAX-RS 2.0 and Jersey 2.x
if at any moment you use maven, your pom.xml would be this:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.X</version>
</dependency>
replace 2.X for your desire version, e.g. 2.15
A simple workaround is , check whether you have dependencies or libs in deployment assembly of eclipse.probably if you are using tomcat , the server might not have identified the libs we are using . in that case specify it explicitly in deployment assembly.
Coming back to the original problem - java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
As rightly said above, in JAX 2.x version, the ServletContainer class has been moved to the package - org.glassfish.jersey.servlet.ServletContainer. The related jar is jersey-container-servlet-core.jar which comes bundled within the jaxrs-ri-2.2.1.zip
JAX RS can be worked out without mvn by manually copying all jars contained within zip file jaxrs-ri-2.2.1.zip (i have used this version, would work with any 2.x version) to WEB-INF/lib folder. Copying libs to right folder makes them available at runtime.
This is required if you are using eclipse to build and deploy your project.
In pom.xml file we need to add
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>
The same error and wasted 2+ hours debugging and trying all options. I was not using the Maven/POM, so I could not leverage that solution given by few.
Finally the following resolved it: Adding the jars directly to the tomcat/lib (NOT WEB-INF\lib) folder and restarting the tomcat.
If anyone is trying to build a hello world application using Jersey, I think one of the easiest ways is to follow Jersey documentation.
https://jersey.github.io/download.html
If you are already using maven, it'd take only a few minutes to see the result.
I used below.
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.26
It basically depends on which version jersey you are using. If you are using Jersey ver.1.X.X you need to add
Jersey 1 uses "com.sun.jersey", and Jersey 2 uses org.glassfish. on servlet class tag.
Also, note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
And Add all the jar file into WEB-INF lib folder
In my case, it worked after adding the jersey-bundle jar in my tomcat lib.

Spring social ConnectController /connect/facebook gives a 404 error

I am trying to get the facebook login working with spring social.
I have created an app on facebook.
Dispatcher Servlet
<servlet-mapping>
<servlet-name>ABC</servlet-name>
<url-pattern>*.cs</url-pattern>
</servlet-mapping>
configfured the xmls
<servlet-mapping>
<servlet-name>ABC</servlet-name>
<url-pattern>*.cs</url-pattern>
</servlet-mapping>
<!-- Configure a connection repository through which account-to-provider connections will be stored -->
<beans:bean id="connectionRepository" class="org.springframework.social.connect.jdbc.JdbcConnectionRepository">
<beans:constructor-arg ref="dataSource" />
<beans:constructor-arg ref="textEncryptor" />
</beans:bean>
<!-- Configure a Facebook service provider -->
<beans:bean class="org.springframework.social.facebook.connect.FacebookServiceProvider">
<beans:constructor-arg index="0" value="564846465216" />
<beans:constructor-arg index="1" value="f4554iojfjh9iu7km54f54pok5ok4" />
<beans:constructor-arg index="2" ref="connectionRepository" />
</beans:bean>
<beans:bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors" factory-method="noOpText" />
<beans:bean class="org.springframework.social.web.connect.ConnectController">
<beans:constructor-arg index="0" value="http://localhost:8080/CitySpanks/" />
</beans:bean>
I am referring this link
http://static.springsource.org/spring-social/docs/1.0.0.M2/reference/html/connecting.html
in my jsp
Connect to Facebook
Anything i am missing ??? I keep getting a 404 error on clicking the above link.
Thanks :)
I'm not sure where you are at in your project but I think the following may be easy to overlook...
If you look at the sample projects for spring-social-showcase you'll notice that under
spring-social-showcase-sec/src/main/webapp/WEB-INF/views/connect
exist the following pages:
status.jsp, facebookConnect.jsp, facebookConnected.jsp.
In the sample project when the following (left) is requested it will resolve the .jsp's below (right):
"connect/facebook" => Connected => "connect/facebookConnected.jsp"
"connect/facebook" => Not Connected => "connect/facebookConnect.jsp"
"connect" => "connect/status.jsp"
This is documented here: 4.3.1 Displaying a connection page
So, to get that example to work in YOUR code you have to create the same directory structure "WEB-INF/views/connect/" with the above .jsp's in it. Otherwise they will not be found, hence the 404. I believe this is resolved by the ConnectController class, behind the scenes, as described in docs linked above.
However, you may want to start off with using their web.xml configuration found in spring-social-samples and once you get the hang of that, go from there.
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
DispatcherServlet is mapped to *.cs, but the link in your JSP is to /connect/facebook (relative to the application root). That URL doesn't end in .cs, so there's no match and DispatcherServlet never gets involved.
Although you can still map DispatcherServlet to any arbitrary URL pattern, the "best practice" since Spring 3.0 is to simply map it to "/", to allow it to respond to a variety of URLs, including those for RESTful APIs.
Of course, that also means that DispatcherServlet will respond to requests for images, style sheets, javascript, and other resources, which is probably not what you want. and were created to solve that problem. See the reference documentation here and here for more details.

Liferay JPA ClassLoader Error

EDITED (I'm simplifying as my original question was too convoluted):
Create a portlet plugin in Liferay 6.1.20
Add in your portlet's web.xml a spring context loader
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Add in your application context an instance of an EntityManagerFactory
<bean id="localEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="WEB-INF/classes/META-INF/persistence.xml" />
</bean>
Make sure in your persistance definition you're referencing a JPA Implementation
<persistence-unit name="casd" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="eclipselink.weaving" value="false" />
</properties>
</persistence-unit>
Watch Tomcat cry
java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "org.eclipse.persistence.jpa.PersistenceProvider.getProviderUtil()Ljavax/persistence/spi/ProviderUtil;" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, org/eclipse/persistence/jpa/PersistenceProvider, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for interface javax/persistence/spi/PersistenceProvider have different Class objects for the type javax/persistence/spi/ProviderUtil used in the signature
Play with hibernate3.jar, eclipselink.jar, persistence.jar until exhaustion, and realize Liferay is grinning at you.
How would you erase that irritating smile from Liferay's face?
In other words,
How can I get a JPA provider instantiated in a spring context within a portlet plugin in liferay 6.1.20 without getting a classloader error?
Ok. I managed to "solve" the problem moving ecliseplink.jar from /ROOT/WEB-INF/lib to {tomcat}/lib/ext
I don't like it as a solution, having to mess with ext, but it's a take it or leave it situation.

Struts no action mapped issue

When deploying a struts application to tomcat running in eclipse I'm getting the following error to the console when trying to load the welcome page.
"There is no Action mapped for namespace / and action name . - [unknown location]"
I was trying to follow the tutorial at: http://viralpatel.net/blogs/2009/12/tutorial-create-struts-2-application-eclipse-example.html.
I am obviously just getting started with struts and any help would be appreciated.
Make sure to have the URL-Pattern mapped to Struts in your web.xml file.
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
Make sure you have the action name then mapped to a Java class in your struts.xml.
<action name="find" class="findAction" method="input">
<interceptor-ref name="myStack" />
<result name="input">find</result>
</action>
These are the essentials. The result will then need mapped to a JSP. Good Luck.

Problems with classpath between Eclipse, Tomcat and JUnit in Spring 3 app

I have web app, based on Spring 3.0.3, that I've been developing using Eclipse 3.4. While doing so I've been running the web app in Tomcat 6.0.18 from Eclipse. That is, I have Eclipse use the Tomcat installation meaning that Tomcat will, as need, modify files etc. (at least, that's my understanding of what it's doing).
My problem is specifying the values for the contextConfigLocations in the web.xml. When running from within Eclipse this worked fine:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:applicationContext-security.xml
</param-value>
</context-param>
However, when I package the app into a war file (ROOT.war) and then added it to Tomcat's webapp directory and the try to start Tomcat, I get an error that neither of these applicationContext files can be found. But when I change it to below, Tomcat can find the files:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/applicationContext.xml
/WEB-INF/config/applicationContext-security.xml
</param-value>
</context-param>
I should note that applicationContext.xml includes other applicationContext files that also use the classpath: short hand. When running within Tomcat, I need to drop all use of classpath: in favor of relative paths to get Tomcat to see these files.
Great. Tomcat and Eclise are getting along nicely. But JUnit 4.7 is no longer happy. For whatever reason, files specified using #ContextConfiguration in a test class can't be found unless the classpath: short hand is used. Here is an example:
#ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:applicationContext-security.xml"})
public class UserDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
#Test
public void testCreateUser() {
}
So applicationContext.xml and applicationContext-security.xml are found without a problem; however, property files that are specified in applicationContext.xml are not found.
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>/WEB-INF/config/spring/base.spring-config.properties</value>
<value>/WEB-INF/config/spring/local.spring-config.properties</value>
</list>
</property>
</bean>
But if I specify the location of these files using the classpath: short hand, the property files are found. If I do this though, the files won't be found when running from a war file in Tomcat.
For now I've created a applicationContext-test.xml that is a cut-and-paste conglomeration of all of the other applicationContext files wherein I'm using the classpath: short hand. This seems hacky and error prone and I'm wondering what the issue might be across all of these technologies.
Feedback most welcome!
web.xml content should look like
<context-param>
<description>
Spring Context Configuration.
</description>
<param-name>contextConfigLocation</param-name>
<!-- spring loads all -->
<param-value>
classpath*:spring/*.xml,
classpath*:spring/persistence/*.xml,
classpath*:spring/webapp/*.xml</param-value>
</context-param>
see http://static.springsource.org/spring/docs/2.5.x/reference/resources.html#resources-app-ctx-wildcards-in-resource-paths for further reference
the junit config should follow the same convention with classpath*:
but beware spring might load .xml context files you don't want it to do