Spring-Security OAuth2 setup - unable to locate oauth2 namespace handler - spring

I'm trying to setup our REST server for OAuth2 with spring-security. (The server already supports spring-security without OAuth). Now I tried to follow the sparklr example and added the spring-security-oauth artifact to my maven (mvn dependency:tree shows it is available) as well as the namespace configuration to my spring-security-context.xml but all I get is:
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security/oauth2]
Browsing to http://www.springframework.org/schema/security/ all I see are the .xsd files but no oauth2 folder.. How can that be? I am assuming the sparklr example is a working version, so what am I doing wrong?
Here's my spring-security-context header:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
If needed, here's the maven setup:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth</artifactId>
<version>1.0.0.RC2</version>
</dependency>

You referer to OAuth2 but import the OAuth 1. replace the dependency to
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

You need to ensure that spring-security-oauth2 jar is present in classpath and it will work
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.4.0.RELEASE</version>
</dependency>

Related

Spring boot SSO with Oauth2 and Spring-boot-starter-parent version 2+

I got a good and simple SSO sample project from here
Currently the sample works as below.
Start app1, app2, sso-server.
Load http://localhost:8082/app1 It will redirect to login page of
http://localhost:8080/sso-server
username: user, password: password
On successful login, it will redirect back to
http://localhost:8082/app1 The page will show "Welcome to app1, user"
Now on loading http://localhost:8083/app2 The page will show "Welcome
to app2, user" since we have already logged in.
Now my issue is the sample uses spring-boot-starter-parent version 1.5.9.RELEASE
The sample uses spring-cloud dependency also. I read like spring-cloud will not support spring-boot-starter-parent version 2 or above.
So I tried to remove spring cloud dependency from app1 and I could start the application after a tough try. My new pom is as below.
<?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.shekhargulati</groupId>
<artifactId>app1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>app1</name>
<description>App1</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
Now on loading app1, it will show sso login window. But after successfull login, it will throw 404 error.
And if I do the same changes in sso-server application, the authorization will not work. Authorization API wil lthrow 404.
Please help me to fix this.
My need is, make this application work as earlier with spring boot starter parent version 2+.
Here is the link for the code in github which is forked from the link which is provided in the above question which is providing all the feature you have expected using Spring-Boot 2.X version.
In the above code I have moved your code from Spring boot version 1.5.9.RELEASE to 2.1.3.RELEASE and Spring Cloud version from Edgware.SR1 to Finchley.SR1

Multiple Spring WEB MVC versions in pom.xml

I totally new with Spring MVC and just started learning it 3 days ago. I'm still trying to understand it's configuration. I would like to know if it's advisable and possible to have multiple dependency definitions in pom.xml file.
<?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.mywebsite</groupId>
<artifactId>emusicstore</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
I know this may be a simple question but I need to know if this is a good practice or not.
I noticed that when I tried to use the 4.3.3 RELEASE it didn't generate the dispatcher-servlet.xml and applicationContext.xml files inside the WEB-INF folder. But when I used the 4.2.4 RELEASE it generated the two files.
Thank you.
It's not a good practice, because it's better to be explicit in which versions to use.
Maven will just omit one of the versions. You can use the Maven dependency plugin to see which ones are selected:
mvn dependency:analyze

SolrJ Embedded Server Spring Config No longer working

I have this spring config that I used in my tests:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd">
<solr:embedded-solr-server id="tgySolrServer" solrHome="target/test-classes" />
</beans>
throwing now this exception
Caused by: java.lang.NoSuchMethodError: org.apache.solr.core.CoreContainer.<init>(Ljava/lang/String;Ljava/io/File;)V
at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.createPathConfiguredSolrServer(EmbeddedSolrServerFactory.java:101)
at org.springframework.data.solr.server.support.EmbeddedSolrServerFactory.initSolrServer(EmbeddedSolrServerFactory.java:77)
at org.springframework.data.solr.server.support.EmbeddedSolrServerFactoryBean.afterPropertiesSet(EmbeddedSolrServerFactoryBean.java:36)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 40 more
I currently have Solr 4.5.0 and spring-data-solr 1.0.0-RELEASE. What should I do to use the embedded server in my tests?
I got it working now
we need to add following dependency and repository to pom.xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
</dependency>
<repositories>
<repository>
<id>spring-maven-snapshot</id>
<url>http://repo.springsource.org/libs-snapshot</url>
</repository>
</repositories>
That's it !!!

Spring dependency Error

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
I got an error:
("cvc-complex-type.2.4.a: Invalid content was found starting with
element 'dependency'. One of
'{"http://www.springframework.org/schema/beans":import, "http://
www.springframework.org/schema/beans":alias,
"http://www.springframework.org/schema/beans":bean,
WC[##other:"http://www.springframework.org/schema/beans"]}' is
expected.)
i have been included (jackson-mapper-asl and jackson-core-asl) into my lib in my Work Space
not getting Why This Error Occurred Kindly Help.
You have two things mixed up: Spring Configuration files and Maven's project configuration file (pom.xml). The <dependency> element belongs in the pom.xml file, not in the Spring Configuration.
Also you do not need to include the jar files anywhere, Maven takes care of the dependencies.

Spring 3.1 Cache namespace problem

i am trying to use 3.1 spring cache configuration and namespaces as per spring doc. i have also downloaded latest jar from spring site but still i am getting
this problem regarding namespaces
Unable to locate Spring NamespaceHandler for element 'cache:annotation-driven' of schema namespace 'http://www.springframework.org/
schema/cache'
spring config file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml"/>
you need spring-context-support.jar and spring-context.jar on the classpath.
spring-context has the CacheNamespaceHandler class.
(note that spring is now using a different naming scheme for their jars, but context and context support are retained in the names)
For Maven users, add the following dependency:
<spring-version>3.1.0.RELEASE</spring-version>
<ehcache-version>2.4.2</ehcache-version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>${ehcache-version}</version>
</dependency>

Resources