java - Spring security null pointer exception -
i'm trying map users in database spring security users, without luck. userserviceimpl follows (the autowiring works fine when i'm calling through servlet, throws null pointer when used in spring security...
@service("userservice") @transactional public class userserviceimpl implements userservice, userdetailsservice { protected static logger logger = logger.getlogger("service"); @autowired private userdao userdao; public userserviceimpl() { } @transactional public user getbyid(long id) { return userdao.getbyid(id); } @transactional public user getbyusername(string username) { return userdao.getbyusername(username); } @override public userdetails loaduserbyusername(string username) throws usernamenotfoundexception { userdetails user = null; try { system.out.println(username); user dbuser = getbyusername(username); user = new org.springframework.security.core.userdetails.user( dbuser.getusername(), dbuser.getpassword(), true, true, true, true, getauthorities(dbuser.getaccess())); } catch (exception e) { e.printstacktrace(); logger.log(level.fine, "error in retrieving user"); throw new usernamenotfoundexception("error in retrieving user"); } // return user spring processing. // take note we're not 1 evaluating whether user // authenticated or valid // merely retrieve user matches specified username return user; } public collection<grantedauthority> getauthorities(integer access) { // create list of grants user list<grantedauthority> authlist = new arraylist<grantedauthority>(2); // users granted role_user access // therefore user gets role_user default logger.log(level.info, "user role selected"); authlist.add(new grantedauthorityimpl("role_user")); // check if user has admin access // interpret integer(1) admin user if (access.compareto(1) == 0) { // user has admin access logger.log(level.info, "admin role selected"); authlist.add(new grantedauthorityimpl("role_admin")); } // return list of granted authorities return authlist; } } i following exception (the first line system.out)
dusername java.lang.nullpointerexception @ org.assessme.com.service.userserviceimpl.getbyusername(userserviceimpl.java:40) @ org.assessme.com.service.userserviceimpl.loaduserbyusername(userserviceimpl.java:50) @ org.springframework.security.authentication.dao.daoauthenticationprovider.retrieveuser(daoauthenticationprovider.java:81) @ org.springframework.security.authentication.dao.abstractuserdetailsauthenticationprovider.authenticate(abstractuserdetailsauthenticationprovider.java:132) @ org.springframework.security.authentication.providermanager.authenticate(providermanager.java:156) @ org.springframework.security.authentication.providermanager.authenticate(providermanager.java:174) @ org.springframework.security.web.authentication.usernamepasswordauthenticationfilter.attemptauthentication(usernamepasswordauthenticationfilter.java:94) @ org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:194) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:323) @ org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:105) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:323) @ org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:87) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:323) @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:173) @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:346) @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:259) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:235) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:233) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:191) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:127) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:298) @ org.apache.coyote.http11.http11processor.process(http11processor.java:859) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:588) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:489) @ java.lang.thread.run(thread.java:722) so looks userdao not autowiring correctly, works fine when call service layer servlet, apparently not when using spring-security.
line 40 refers return userdao.getbyusername(username);
does have ideas how can userdao populated through @autowired? say, works fine when call through servlet, not when trying use spring-security.
is there easier way can map users , passwords in spring-security?
my security-app-context follows...
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"> <context:annotation-config /> <context:component-scan base-package="org.assessme.com." /> <http pattern="/static/**" security="none" /> <http use-expressions="true"> <intercept-url pattern="/login" access="permitall" /> <intercept-url pattern="/*" access="isauthenticated()" /> <!-- <intercept-url pattern="/secure/extreme/**" access="hasrole('supervisor')" /> --> <!-- <intercept-url pattern="/listaccounts.html" access="isauthenticated()" /> --> <!-- <intercept-url pattern="/post.html" access="hasanyrole('supervisor','teller')" /> --> <!-- <intercept-url pattern="/*" access="denyall" /> --> <form-login /> <logout invalidate-session="true" logout-success-url="/" logout-url="/logout" /> </http> <authentication-manager> <authentication-provider user-service-ref="userdetailsservice"> <password-encoder ref="passwordencoder"/> </authentication-provider> </authentication-manager> <context:component-scan base-package="org.assessme.com" /><context:annotation-config /> <beans:bean class="org.springframework.security.authentication.encoding.md5passwordencoder" id="passwordencoder"/> <beans:bean class="org.assessme.com.service.userserviceimpl" id="userdetailsservice" autowire="bytype"/> </beans:beans> i guess question is, why userdao @autowired not working spring-security, yet works fine when used in servlet return user object? example, following servlet works fine...
why autowiring (as throws npe) not working when it's going through spring-security, yet works fine when being called servlet?
edit :- added
but
error: org.springframework.web.context.contextloader - context initialization failed org.springframework.beans.factory.xml.xmlbeandefinitionstoreexception: line 9 in xml document servletcontext resource [/web-inf/spring/security-app-context.xml] invalid; nested exception org.xml.sax.saxparseexception; linenumber: 9; columnnumber: 30; cvc-complex-type.2.4.c: matching wildcard strict, no declaration can found element 'context:annotation-config'.
you care creating service through bean declaration:
<beans:bean class="org.assessme.com.service.userserviceimpl" id="userdetailsservice"/> therefore need configure eligible autowiring setting property autowire. default no. configuration like:
<beans:bean class="org.assessme.com.service.userserviceimpl" id="userdetailsservice" autowire="bytype" /> additionally indicate bean participate in others bean autowire process configuring property autowire-candidate="true".
check documentation in order find out best strategy bean have properties autowired. use bytype , constructor, depends on requirement.
another solution configure default-autowire="true" in tag beans of context.
Comments
Post a Comment