Hi i am adding spring security to my application (spring with angular). But i am getting my service resource as null. Following is my code.
here is my security.xml contents
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<http auto-config="true" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/home" access="isAuthenticated()" />
<form-login />
</http>
<beans:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<beans:property name="loginFormUrl" value="/" />
</beans:bean>
<beans:bean id="securityFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationSuccessHandler"
ref="authenticationSuccessHandler" />
</beans:bean>
<beans:bean id="customUserDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="customUserDetailsService"/>
</authentication-manager>
<beans:bean id="authenticationSuccessHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<beans:property name="defaultTargetUrl" value="/home" />
</beans:bean>
</beans:beans>
and my custom user details service class is
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Resource
UserRepository userRepository;
@Resource
UserService userService;
@Override
public UserDetails loadUserByUsername(String email)
throws UsernameNotFoundException {
try{
User user = userService.getUser(email);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
and my angular controller is
DashBoard.controller('LoginController', function($scope,$http,$location) {
$scope.login = function(){
var data = "j_username="+$scope.username+"&j_password="+$scope.password+"&_spring_security_remember_me=true";
$http.post('j_spring_security_check', data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}).success(function(data,status) {
$location.path('/home');
}).
error(function(data,status) {
console.debug("failed :"+status+" Data : "+data);
$location.path('/login');
});
};
});
and login form is
<form class="form-horizontal" name="signUpForm" ng-controller="LoginController" ng-submit="login()">
<input class="span12 tenF" ng-model="username" type="email" id="j_username" name="j_username" placeholder="E-mail address" required>
<input class="span12 tenF" type="password" ng-model="password" id="j_password" name="j_password" placeholder="Password" required>
<input class="span3 btn btn-large btn-block btn-success" type="submit" value="Login" style="float:right;font-size:20px;margin-top:10px;">
</ng-form>
while debugging i reaches to
User user = userService.getUser(email);//.findByEmail(email);
but userService is null. Can any one kindly help me figure out why it is null.
The problem is the mixing of xml configuration and annotation based autowiring.
In such an situation, I usual add an constructor that take all the required parameter and add in the xml autowire="constructor"
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository userRepository;
private UserService userService;
@Autowire
public CustomUserDetailsService(UserRepository userRepository,
UserService userService){
this.userRepository=userRepository;
this.userService=userService;
}
....
}
<beans:bean id="customUserDetailsService"
class="com.dashboard.service.CustomUserDetailsService"
autowire="constructor"/>