spring - Autowire doesnt work in custom UserDetailsService -
in custom user details service, variable "auserservice" doesnt auto wire annotated. tried lot method, still failed , value null.
acutally follow krams tutorial. http://krams915.blogspot.sg/2012/01/spring-security-31-implement_1244.html
here project https://skydrive.live.com/#cid=837ef1fa9a4c06ae&id=837ef1fa9a4c06ae%21130 ,enviroment: netbeans 7.2,maven,tomcat 7.0.39,postgresql 9.2,db sql inside project. db pool defined @ meta-inf/context.xml
here web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>setencoding</filter-name> <filter-class>sg.com.innovax.opscentralv5.objects.setencoding</filter-class> </filter> <filter-mapping> <filter-name>setencoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ================================ start captha ================================ --> <filter> <filter-name>jcaptchafilter</filter-name> <filter-class>sg.com.innovax.jcaptcha.jcaptchafilter</filter-class> <init-param> <param-name>failureurl</param-name> <param-value>/?error=true</param-value> </init-param> </filter> <filter-mapping> <filter-name>jcaptchafilter</filter-name> <url-pattern>/jcaptcha.jpg</url-pattern> </filter-mapping> <filter-mapping> <filter-name>jcaptchafilter</filter-name> <url-pattern>/j_spring_security_check</url-pattern> </filter-mapping> <!-- ================================ end captha ================================ --> <!-- spring security --> <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> <!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value> /web-inf/spring/root-context.xml /web-inf/spring/security.xml </param-value> </context-param> <!-- creates spring container shared servlets , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- processes application requests --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <error-page> <error-code>401</error-code> <location>/web-inf/views/401.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/web-inf/views/403.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/web-inf/views/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/web-inf/views/500.jsp</location> </error-page> <resource-ref> <description>postgresql</description> <res-ref-name>jdbc/postgres</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> </resource-ref> <!-- solr server url --> <context-param> <param-name>solr_url</param-name> <param-value>http://localhost:8080/solr</param-value> </context-param> </web-app> my spring servlet xml
<?xml version="1.0" encoding="utf-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- resolves views selected rendering @controllers .jsp resources in /web-inf/views directory --> <beans:bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <beans:property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/> <beans:property name="prefix" value="/web-inf/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> </beans:beans> my security xml:
<?xml version="1.0" encoding="utf-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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/context/spring-context-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <debug /> <global-method-security pre-post-annotations="enabled"/> <http pattern="/resources/**" security="none"/> <http pattern="/test/**" security="none"/> <http pattern="/loggedout.jsp" security="none"/> <http pattern="/forgotpassword" security="none"/> <http pattern="/processresetpassword" security="none"/> <http pattern="/test" security="none"/> <http pattern="/jcaptcha.jpg" security="none"/> <http use-expressions="true"> <intercept-url pattern="/" access="permitall"/> <intercept-url pattern="/**" access="isauthenticated()" /> <form-login login-processing-url="/j_spring_security_check" login-page="/" default-target-url="/user/" always-use-default-target="true" authentication-failure-url="/?error=true" /> <remember-me key="opscentral" token-validity-seconds="3600"/> <logout logout-url="/j_spring_security_logout" /> </http> <context:annotation-config /> <context:component-scan base-package="sg.com.innovax" /> <authentication-manager > <authentication-provider user-service-ref="customuserdetailsservice"> <password-encoder hash="sha"/> </authentication-provider> </authentication-manager> <!-- jcaptcha --> <beans:bean id="captchaservice" class="com.octo.captcha.service.image.defaultmanageableimagecaptchaservice"> <beans:property name="captchaengine"> <beans:bean class="sg.com.innovax.jcaptcha.gmailengine" /> </beans:property> <!-- 180 secs expired <property name="minguarantedstoragedelayinseconds" value="180" /> --> </beans:bean> </beans:beans> my root-context xml
<?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:jee="http://www.springframework.org/schema/jee" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd "> <context:annotation-config /> <context:component-scan base-package="sg.com.innovax" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <tx:annotation-driven transaction-manager="transactionmanager" /> <!-- root context: defines shared resources visible other web components --> <jee:jndi-lookup id="datasource" jndi-name="jdbc/postgres" expected-type="javax.sql.datasource" /> <beans:bean id="localeresolver" class="org.springframework.web.servlet.i18n.sessionlocaleresolver"> <beans:property name="defaultlocale" value="en" /> </beans:bean> <!-- register welcome.properties --> <beans:bean id="messagesource" class="org.springframework.context.support.resourcebundlemessagesource"> <beans:property name="basename" value="welcome" /> </beans:bean> <!-- jpa --> <beans:bean id="emf" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <beans:property name="datasource" ref="datasource" /> <beans:property name="jpavendoradapter"> <beans:bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <beans:property name="database" value="postgresql" /> </beans:bean> </beans:property> <beans:property name="packagestoscan" value="sg.com.innovax" /> <beans:property name="jpaproperties"> <beans:props> <beans:prop key="hibernate.dialect">org.hibernate.dialect.postgresqldialect</beans:prop> <beans:prop key="hibernate.show_sql">true</beans:prop> </beans:props> </beans:property> </beans:bean> <beans:bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <beans:property name="entitymanagerfactory" ref="emf" /> </beans:bean> <beans:bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <beans:property name="maxuploadsize" value="100000000"/> </beans:bean> my custom user detail service
package sg.com.innovax.opscentralv5.objects; import java.util.arraylist; import java.util.collection; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.security.core.grantedauthority; import org.springframework.security.core.authority.simplegrantedauthority; import org.springframework.security.core.userdetails.user; import org.springframework.security.core.userdetails.userdetails; import org.springframework.security.core.userdetails.userdetailsservice; import org.springframework.security.core.userdetails.usernamenotfoundexception; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import sg.com.innovax.opscentralv5.table.service.userservice; /** * custom {@link userdetailsservice} user information * retrieved jpa repository */ @service @transactional(readonly = true) public class customuserdetailsservice implements userdetailsservice { @autowired private userservice auserservice; /** * returns populated {@link userdetails} object. * username first retrieved database , mapped * {@link userdetails} object. */ public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { try { sg.com.innovax.opscentralv5.table.user u = auserservice.findbyusername(username); string pass = sg.com.innovax.opscentralv5.table.user.bytetohex(u.getpassword()); boolean enabled = true; boolean accountnonexpired = true; boolean credentialsnonexpired = true; boolean accountnonlocked = true; return new user( u.getusername(), pass, enabled, accountnonexpired, credentialsnonexpired, accountnonlocked, getauthorities(1)); } catch (exception e) { system.out.println(e.tostring()); throw new runtimeexception(e); } } /** * retrieves collection of {@link grantedauthority} based on numerical role * @param role numerical role * @return collection of {@link grantedauthority */ public collection<? extends grantedauthority> getauthorities(integer role) { list<grantedauthority> authlist = getgrantedauthorities(getroles(role)); return authlist; } /** * converts numerical role equivalent list of roles * @param role numerical role * @return list of roles as list of {@link string} */ public list<string> getroles(integer role) { list<string> roles = new arraylist<string>(); if (role.intvalue() == 1) { roles.add("role_user"); roles.add("role_admin"); } else if (role.intvalue() == 2) { roles.add("role_user"); } return roles; } /** * wraps {@link string} roles {@link simplegrantedauthority} objects * @param roles {@link string} of roles * @return list of granted authorities */ public static list<grantedauthority> getgrantedauthorities(list<string> roles) { list<grantedauthority> authorities = new arraylist<grantedauthority>(); (string role : roles) { authorities.add(new simplegrantedauthority(role)); } return authorities; } } my user service implement
package sg.com.innovax.opscentralv5.table.service.impl; import .....; @service("jpauserservice") @repository @transactional public class userserviceimpl implements userservice { private static final logger logger = loggerfactory.getlogger(userserviceimpl.class); @persistencecontext private entitymanager em; @transactional(readonly=true) public user findbyusername(string username) { ........ } } my user class
package sg.com.innovax.opscentralv5.table; import ....; @entity @table(name = "users", uniqueconstraints = { @uniqueconstraint(columnnames = "username") }) @namedqueries({ ..... }) public class user implements serializable { public static final string role_admin = "role_admin"; public static final string role_user = "role_user"; @id @sequencegenerator(name="users_id_seq", sequencename="users_id_seq", allocationsize=1) @generatedvalue(strategy=generationtype.sequence, generator="users_id_seq") @basic(optional = false) private integer id; @column(unique=true) private string username; private byte[] password; private string email; private string mobile; private boolean enabled; private timestamp deleted; @onetoone(cascade=cascadetype.all) @primarykeyjoincolumn private role role; public integer getid() { return id; } ...... } my user interface
package sg.com.innovax.opscentralv5.table.service; import ....; import sg.com.innovax.opscentralv5.table.user; public interface userservice { public list<user> findall(); ......; }
problems solved. 2 reasons caused it.
remove
<debug/>security xml https://jira.springsource.org/browse/sec-1885put org.springframework.web.servlet.view.internalresourceviewresolver class in spring-servlet.xml(previously put in application context xml).
now:
my web.xml
...... <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ...... my spring-servlet.xml
<?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.1.xsd"> <!-- resolves views selected rendering @controllers .jsp resources in /web-inf/views directory --> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/> <property name="prefix" value="/web-inf/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
Comments
Post a Comment