I'm using Spring 4.3.1.RELEASE version and it's custom Authentication Login application. But I'm facing issue
First take a look on code
CustomAuthenticationProvider.java
@Component
@Qualifier(value = "customAuthenticationProvider")
public class CustomAuthenticationProvider implements AuthenticationProvider{
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = (String) authentication.getCredentials();
User user = new User();
user.setUsername(username);
user.setPassword(password);
Role r = new Role();
r.setName("ROLE_ADMIN");
List<Role> roles = new ArrayList<Role>();
roles.add(r);
Collection<? extends GrantedAuthority> authorities = roles;
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
public boolean supports(Class<?> arg0) {
return true;
}
}
SecurityConfiguration.java
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
//.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_USER')")
.and()
.formLogin()
.loginPage("/login").failureUrl("/login?error")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.csrf();
}
}
login.jsp Here is my Login page
<form name="loginForm" novalidate ng-submit="ctrl.login(user)">
<div class="form-group" ng-class="{'has-error': loginForm.username.$invalid}">
<input class="form-control" name="username" id="username" type="text"
placeholder="Username" required ng-model="user.username" />
<span class="help-block"
ng-show="loginForm.username.$error.required">Required</span>
</div>
<div class="form-group" ng-class="{'has-error': loginForm.password.$invalid}">
<input class="form-control" name="password" id="password" type="password"
placeholder="Password" required ng-model="user.password" />
<span class="help-block"
ng-show="loginForm.password.$error.required">Required</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary pull-right"
value="Login" title="Login" ng-disabled="!loginForm.$valid">
<span>Login</span>
</button>
</div>
</form>
On authenticate() in CustomAuhtenticationProvider class
- authentication.getCredentials();
- authentication.getName();
both giving empty string , but I need username and password in this.
Here is my AngularJS Service
Service.js
function loginUser(user) {
var config = {
headers: {
'csrf_token': csrfToken
}
}
var deferred = $q.defer();
$http.post("/login", user,config)
.then(
function (response) {
deferred.resolve(response.data);
},
function(errResponse){
console.error('Error while creating User');
deferred.reject(errResponse);
}
);
return deferred.promise;
}