I done my MVC web application using spring security 2.0.4 and spring 2.5 and HSQLDB, where I made CRUD application. For products I already used HSQL as a database. And I integrated security by using roles which are hard coded in my applicationContext-security.xml like this:
Now I have to use same HSQL databse which I am using for products for the roles(Their user names and passwords). So I done the following cofigurations in my application:Code:<authentication-provider> <user-service id="userDetailsService"> <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> <user name="username" password="password" authorities="ROLE_USER" /> <user name="test" password="test" authorities="ROLE_USER" /> </user-service> </authentication-provider>
My dataAccessContext.xml
This is my dataSourcePopulator file: HsqldbSchemaAndDataPopulator.javaCode:<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:mem:test" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean> <bean id="dataSourcePopulator" class="springapp1.service.HsqldbSchemaAndDataPopulator"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
My web.xml snippet:Code:package springapp1.service; import javax.sql.DataSource; import org.springframework.beans.factory.InitializingBean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.Assert; /** * I am responsible for populating the configured datasource */ public class HsqldbSchemaAndDataPopulator implements InitializingBean { private JdbcTemplate template; /** * */ public void afterPropertiesSet() throws Exception { Assert.notNull(template, "dataSource required"); // add tables to represent admin core-domain instances. template .execute("CREATE TABLE USERS(USERNAME VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY," + "PASSWORD VARCHAR_IGNORECASE(50) NOT NULL," + "ENABLED BOOLEAN NOT NULL);"); template .execute("CREATE TABLE AUTHORITIES(USERNAME VARCHAR_IGNORECASE(50) NOT NULL,AUTHORITY VARCHAR_IGNORECASE(50) NOT NULL,CONSTRAINT FK_AUTHORITIES_USERS FOREIGN KEY(USERNAME) REFERENCES USERS(USERNAME));"); template .execute("CREATE UNIQUE INDEX IX_AUTH_USERNAME ON AUTHORITIES(USERNAME,AUTHORITY);"); // insert data here template .execute("INSERT INTO USERS VALUES('disabled','disabled',FALSE);"); template.execute("INSERT INTO USERS VALUES('admin','admin',TRUE);"); template .execute("INSERT INTO USERS VALUES('username','password',TRUE);"); template.execute("INSERT INTO USERS VALUES('test','test',TRUE);"); template .execute("INSERT INTO AUTHORITIES VALUES('admin','ROLE_USER');"); template .execute("INSERT INTO AUTHORITIES VALUES('admin','ROLE_ADMIN');"); template .execute("INSERT INTO AUTHORITIES VALUES('username','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('test','ROLE_USER');"); } public void setDataSource(final DataSource dataSource) { this.template = new JdbcTemplate(dataSource); } }
I updated my applicationContext-security.xml fileCode:<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext-security.xml /WEB-INF/dataAccessContext.xml /WEB-INF/applicationContext.xml </param-value> </context-param>
Now when i run the application it gives me following error in localhost log file:Code:<authentication-provider> <jdbc-user-service id="userDetailsService" data-source-ref="dataSource" /> </authentication-provider>
And its looking in the product list not in the users info list.Code:exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select id, description, price from products]; nested exception is java.sql.SQLException: Table not found in statement [select id, description, price from products] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:583) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) .... root cause org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select id, description, price from products]; nested exception is java.sql.SQLException: Table not found in statement [select id, description, price from products] org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:220) org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:407) org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:458) org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:466) org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:187) springapp1.repository.JdbcProductDao.getProductList(JdbcProductDao.java:58) springapp1.service.SimpleProductManager.getProducts(SimpleProductManager.java:20) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ...
Continued...


Reply With Quote
