I am trying to follow some tutorials to learn how to secure a very simply spring-boot REST app with oauth2. I am trying to implement the Java config file as done on https://blog.safaribooksonline.com/2013/10/08/secure-rest-services-with-spring-security/.
That starts with
#Configuration
#EnableWebSecurity
class SecurityConfiguration extends OAuth2ServerConfigurerAdapter {
private final String applicationName = ServiceConfiguration.CRM_NAME;
However, when I add that to my project, my IDE tells me it cannot resolve OAuth2ServerConfigurerAdapter or ServiceConfiguration, and I cannot find a import (or a maven dependency with a import) that resolves.
In my pom.xml, I have
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
I have also tried to add this import to the class:
import org.springframework.security.oauth2.*;
Even with that and making sure to reimport the dependencies, I am still being told that that cannot be resolved.
Any ideas as to what I am missing? Is there a maven dependency I need? Do I need to have this spring config class in a certain place in my project and/or have something specific in a xml config file or something...?
Thanks for any help. Let me know if there is more detail I need to provide
I suspect that when the code you're referring to was written, the Java configuration for OAuth2 was a work in progress and has since changed.
This article might be a better starting point. The OAuth2 samples are also now using Java Config, so are a good resource to based your code on.
Related
So, I am working with webservices using SOAP and Maven. This error - I think - is a warning instead because it let me run the application and the service just fine for now. Whenever I call any other package inside this module class it turns red. If I don't add packages the error disappears but I need jasperreports (strange, as the involved package got nothing to do with this)
I've followed this: Module reads package from both
Using the solution "implementation and excludes" does nothing. Probably because it is Gradle but I had to try restricting the xml transform using the suggested message.
I also tried Package 'com.example' reads package 'javafx.beans' from both 'javafx.base' and 'javafx.base' this.
I followed this guide as well. https://maven.apache.org/guides/mini/guide-multiple-modules.html
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
Making those package use optional does not help either.
Important note, using a different package I want to use the ERROR WILL CHANGE. I removed and added some so you can withness it. Check these:
In short, a Web Service dependency using some of the declared in the client as well. As mentioned, I went one by one until I found it.
Also, updating javax.xml version of the dependency was a necessary step in order to solve the problem.
I try to import a dependency in POM in the following way:
<dependency>
<groupId>org.test.maven-presets</groupId>
<artifactId>caffe</artifactId>
<version>preset-1.3.3-SNAPSHOT</version>
<classifier>pojos</classifier>
</dependency>
There are 4 jar files: 1) caffe-preset-1.3.3-SNAPSHOT.jar 2) caffe-preset-1.3.3-SNAPSHOT-javadoc.jar 3) caffe-preset-1.3.3-SNAPSHOT-sources.jar and 4) caffe-preset-1.3.3-SNAPSHOT-pojos.jar.
My requirement is only I have to use caffe-preset-1.3.3-SNAPSHOT-pojos.jar. But even though I use the classifier tag, I am still fetching the classes that are under caffe-preset-1.3.3-SNAPSHOT.jar. How can I only use the classes that are under the tag?
Like I have only the POJOs under 4th Jar which is used as a dependency in another project.
I can add these POJO classes in another project and create a Jar out of it separately and can use in other projects, but our technical team management is not agreeing to create New Project, as it requires different approvals and has to Justify to each of them clearly.
Could anyone please help me to get through the requirement? Thanks.
I am currently running into some problems with spring boot and multi maven project structure. I am using Spring Boot 4.3.1.
My project structure looks as follows:
parent
-- pom.xml
-- application
-- pom.xml
-- src
-- main
-- java
-- Application.java (annotated with #SpringBootApplication)
-- test
-- java
-- MyApplicationTest.java (annotated with #SpringBootTest)
-- library
-- pom.xml
-- src
-- main
-- java (...)
-- test
-- java
-- MyLibraryTest.java (annotated with #SpringBootTest)
application module has a dependency on library.
MyApplicationTest works perfectly fine, but running MyLibraryTest instead, I fail with the following error:
java.lang.IllegalStateException: Unable to find a #SpringBootConfiguration, you need to use #ContextConfiguration or #SpringBootTest(classes=...) with your test at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOr FindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
My first guess is that library needs a dependency on application, but this causes a cycle.
Is there any solution to that problem?
How can I structure my application correctly?
thanks a lot for suggestions.
MyLibraryTest looks as follow:
#RunWith(SpringRunner.class)
#SpringBootTest
#Transactional
public class MyLibraryTest {
#Autowired
private MyService service;
#Test
public void testMyService_Save() {...}
}
You need to make sure that the library module's pom.xml includes -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
which is required for the module to use the #SpringBootTest annotation. You might already be using the same in your app module but is not included in the library module.
Well post the Edit, found the question to be possibly a duplicate of Unable to find a #SpringBootConfiguration when doing a JpaTest
Also quoting from Thomas's answer from the same thread, here
The thing about #DataJpaTest and a few other annotations is that they
look for a #SpringBootConfiguration annotation in the current package,
and if they cannot find it there, they traverse the package hierarchy
until they find it.
For example, if the fully qualified name for your test class was
com.example.test.JpaTest and the one for your application was
com.example.Application, then your test class would be able to find
the #SpringBootApplication (and therein, the
#SpringBootConfiguration).
If the application resided in a different branch of the package
hierarchy, however, like com.example.application.Application, it would
not find it.
which seems to be the case for you, where you are trying to test an application in a different module itself. Hence the error that you see.
I've been going through the process of converting my Mule project to a Spring Boot application, and have hit a snag I can't seem to figure out.
I'm pretty new to Spring Boot so I'm not sure if my issues lie with it, or with the way I'm doing my mule stuff.
Here is my sample project I've been trying to convert: https://github.com/JustinBell/mule-webapp-example
When I deploy this to a tomcat instance it works great, the issue comes when I try to run it as a Spring Boot application I'm getting this exception:
ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
Just as a note I'm moving from mule 3.6.1 to 3.7.0-M1 as that's required (from my understanding) to use Spring Boot.
I've tried looking around for support on this issue which seems to pretty common, but none of the suggestions I've found have solved the issue.
Thanks for any help with these issues!
There are a few things that aren't quite right in your code as it stands.
If you want to build a web app with Spring Boot, you'll typically want to add a dependency on spring-boot-starter-web. This provides, among other things, the embedded servlet container:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
Your app's dependency on org.mule.transports:mule-transport-servlet pulls in a very old version of Tomcat's Coyote module. You need to exclude this to avoid it clashing with the up-to-date dependency that's provided by spring-boot-starter-web:
<dependency>
<groupId>org.mule.transports</groupId>
<artifactId>mule-transport-servlet</artifactId>
<version>${mule.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>coyote</artifactId>
</exclusion>
</exclusions>
</dependency>
Your Application class is trying to run MuleContextInitializer which it also declares as a bean. It should be running Application.class:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
// ...
}
Your Application class is also in the default package. You should avoid using the default package as it will cause Spring Boot to scan then entire classpath looking for your application's classes and configuration. Moving it into a package of its own to stop this from happening.
Lastly, the app fails to launch as it's looking for a file named mule-config.xml. Renaming mule-webapp-demo.xml to mule-config.xml addresses this.
I believe autodelete is an Enterprise feature, perhaps you are using ftp rather than ftp-ee.
getting below error after i configure MQ connection factory.
java.lang.ClassCastException: com.ibm.ejs.jms.JMSQueueConnectionFactoryHandle incompatible with com.ibm.mq.jms.MQQueueConnectionFactory
my code snippet where the exception is pointing to :
String queueConnectionJndi = props.getProperty(queueConnection + MQ_CONN);
queueConnectionFactory = MQQueueConnectionFactory)initialContext.lookup(queueConnectionJndi);
I am not able to find out the root cause of this.
can any body please help me on this, Thanks in advance.
There is no way to be sure without more context, but it looks like this method call:
initialContext.lookup(queueConnectionJndi);
is returning an object of type com.ibm.ejs.jms.JMSQueueConnectionFactoryHandle which cannot be cast to com.ibm.mq.jms.MQQueueConnectionFactory.
Can you provide more context?
This Post on old nabble sounds like a similar issue and may help you out.
Specifically the final response talks about removing any jms.jar file(s) that may be in your deployed WAR. Check your WEB-INF/lib. Certain jars are provided by the Websphre container and shouldn't be including them in your WAR.
This Post on the spring fourm also indicates issues of this nature caused by jars included in the classpath that shouldn't be there
Remove any of the following if you find them...
naming.jar
providerutil.jar
jndi.jar
jms.jar
mq.jar
websphere.jar
Can you rewrite your code to use JMS standard (ConnectionFactory or QueueConnectionFactory)instead of a Websphere MQ specific implementation class? That way you won't be tying your app to Websphere MQ and porting it to an alternative MQ implementation would be easier...
i.e.
import javax.jms.QueueConnectionFactory;
...
queueConnectionFactory = (QueueConnectionFactory)initialContext.lookup(jndiName);
the MQ jars which WAS is using and my application using are different so this problem occured. when i corrected the classpath it is resolved. sorry for the trouble, thanks for the help.
I went through a lot of trial and error to find the answer (the answer to my question at least). I hope this solution will solve your issues too. As mentioned from another post excluding the jms library works. But how do you exclude the jms library and still be able to compile the code? That was something no one seems to have mentioned. The solution to that is to make the scope for the jms library to "provided" (if you are using Maven or Gradle).
As mentioned somewhere:
"Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory."
So in your pom.xml add/update these:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
Hopefully this can be helpful to those who have been frustrated by the lack of answers from the Internet.
Remove all the ibm libraries. They are useless. Once you deploy onto Websphere, it will use its libraries anyways.