Jetty Embedded Server and Spring Security integration - spring-mvc

I'm trying to log-in using Spring Security, my code has worked when I'm using jetty maven plugin. But now I want it to work when run on Jetty Embedded Server too. When I submit the log-in to spring security processing link, it show this warning:
HTTP ERROR: 500
INTERNAL_SERVER_ERROR
RequestURI=/auth/login_check
Caused by:
java.lang.AbstractMethodError
at javax.servlet.http.HttpServletRequestWrapper.changeSessionId(HttpServletRequestWrapper.java:290)
at javax.servlet.http.HttpServletRequestWrapper.changeSessionId(HttpServletRequestWrapper.java:290)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:209)
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:194)
at org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy.applySessionFixation(ChangeSessionIdAuthenticationStrategy.java:48)
at org.springframework.security.web.authentication.session.AbstractSessionFixationProtectionStrategy.onAuthentication(AbstractSessionFixationProtectionStrategy.java:82)
at org.springframework.security.web.authentication.session.ChangeSessionIdAuthenticationStrategy.onAuthentication(ChangeSessionIdAuthenticationStrategy.java:32)
at org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy.onAuthentication(CompositeSessionAuthenticationStrategy.java:83)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:216)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1115)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:361)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:417)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:879)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:741)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:213)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:403)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:522)
I'm guessing it conflict with jsp-2.1, jsp-api-2.1 jar of Jetty. What could I do to fix this? This is my jetty dependency:
<properties>
<jetty.version>6.1.14</jetty.version>
</properties>
...
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>${jetty.version}</version>
</dependency>
<!--jsp support for jetty, add the 2 following -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.1</artifactId>
<version>${jetty.version}</version>
</dependency>
Spring security config in dispatcher servlet xml:
<security:http auto-config="true" use-expressions="true">
<security:form-login login-page="/login"
username-parameter="email" password-parameter="password"
login-processing-url="/auth/login_check" authentication-failure-url="/login?error"
default-target-url="/" always-use-default-target="true" />
<security:logout logout-url="/logout"
logout-success-url="/" delete-cookies="JSESSIONID" />
<security:remember-me token-validity-seconds="1209600"
remember-me-parameter="remember-me" data-source-ref="dataSource" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:password-encoder hash="md5" />
<security:jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select email, password, enabled from users where email=?"
authorities-by-username-query="select username, role from user_roles where username=?" />
</security:authentication-provider>
</security:authentication-manager>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/chamgroupdb" />
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="javax.persistence.validation.factory">validator</prop>
</props>
</property>
<property name="packagesToScan" value="com.chamgroup.model" />
</bean>

You need to upgrade Jetty for this to work.
javax.servlet.http.HttpServletRequest.changeSessionId() was introduced in Servlet 3.1.
Jetty 6 is Servlet 2.4 - and was EOL (End of Life) in 2010
Jetty 7 is Servlet 2.5 - and was EOL in 2014
Jetty 8 is Servlet 3.0 - and was EOL in 2014
Jetty 9.0 - 9.1 were releases based on early draft/alpha/beta releases of the Servlet 3.1 spec
Jetty 9.2.x was the first version to support the final Servlet 3.1 spec
Jetty 9.3.2 is the current stable version of Jetty (and requires Java 8)

Related

java.lang.NoClassDefFoundError: org/springframework/data/geo/Metric

Error:
java.lang.NoClassDefFoundError: org/springframework/data/geo/Metric
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.(JedisConnectionFactory.java:93)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at de.hybris.platform.spring.LazyInitOverrideBeanFactoryPostProcessor.forName(LazyInitOverrideBeanFactoryPostProcessor.java:82)
at java.base/java.util.Optional.map(Optional.java:265)
Even Jedis 3.x has the same issue.
Spring Dependencies:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.0</version>
</dependency>
Beans:
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
</bean>
<bean id ="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- redis template definition -->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="jedisConnFactory" ref="jedisConnFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
I believe that to use Jedis 4.x you would need Spring 3.x. You need to drop to Jedis 3.x I believe. Although I don't believe the particular class you are getting a Class Not Found (Metric) which is Spring Data Commons has changed locations.

using c3p0 with java MVC (JDK 1.6)

I'm trying to create global connections for multiple WAR files (apps) to use.
Each project use entity manager architecture and maven. I'm using spring 3.5.1, tomcat 6.0.39, JDK 1.6.0_45, C3P0 0.9.1.2, hibernate-c3p0 4.2.3.Final, com.mchange c3p0 0.9.2
I saw examples all over but none works for me, I'm not sure why.
I tried almost everything, but I have no clue what's wrong.
I've written into server.xml the following:
<Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="20"
minPoolSize="12"
acquireIncrement="1"
name="jdbc/testdb1"
user="root"
password=""
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/test_db1?autoReconnect=true" />
and into context.xml the following:
<ResourceLink name="jdbc/testdb1"
global="jdbc/testdb1"
type="javax.sql.DataSource" />
and into web.xml the following:
<resource-ref>
<res-ref-name>jdbc/testdb1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
and my project (app) specific configuration:
PART 1 (JPA XML FILE):
<bean id="transactionManagerMysql" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emfMysql"/>
</bean>
<bean id="emfMysql" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jdbc/testdb1" />
<property name="persistenceXmlLocation" value="classpath:persistence-infrastructure.xml" />
<property name="persistenceUnitName" value="puMysql" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<jpa:repositories base-package="domain.data.repository"
entity-manager-factory-ref="emfMysql"
transaction-manager-ref="transactionManagerMysql"/>
PART 2 (PERSISTENCE XML FILE):
<persistence-unit name="puMysql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>domain.Mysqltable1</class>
<properties>
<property name="hibernate.connection.datasource" value="jdbc/testdb1"/>
</properties>
</persistence-unit>
I get an exception:
Error creating bean with name 'emfMysql' defined in class path resource [datasource-tx-jpa-infrastructure.xml]: Cannot resolve reference to bean 'jdbc/testdb1' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jdbc/testdb1' is defined
I hope someone can help me :)
Thanks ahead
It said that the datasource resource you defined in tomcat server.xml is not a bean in spring. You should define a datasource bean like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>
And use this bean instead of jndi name for datasource property of emfMysql.
Please refer this answer

Tiles 3 with spring MVC 3 integration not working

First time I am trying to integrate Tiles3 with spring MVC 3. My Spring servlet has the following entries:
<bean id="tilesviewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" p:order="0"/>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
The jar files I have in my project's class path are:
commons-beanutils-1.8.0.jar
commons-digester-2.0.jar
commons-lang-2.4.jar
commons-logging-1.0.4.jar
jstl-1.2.jar
log4j-1.2.16.jar
logback-access-1.0.0.jar
logback-classic-1.0.0.jar
logback-core-1.0.0.jar
logback-examples-1.0.0.jar
mysql-connector-java-5.0.5-bin.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE
org.springframework.jdbc-3.0.1.RELEASE-A.jar
org.springframework.transaction-3.0.1.RELEAS
org.springframework.web-3.0.1.RELEASE-A.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.10.jar
tiles-api-3.0.3.jar
tiles-autotag-core-runtime-1.1.0.jar
tiles-compat-3.0.3.jar
tiles-core-3.0.3.jar
tiles-el-3.0.3.jar
tiles-extras-3.0.3.jar
tiles-freemarker-3.0.3.jar
tiles-jsp-3.0.3.jar
tiles-mvel-3.0.3.jar
tiles-ognl-3.0.3.jar
tiles-request-api-1.0.3.jar
tiles-request-freemarker-1.0.3.jar
tiles-request-jsp-1.0.3.jar
tiles-request-mustache-1.0.3.jar
tiles-request-servlet-1.0.3.jar
tiles-request-servlet-wildcard-1.0.3.jar
tiles-request-velocity-1.0.3.jar
tiles-servlet-3.0.3.jar
tiles-template-3.0.3.jar
tiles-velocity-3.0.3.jar
But I am getting the error:
Cannot find class [org.springframework.web.servlet.view.tiles3.TilesConfigurer] for bean with name 'tilesConfigurer' defined in ServletContext resource [/WEB-INF/thinkTank-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.tiles3.TilesConfigurer
thinkTank-servlet.xml is my spring servlet, can anyone help, what I am doing wrong:
You need spring 3.2 for the org.springframework.web.servlet.view.tiles3 classes.
If for some reason you can't upgrade then you can download just the classes you need here from http://wever.org/spring-webmvc-tiles3-3.2.0.RC2-finn-1.jar
That jar file should be compatible with the spring 3.0.1 you have, but i would still recommend upgrading to 3.2 if you can.
add this snipt in your spring servlet
<beans:bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/spring/appServlet/views.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>

SpringMVC+Thymeleaf ,error message is : template might not exist or might not be accessible by any of the configured Template Resolvers

I've read the other answers, but no matter,thanks
Spring:
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
</bean>
pom.xml:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>2.0.13</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "login.html", template might not exist or might not be accessible by any of the configured Template Resolvers
Do you need a template resolver?
Try something like this:
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="templateMode" value="HTML5" />
</bean>
Make sure that /path/after/web-inf/login.html is being returned by a Spring MVC controller.
You are likely missing the "suffix" property within the templateResolver configuration. It should be best practice to use both a path "prefix" (where the file is located) and a "suffix" (the file extension - otherwise thymeleaf tries to resolve just "login" w/o an extension) within the template resolver configuration. Like so:
<beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<beans:property name="prefix" value="/WEB-INF/views/"/>
<beans:property name="suffix" value=".html"/>
<beans:property name="templateMode" value="HTML5"/>
<beans:property name="cacheable" value="false"/> <!-- Development only -->
</beans:bean>
And requesting the login view within a controller does not include the file extension.
#RequestMapping(value = "/login")
public String login(HttpSession session) {
// do stuff
// Thymeleaf (delegated by springmvc) will request the "login" view at which
// point the prefix and suffix are added to resolve the template location.
return "login";
}
So based on this configuration and within a maven-based project, the login.html should be located at
${project_home}/src/main/webapp/WEB-INF/views/login.html
Maybe remove slash "/" in the controller request mapping will work.
#RequestMapping(value = "login")
In my case it turned out that thymeleaf html parser works poorly and doesn't accept one line html files. Formatting it solved the problem.

Apache tiles EL support in Spring 3

I'm trying to configure apache tiles 2.2.2 EL support in Spring 3.1.
According to tiles documentation and from what I've found on google, this should work, but it doens't, instead, it throws NullPointerException
Tiles config:
<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
<property name="order" value="1"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/**/views-def.xml</value>
</list>
</property>
<property name="tilesProperties">
<props>
<prop key="org.apache.tiles.evaluator.AttributeEvaluator">
org.apache.tiles.el.ELAttributeEvaluator
</prop>
</props>
</property>
</bean>
Simple tiles definition:
<tiles-definitions>
<definition name="temp.test" template="/WEB-INF/views/temp/test.jsp">
<put-attribute name="test" expression="${test}" />
</definition>
</tiles-definitions>
test.jsp
<%--Works fine--%>
<h2>Called in jsp</h2>
<p>
<c:out value="${test}"/>
</p>
<%-- throws NullPointerException --%>
<h2>Inserted from tiles</h2>
<p>
<tiles:getAsString name="test"/>
</p>
Tiles libraries on cp:
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-template</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>2.2.2</version>
</dependency>
use
<!--this works fine with expressions-->
<tiles:insertAttribute name="test" ignore="true" />
instead of
<!-- and this will throw NullPointerException if value is not provided-->
<tiles:getAsString name="test" ignore="true"/>
Had similar behavior with null pointer while playing with spring mvc 3.1.2.RELEASE and tiles 2.2.2. Check your xerxesImpl if is the latest 2.9.1. Also check your taglib as mentioned earlier also check your logging. These work fine together:
<org.slf4j.version>1.5.8</org.slf4j.version>
<log4j.version>1.2.16</log4j.version>
<xercesImpl.version>2.9.1</xercesImpl.version>
<org.springframework-version>3.1.2.RELEASE</org.springframework-version>
<tiles.core.api.servlet.jsp.version>2.2.2</tiles.core.api.servlet.jsp.version>

Resources